begin process at 2010 02 10 10:28:32
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

Périphériques

 > DRIVER MODBUS RS232 ASCII LRC

DRIVER MODBUS RS232 ASCII LRC


 Information sur la source

Note :
Aucune note
Catégorie :Périphériques Source .NET ( DotNet ) Classé sous :LRC, Modbus, x2bf3, ASCII, RS232 Niveau :Initié Date de création :15/10/2007 Vu :8 835

Auteur : x2bf3

Ecrire un message privé
Commentaire sur cette source (1)
Ajouter un commentaire et/ou une note

 Description

Code semi-final d'un driver RS232 pour un Cubloc CB290 sur une proto-board.
Interfaçage RS232, Modbus ASCII avec test LRC ( qui fonctionne ! ).
Lecture d'entrées analogiques, TOR, Registres Dxx et ecriture sur registres.

Code du Cubloc CB290 ( Non finalisé ):

Const Device = cb290
Ramclear
Set Outonly On

Opencom 1,115200,3,80,80
Set Modbus 0,3

Set Ladder On

Usepin 40,Out

Input 0
Input 1                              
Input 2
Input 3
Input 4
Input 5
Input 6
Input 7

Low 5
Low 6
Low 7
Low 89
Low 90
Low 91

Do
Dim test As Integer


If _P(87) Then
_d(10)=_d(10)+1
Endif
If _P(86) Then
_d(10)=_d(10)-1
Endif

If _P(85) Then
_d(11)=_d(11)+1
Endif
If _P(84) Then
_d(11)=_d(11)-1
Endif

If _P(83) Then
_d(12)=_d(12)+1
Endif
If _P(82) Then
_d(12)=_d(12)-1
Endif

If _P(81) Then
_d(13)=_d(13)+1
Endif
If _P(79) Then
_d(13)=_d(13)-1
Endif

If _P(78) Then
_d(14)=_d(14)+1
Endif
If _P(77) Then
_d(14)=_d(14)-1
Endif

If _P(76) Then
_d(15)=_d(15)+1
Endif
If _P(75) Then
_d(15)=_d(15)-1
Endif

'Pwm 0,analog,32768 sortie Pwm P5
'Pwm 1,analog,32768 sortie Pwm

'_d(0)= Adin(0)
'_d(1)= Adin(1)
'_d(2)= Adin(2)
'_d(3)= Adin(3)
'_d(4)= Adin(4)
'_d(5)= Adin(5)
'_d(6)= Adin(6)
'_d(7)= Adin(7)


'_d(100)= 123
'_d(101)= 200
'_d(102)= 300
'_d(102)= 400
'_d(103)= 500
'_d(104)= 600
'_d(105)= 700
'_d(106)= 800
'_d(107)= 900
'_d(108)= 1000
'_d(109)= 1100




'Pwm 0, _d(10),32768
'Pwm 1, _d(11),32768
'Pwm 2, _d(12),32768
'Pwm 3, _d(13),32768
'Pwm 4, _d(14),32768
'Pwm 5, _d(15),32768

'_D(8) = Keypad(0)

Loop

Source

  • using System;
  • using System.Collections.Generic;
  • using System.ComponentModel;
  • using System.Data;
  • using System.Drawing;
  • using System.Text;
  • using System.Windows.Forms;
  • namespace WindowsApplication2
  • {
  • public partial class Form1 : Form
  • {
  • public Form1()
  • {
  • InitializeComponent();
  • }
  • private void Form1_Load(object sender, EventArgs e)
  • {
  • //Initialisation Communication RS232
  • RS232_ModBus.init_RS232();
  • }
  • private void button1_Click_2(object sender, EventArgs e)
  • {
  • timer1.Enabled = true;
  • }
  • private void timer1_Tick(object sender, EventArgs e)
  • {
  • /*Lecture des entrées P56 à P87
  • textBox4.Text = lecture_Px().P80;//debug;
  • Lecture de D0 à D15 + mise en forme
  • Dx_Entree Dxx = new Dx_Entree (lecture_Dx(28672,16));
  • textBox4.Text = Dxx.D1+"";*/
  • /*string debug = "";
  • int[]temps = new int[int.Parse(textBox5.Text)];
  • //28772
  • temps = RS232_ModBus.lecture_Dx(int.Parse(textBox1.Text),16);
  • try
  • {
  • for (int i = 0; i < int.Parse(textBox5.Text); i++)
  • debug += "D(" + i + "):" + temps[i] + " ";
  • textBox4.Text = debug;
  • }
  • catch
  • {
  • textBox4.Text = "erreure";
  • }*/
  • /*Ana_Entree Ana = new Ana_Entree (lecture_Ana());
  • textBox4.Text = Ana.Ana1+"";*/
  • /*textBox2.Text = (int.Parse(textBox2.Text)) + "";
  • ecrire_un_mot(100, int.Parse(textBox2.Text));*/
  • }
  • }
  • public class RS232_ModBus //Noyau de communication ModBus Cubloc ASCII
  • {
  • //Adresse de l'esclave ( Le cubloc CB290 )
  • private static string adresse_esclave = "03";
  • //Initialisation du tampon de trames RS232 ModBus ASCII
  • private static string trame_rs232 = "";
  • //Initialisation port serie (RS232 pour Modbus Cubloc)
  • public static void init_RS232()
  • {
  • try
  • {
  • Form1.serialPort1.BaudRate = 115200;
  • Form1.serialPort1.DataBits = 8;
  • Form1.serialPort1.Parity = System.IO.Ports.Parity.None;
  • Form1.serialPort1.StopBits = System.IO.Ports.StopBits.One;
  • Form1.serialPort1.PortName = "COM3";
  • Form1.serialPort1.Open();
  • }
  • catch{}
  • }
  • //Conversion d'une String en Byte Array
  • private static byte[] StrToByteArray(string str)
  • {
  • System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
  • return encoding.GetBytes(str);
  • }
  • //Ajoute un byte '\n' à la fin d'une chaine de Bytes
  • private static byte[] Retour_chariot(string trame)
  • {
  • byte[] tramebyte = new byte[trame.Length];
  • tramebyte = StrToByteArray(trame);
  • byte[] tramebyte1 = new byte[tramebyte.Length + 1];
  • for (int i = 0; i < tramebyte.Length; i++)
  • { tramebyte1[i] = tramebyte[i]; }
  • tramebyte1[tramebyte.Length] = (byte)'\n';
  • return tramebyte1;
  • }
  • //Generation du LRC
  • private static string LRC(String trame_test)
  • {
  • Char[] instructions = new Char[trame_test.Length / 2];
  • for (Int32 i = 0; i < trame_test.Length; i += 2)
  • instructions[i / 2] = (Char)Byte.Parse(trame_test.Substring(i, 2),
  • System.Globalization.NumberStyles.HexNumber);
  • int LRC = 0;
  • for (int i = 0; i < instructions.Length; i++)
  • LRC += (int)instructions[i];
  • LRC = 255 - LRC + 1;
  • return LRC.ToString("x2");
  • }
  • //Transcodage tout string d'hexa en decimal
  • private static int transcode_16bits(String trame_test)
  • {
  • return Convert.ToInt32(trame_test, 16);
  • }
  • //Transcode tout decimal en hexa XX XX
  • private static string transcode_hexa(int nombre)
  • {
  • return nombre.ToString("x4");
  • }
  • //Transcode tout decimal en hexa XX
  • private static string transcode_nbhexa(int nombre)
  • {
  • return nombre.ToString("x2");
  • }
  • //Generateur de trame de lecture _D() Cubloc CB290
  • private static string lire_registre_D(int adresse_depart, int nombre_registres)
  • {
  • //Adresse de départ, Nombre de données à récupérer
  • string trame = "";
  • string code_fonction = "03"; //0x03 Lecture de registre maintenu
  • string corps_trame = adresse_esclave + code_fonction + transcode_hexa(adresse_depart) + transcode_hexa(nombre_registres);
  • trame = ":" + corps_trame + LRC(corps_trame);
  • return trame.ToUpper();
  • }
  • //Lecture des registre Dxx
  • public static int[] lecture_Dx(int position, int nombre)
  • {
  • //28672
  • //Coder la trame de lecture des _D(x)
  • trame_rs232 = lire_registre_D(position, nombre);
  • //Emmetre la trame vers l'esclave
  • Form1.serialPort1.Write(Retour_chariot(trame_rs232), 0, Retour_chariot(trame_rs232).Length);
  • //Lire la trame de retour
  • string donnee_recues = Form1.serialPort1.ReadLine();
  • //Interpréter la trame reçue
  • int esclave = transcode_16bits(donnee_recues.Substring(1, 2));
  • int commande = transcode_16bits(donnee_recues.Substring(3, 2));
  • int nombre_donnee = transcode_16bits(donnee_recues.Substring(5, 2)) / 2;
  • int[] valeures = new int[nombre_donnee];
  • if (donnee_recues.Substring(3, 2) == "03")
  • for (int i = 0; i < nombre_donnee; i++)
  • {
  • valeures[i] = transcode_16bits(donnee_recues.Substring(7 + 4 * i, 4));
  • //debug += "--" + donnee_recues.Substring(7 + 4 * i, 4);
  • }
  • return valeures;
  • }
  • //Lecture des registres Analog
  • public static int[] lecture_Ana()
  • {
  • //28672
  • //Coder la trame de lecture des _D(x)
  • trame_rs232 = lire_registre_D(28672, 8);
  • //Emmetre la trame vers l'esclave
  • Form1.serialPort1.Write(Retour_chariot(trame_rs232), 0, Retour_chariot(trame_rs232).Length);
  • //Lire la trame de retour
  • string donnee_recues = Form1.serialPort1.ReadLine();
  • //Interpréter la trame reçue
  • int esclave = transcode_16bits(donnee_recues.Substring(1, 2));
  • int commande = transcode_16bits(donnee_recues.Substring(3, 2));
  • int nombre_donnee = transcode_16bits(donnee_recues.Substring(5, 2)) / 2;
  • int[] valeures = new int[nombre_donnee];
  • for (int i = 0; i < nombre_donnee; i++)
  • {
  • valeures[i] = transcode_16bits(donnee_recues.Substring(7 + 4 * i, 4));
  • //debug += "--" + donnee_recues.Substring(7 + 4 * i, 4);
  • }
  • return valeures;
  • }
  • //Ecriture d'un bloc de registre Dxx
  • public static string ecrire_registre_D(int adresse_depart, int nombre_registres, int[] data)
  • {
  • string code_fonction = "10";
  • string corps_trame = adresse_esclave + code_fonction + transcode_hexa(adresse_depart) + transcode_hexa(nombre_registres) + transcode_nbhexa(nombre_registres * 2);
  • for (int i = 0; i < data.Length; i++)
  • corps_trame += transcode_hexa(data[i]);
  • return ":" + corps_trame + LRC(corps_trame);
  • }
  • //Ecriture d'un seul mot Dxx
  • public static void ecrire_un_mot(int position_D, int valeure)
  • {
  • //D100 : 28772
  • //Coder la trame de lecture des _D(x)
  • int[] donnees = new int[1];
  • donnees[0] = valeure;
  • trame_rs232 = ecrire_registre_D(28672 + position_D, 1, donnees).ToUpper();
  • //Emmetre la trame vers l'esclave
  • Form1.serialPort1.Write(Retour_chariot(trame_rs232), 0, Retour_chariot(trame_rs232).Length);
  • //Lire la trame de retour
  • Form1.serialPort1.ReadLine();
  • }
  • //Lecture de l'etat des entrées de la proto-board
  • public static Px_Entree lecture_Px()
  • {
  • //P56 à P87
  • int position = 50;
  • int nombre = 3;
  • //Coder la trame de lecture des _D(x)
  • trame_rs232 = lire_registre_D(position, nombre);
  • //Emmetre la trame vers l'esclave
  • Form1.serialPort1.Write(Retour_chariot(trame_rs232), 0, Retour_chariot(trame_rs232).Length);
  • //Lire la trame de retour
  • string donnee_recues = Form1.serialPort1.ReadLine();
  • //Interpréter la trame reçue
  • int esclave = transcode_16bits(donnee_recues.Substring(1, 2));
  • int commande = transcode_16bits(donnee_recues.Substring(3, 2));
  • int nombre_donnee = transcode_16bits(donnee_recues.Substring(5, 2));
  • int[] valeures = new int[nombre_donnee];
  • for (int i = 0; i < nombre_donnee; i++)
  • {
  • valeures[i] = transcode_16bits(donnee_recues.Substring(7 + 2 * i, 2));
  • //debug += "--" + donnee_recues.Substring(7 + 4 * i, 4);
  • }
  • Px_Entree entree_lues = new Px_Entree(valeures);
  • return entree_lues;
  • }
  • }
  • public class Dx_Entree //Constructeur des entrées analog Cubloc CB290
  • {
  • public int D0;
  • public int D1;
  • public int D2;
  • public int D3;
  • public int D4;
  • public int D5;
  • public int D6;
  • public int D7;
  • public int D8;
  • public int D9;
  • public int D10;
  • public int D11;
  • public int D12;
  • public int D13;
  • public int D14;
  • public int D15;
  • public Dx_Entree(int[] donnees)
  • {
  • this.D0 = donnees[0];
  • this.D1 = donnees[1];
  • this.D2 = donnees[2];
  • this.D3 = donnees[3];
  • this.D4 = donnees[4];
  • this.D5 = donnees[5];
  • this.D6 = donnees[6];
  • this.D7 = donnees[7];
  • this.D8 = donnees[8];
  • this.D9 = donnees[9];
  • this.D10 = donnees[10];
  • this.D11 = donnees[11];
  • this.D12 = donnees[12];
  • this.D13 = donnees[13];
  • this.D14 = donnees[14];
  • this.D15 = donnees[15];
  • }
  • }
  • public class Ana_Entree //Constructeur des registres Cubloc CB290
  • {
  • public int Ana0;
  • public int Ana1;
  • public int Ana2;
  • public int Ana3;
  • public int Ana4;
  • public int Ana5;
  • public int Ana6;
  • public int Ana7;
  • public Ana_Entree(int[] donnees)
  • {
  • this.Ana0 = donnees[0];
  • this.Ana1 = donnees[1];
  • this.Ana2 = donnees[2];
  • this.Ana3 = donnees[3];
  • this.Ana4 = donnees[4];
  • this.Ana5 = donnees[5];
  • this.Ana6 = donnees[6];
  • this.Ana7 = donnees[7];
  • }
  • }
  • public class Px_Entree //Constructeur des entrées TOR Cubloc CB290
  • {
  • public string Block7; //Entrées P56-P63 en décimal
  • public string P56;
  • public string P57;
  • public string P58;
  • public string P59;
  • public string P60;
  • public string P61;
  • public string P62;
  • public string P63;
  • public string Block8; //Entrées P64-P71 en décimal
  • public string P71;
  • public string P64;
  • public string P65;
  • public string P66;
  • public string P67;
  • public string P68;
  • public string P69;
  • public string P70;
  • public string Block9; //Entrées P72-P79 en décimal
  • public string P72;
  • public string P73;
  • public string P74;
  • public string P75;
  • public string P76;
  • public string P77;
  • public string P78;
  • public string P79;
  • //Block 10
  • public string Block10; //Entrées P80-P87 en décimal
  • public string P87;
  • public string P86;
  • public string P85;
  • public string P84;
  • public string P83;
  • public string P82;
  • public string P81;
  • public string P80;
  • //the default constructor
  • public Px_Entree(int[] entree_recues)
  • {
  • //Bloc 7
  • string block_7 = transcode_binaire(entree_recues[0]);
  • this.Block7 = block_7;
  • this.P63 = block_7.Substring(0, 1);
  • this.P62 = block_7.Substring(1, 1);
  • this.P61 = block_7.Substring(2, 1);
  • this.P60 = block_7.Substring(3, 1);
  • this.P59 = block_7.Substring(4, 1);
  • this.P58 = block_7.Substring(5, 1);
  • this.P57 = block_7.Substring(6, 1);
  • this.P56 = block_7.Substring(7, 1);
  • //Bloc 8
  • string block_8 = transcode_binaire(entree_recues[3]);
  • this.Block8 = block_8;
  • this.P71 = block_8.Substring(0, 1);
  • this.P70 = block_8.Substring(1, 1);
  • this.P69 = block_8.Substring(2, 1);
  • this.P68 = block_8.Substring(3, 1);
  • this.P67 = block_8.Substring(4, 1);
  • this.P66 = block_8.Substring(5, 1);
  • this.P65 = block_8.Substring(6, 1);
  • this.P64 = block_8.Substring(7, 1);
  • //Bloc 9
  • string block_9 = transcode_binaire(entree_recues[2]);
  • this.Block9 = block_9 ;
  • this.P79 = block_9.Substring(0, 1);
  • this.P78 = block_9.Substring(1, 1);
  • this.P77 = block_9.Substring(2, 1);
  • this.P76 = block_9.Substring(3, 1);
  • this.P75 = block_9.Substring(4, 1);
  • this.P74 = block_9.Substring(5, 1);
  • this.P73 = block_9.Substring(6, 1);
  • this.P72 = block_9.Substring(7, 1);
  • //Bloc 10
  • string block_10 = transcode_binaire(entree_recues[5]);
  • this.Block10 = block_10;
  • this.P87 = block_10.Substring(0,1);
  • this.P86 = block_10.Substring(1,1);
  • this.P85 = block_10.Substring(2,1);
  • this.P84 = block_10.Substring(3,1);
  • this.P83 = block_10.Substring(4,1);
  • this.P82 = block_10.Substring(5,1);
  • this.P81 = block_10.Substring(6,1);
  • this.P80 = block_10.Substring(7,1);
  • }
  • public string transcode_binaire(int chaine)
  • {
  • string transcode = Convert.ToString(chaine, 2);
  • int taille = transcode.Length;
  • string complement = "";
  • for(int i=0;i<8-taille;i++)
  • complement += "0";
  • return complement+ transcode;
  • }
  • }
  • }
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication2
{

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //Initialisation Communication RS232
            RS232_ModBus.init_RS232();
        }

        private void button1_Click_2(object sender, EventArgs e)
        {
            timer1.Enabled = true;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            /*Lecture des entrées P56 à P87
            textBox4.Text = lecture_Px().P80;//debug;
            Lecture de D0 à D15 + mise en forme
            Dx_Entree Dxx = new Dx_Entree (lecture_Dx(28672,16));
            textBox4.Text = Dxx.D1+"";*/
            
            /*string debug = "";
            int[]temps = new int[int.Parse(textBox5.Text)];
            //28772
            temps = RS232_ModBus.lecture_Dx(int.Parse(textBox1.Text),16);
            try
            {
                for (int i = 0; i < int.Parse(textBox5.Text); i++)
                    debug += "D(" + i + "):" + temps[i] + " ";
                textBox4.Text = debug;
            }
            catch
            {

                textBox4.Text = "erreure";
            }*/

            /*Ana_Entree Ana = new Ana_Entree (lecture_Ana());
            textBox4.Text = Ana.Ana1+"";*/

            /*textBox2.Text = (int.Parse(textBox2.Text)) + "";
            ecrire_un_mot(100, int.Parse(textBox2.Text));*/
        }

    }


    public class RS232_ModBus     //Noyau de communication ModBus Cubloc ASCII   
    {
        //Adresse de l'esclave ( Le cubloc CB290 )
        private static string adresse_esclave = "03";
        
        //Initialisation du tampon de trames RS232 ModBus ASCII
        private static string trame_rs232 = "";
        
        //Initialisation port serie (RS232 pour Modbus Cubloc)
        public static void init_RS232()
        {             
            try
            {
                Form1.serialPort1.BaudRate = 115200;
                Form1.serialPort1.DataBits = 8;
                Form1.serialPort1.Parity = System.IO.Ports.Parity.None;
                Form1.serialPort1.StopBits = System.IO.Ports.StopBits.One;
                Form1.serialPort1.PortName = "COM3";
                Form1.serialPort1.Open();
            }
            catch{} 
        }

        //Conversion d'une String en Byte Array
        private static byte[] StrToByteArray(string str)
        {
            System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
            return encoding.GetBytes(str);
        }

        //Ajoute un byte '\n' à la fin d'une chaine de Bytes
        private static byte[] Retour_chariot(string trame)
        {
            byte[] tramebyte = new byte[trame.Length];
            tramebyte = StrToByteArray(trame);
            byte[] tramebyte1 = new byte[tramebyte.Length + 1];
            for (int i = 0; i < tramebyte.Length; i++)
            { tramebyte1[i] = tramebyte[i]; }
            tramebyte1[tramebyte.Length] = (byte)'\n';
            return tramebyte1;
        }

        //Generation du LRC
        private static string LRC(String trame_test)
        {
            Char[] instructions = new Char[trame_test.Length / 2];
            for (Int32 i = 0; i < trame_test.Length; i += 2)
                instructions[i / 2] = (Char)Byte.Parse(trame_test.Substring(i, 2),
                    System.Globalization.NumberStyles.HexNumber);
            int LRC = 0;
            for (int i = 0; i < instructions.Length; i++)
                LRC += (int)instructions[i];
            LRC = 255 - LRC + 1;
            return LRC.ToString("x2");
        }

        //Transcodage tout string d'hexa en decimal
        private static int transcode_16bits(String trame_test)
        {
            return Convert.ToInt32(trame_test, 16);
        }

        //Transcode tout decimal en hexa XX XX
        private static string transcode_hexa(int nombre)
        {
            return nombre.ToString("x4");
        }

        //Transcode tout decimal en hexa XX
        private static string transcode_nbhexa(int nombre)
        {
            return nombre.ToString("x2");
        }

        //Generateur de trame de lecture _D() Cubloc CB290
        private static string lire_registre_D(int adresse_depart, int nombre_registres)
        {
            //Adresse de départ, Nombre de données à récupérer

            string trame = "";
            string code_fonction = "03"; //0x03 Lecture de registre maintenu
            string corps_trame = adresse_esclave + code_fonction + transcode_hexa(adresse_depart) + transcode_hexa(nombre_registres);
            trame = ":" + corps_trame + LRC(corps_trame);
            return trame.ToUpper();
        }

        //Lecture des registre Dxx
        public static int[] lecture_Dx(int position, int nombre)
        {
            //28672
            //Coder la trame de lecture des _D(x)
            trame_rs232 = lire_registre_D(position, nombre);

            //Emmetre la trame vers l'esclave
            Form1.serialPort1.Write(Retour_chariot(trame_rs232), 0, Retour_chariot(trame_rs232).Length);

            //Lire la trame de retour
            string donnee_recues = Form1.serialPort1.ReadLine();

            //Interpréter la trame reçue
            int esclave = transcode_16bits(donnee_recues.Substring(1, 2));
            int commande = transcode_16bits(donnee_recues.Substring(3, 2));
            int nombre_donnee = transcode_16bits(donnee_recues.Substring(5, 2)) / 2;
            int[] valeures = new int[nombre_donnee];
            if (donnee_recues.Substring(3, 2) == "03")
                for (int i = 0; i < nombre_donnee; i++)
                {
                    valeures[i] = transcode_16bits(donnee_recues.Substring(7 + 4 * i, 4));
                    //debug += "--" + donnee_recues.Substring(7 + 4 * i, 4);
                }
            return valeures;
        }

        //Lecture des registres Analog
        public static int[] lecture_Ana()
        {
            //28672
            //Coder la trame de lecture des _D(x)
            trame_rs232 = lire_registre_D(28672, 8);

            //Emmetre la trame vers l'esclave
            Form1.serialPort1.Write(Retour_chariot(trame_rs232), 0, Retour_chariot(trame_rs232).Length);

            //Lire la trame de retour
            string donnee_recues = Form1.serialPort1.ReadLine();

            //Interpréter la trame reçue
            int esclave = transcode_16bits(donnee_recues.Substring(1, 2));
            int commande = transcode_16bits(donnee_recues.Substring(3, 2));
            int nombre_donnee = transcode_16bits(donnee_recues.Substring(5, 2)) / 2;
            int[] valeures = new int[nombre_donnee];
            for (int i = 0; i < nombre_donnee; i++)
            {
                valeures[i] = transcode_16bits(donnee_recues.Substring(7 + 4 * i, 4));
                //debug += "--" + donnee_recues.Substring(7 + 4 * i, 4);
            }
            return valeures;
        }

        //Ecriture d'un bloc de registre Dxx
        public static string ecrire_registre_D(int adresse_depart, int nombre_registres, int[] data)
        {
            string code_fonction = "10";
            string corps_trame = adresse_esclave + code_fonction + transcode_hexa(adresse_depart) + transcode_hexa(nombre_registres) + transcode_nbhexa(nombre_registres * 2);

            for (int i = 0; i < data.Length; i++)
                corps_trame += transcode_hexa(data[i]);

            return ":" + corps_trame + LRC(corps_trame);
        }

        //Ecriture d'un seul mot Dxx
        public static void ecrire_un_mot(int position_D, int valeure)
        {
            //D100 : 28772
            //Coder la trame de lecture des _D(x)
            int[] donnees = new int[1];
            donnees[0] = valeure;
            trame_rs232 = ecrire_registre_D(28672 + position_D, 1, donnees).ToUpper();

            //Emmetre la trame vers l'esclave
            Form1.serialPort1.Write(Retour_chariot(trame_rs232), 0, Retour_chariot(trame_rs232).Length);

            //Lire la trame de retour
            Form1.serialPort1.ReadLine();
        }

        //Lecture de l'etat des entrées de la proto-board
        public static Px_Entree lecture_Px()
        {
            //P56 à P87
            int position = 50;
            int nombre = 3;

            //Coder la trame de lecture des _D(x)
            trame_rs232 = lire_registre_D(position, nombre);

            //Emmetre la trame vers l'esclave
            Form1.serialPort1.Write(Retour_chariot(trame_rs232), 0, Retour_chariot(trame_rs232).Length);

            //Lire la trame de retour
            string donnee_recues = Form1.serialPort1.ReadLine();

            //Interpréter la trame reçue
            int esclave = transcode_16bits(donnee_recues.Substring(1, 2));
            int commande = transcode_16bits(donnee_recues.Substring(3, 2));
            int nombre_donnee = transcode_16bits(donnee_recues.Substring(5, 2));
            int[] valeures = new int[nombre_donnee];
            for (int i = 0; i < nombre_donnee; i++)
            {
                valeures[i] = transcode_16bits(donnee_recues.Substring(7 + 2 * i, 2));
                //debug += "--" + donnee_recues.Substring(7 + 4 * i, 4);
            }
            Px_Entree entree_lues = new Px_Entree(valeures);
            return entree_lues;
        }
    }  

    public class Dx_Entree        //Constructeur des entrées analog Cubloc CB290 
    {
        public int D0;
        public int D1;
        public int D2;
        public int D3;
        public int D4;
        public int D5;
        public int D6;
        public int D7;
        public int D8;
        public int D9;
        public int D10;
        public int D11;
        public int D12;
        public int D13;
        public int D14;
        public int D15;

        public Dx_Entree(int[] donnees)
        {
            this.D0 = donnees[0];
            this.D1 = donnees[1];
            this.D2 = donnees[2];
            this.D3 = donnees[3];
            this.D4 = donnees[4];
            this.D5 = donnees[5];
            this.D6 = donnees[6];
            this.D7 = donnees[7];
            this.D8 = donnees[8];
            this.D9 = donnees[9];
            this.D10 = donnees[10];
            this.D11 = donnees[11];
            this.D12 = donnees[12];
            this.D13 = donnees[13];
            this.D14 = donnees[14];
            this.D15 = donnees[15];
        }
    }

    public class Ana_Entree       //Constructeur des registres Cubloc CB290      
    {
        public int Ana0;
        public int Ana1;
        public int Ana2;
        public int Ana3;
        public int Ana4;
        public int Ana5;
        public int Ana6;
        public int Ana7;

        public Ana_Entree(int[] donnees)
        {
            this.Ana0 = donnees[0];
            this.Ana1 = donnees[1];
            this.Ana2 = donnees[2];
            this.Ana3 = donnees[3];
            this.Ana4 = donnees[4];
            this.Ana5 = donnees[5];
            this.Ana6 = donnees[6];
            this.Ana7 = donnees[7];
        }
    }

    public class Px_Entree        //Constructeur des entrées TOR Cubloc CB290    
    {
        public string Block7;  //Entrées P56-P63 en décimal
        public string P56;
        public string P57;
        public string P58;
        public string P59;
        public string P60;
        public string P61;
        public string P62;
        public string P63;

        public string Block8;  //Entrées P64-P71 en décimal
        public string P71;
        public string P64;
        public string P65;
        public string P66;
        public string P67;
        public string P68;
        public string P69;
        public string P70;

        public string Block9;  //Entrées P72-P79 en décimal
        public string P72;
        public string P73;
        public string P74;
        public string P75;
        public string P76;
        public string P77;
        public string P78;
        public string P79;

        //Block 10
        public string Block10; //Entrées P80-P87 en décimal
        public string P87;
        public string P86;
        public string P85;
        public string P84;
        public string P83;
        public string P82;
        public string P81;
        public string P80;


        //the default constructor
        public Px_Entree(int[] entree_recues)
        {
            //Bloc 7
            string block_7 = transcode_binaire(entree_recues[0]);
            this.Block7 = block_7;
            this.P63 = block_7.Substring(0, 1);
            this.P62 = block_7.Substring(1, 1);
            this.P61 = block_7.Substring(2, 1);
            this.P60 = block_7.Substring(3, 1);
            this.P59 = block_7.Substring(4, 1);
            this.P58 = block_7.Substring(5, 1);
            this.P57 = block_7.Substring(6, 1);
            this.P56 = block_7.Substring(7, 1);


            //Bloc 8
            string block_8 = transcode_binaire(entree_recues[3]);
            this.Block8 = block_8;
            this.P71 = block_8.Substring(0, 1);
            this.P70 = block_8.Substring(1, 1);
            this.P69 = block_8.Substring(2, 1);
            this.P68 = block_8.Substring(3, 1);
            this.P67 = block_8.Substring(4, 1);
            this.P66 = block_8.Substring(5, 1);
            this.P65 = block_8.Substring(6, 1);
            this.P64 = block_8.Substring(7, 1);

            
            //Bloc 9
            string block_9 = transcode_binaire(entree_recues[2]);
            this.Block9 = block_9 ;
            this.P79 = block_9.Substring(0, 1);
            this.P78 = block_9.Substring(1, 1);
            this.P77 = block_9.Substring(2, 1);
            this.P76 = block_9.Substring(3, 1);
            this.P75 = block_9.Substring(4, 1);
            this.P74 = block_9.Substring(5, 1);
            this.P73 = block_9.Substring(6, 1);
            this.P72 = block_9.Substring(7, 1);
            
            //Bloc 10
            string block_10 = transcode_binaire(entree_recues[5]);
            this.Block10 = block_10;
            this.P87 = block_10.Substring(0,1);
            this.P86 = block_10.Substring(1,1);
            this.P85 = block_10.Substring(2,1);
            this.P84 = block_10.Substring(3,1);
            this.P83 = block_10.Substring(4,1);
            this.P82 = block_10.Substring(5,1);
            this.P81 = block_10.Substring(6,1);
            this.P80 = block_10.Substring(7,1);
        }
        public string transcode_binaire(int chaine)
        {
            string transcode = Convert.ToString(chaine, 2);
            int taille = transcode.Length;
            string complement = "";
            for(int i=0;i<8-taille;i++)
                complement += "0";
            return complement+ transcode;
        }
    }     

}


 Conclusion

La vie est belle !


 Sources du même auteur

Source avec Zip Source avec une capture Source .NET (Dotnet) CREATION DE GRAPHIQUES ( COMPATIBLE WINCE6 )
Source avec Zip Source avec une capture Source .NET (Dotnet) PID PROPORTIONNEL INTEGRAL DERIVE
Source avec Zip Source avec une capture Source .NET (Dotnet) CALCUL DE LRC POUR TRAMES MODBUS ASCII

 Sources de la même categorie

Source avec Zip Source avec une capture Source .NET (Dotnet) HOOK CLAVIER EN C# par bmastar
Source avec Zip Source avec une capture Source .NET (Dotnet) HOOK CLAVIER EN C# par shadow1779
Source avec Zip Source .NET (Dotnet) HOOK CLAVIER par damned3
Source avec Zip Source avec une capture Source .NET (Dotnet) LIBRAIRIE RAWINPUT .NET par Lutinore
Source avec Zip Source avec une capture Source .NET (Dotnet) POST-TRAITEMENT NMEA/GPS: FILTRAGE DES POINTS, EXPORT KML/GP... par jray

 Sources en rapport avec celle ci

Source avec Zip Source avec une capture Source .NET (Dotnet) SERVEUR/ESCLAVE MODBUS TCP/IP par SteveFuchsIT
Source avec Zip Source avec une capture Source .NET (Dotnet) ASCII ART MAKER - CONVERSION IMAGE - TEXTE (ASCII ART) par MaxSoldier
Source avec Zip Source avec une capture Source .NET (Dotnet) LOGICIEL DE RÉGULATION PID EN C# - VS 2008 EXPRESS par brunovan
Source avec Zip Source avec une capture Source .NET (Dotnet) CREATION DE GRAPHIQUES ( COMPATIBLE WINCE6 ) par x2bf3
Source avec Zip Source avec une capture Source .NET (Dotnet) CALCUL DE LRC POUR TRAMES MODBUS ASCII par x2bf3

Commentaires et avis

Commentaire de sebmafate le 17/10/2007 11:28:45 administrateur CS

C'est inutilisable dans l'état... et en plus c'est très mal codé !

 Ajouter un commentaire


Discussions en rapport avec ce code source dans le forum

La Fonction ASCII et Chr() [ par DrChal ] DrChalSalut,Je cherche le moyen de remplacer une chaîne de caractère par un retour chariot.En Vb, il y avait chr(10)+chr(13) mais maintenant en C#, c' code ascii [ par alcamatt ] comment peut on reccupérer le code ascii d'un caractere avec csharp ?En visual basic, je crois me rappeler qu'il fallait utiliser la fonction Asc(&lt; conversion string en int, byte, hexa [ par vinc524 ] C#bonjour, je cherche désespérémment un moyen pour convertir une chaine de caractere string en hexadecimal. En fait je voudrais le code ascii d'un car int (ASCII) to char (ou string) [ par JuS ] Bonjour amis développeurs cshapriens,Je cherche à récupérer le caractère correspondant à un code ASCII.Comment faire ???//code ASCII du "A"int code = Web Service+rs232 [ par fryounet ] Bonjour ,Voici mon problèm : j'aimerais accéder à périphérique qui est connecté par port rs232 ,par un Web service.tout d'abord j'aimerais savoir si c communication serie rs232 avec le visual c# .net [ par mm_abdelmadjid ] Bonjour, Je suis entrein de réaliser un logiciel qui permet l'acquisition de données en serie via un bus de terrain avec le port serie du com1,2 du WinCE + RS232 [ par nrv22 ] Salut,Je voulais savoir s'il existe des Dll ou bien des classes en C# pour envoyer ou recevoir des données sur le port RS232? Si oui, est ce que l'on webrequest unicode en ascii [ par ultima_93 ] Bonjour,Je vous explique mon probleme, j'envoi une url grace a webrequest , à un service qui est en c++.lorsque j'envoi mon url , ell peut comporter probleme de code ascii sous linux [ par kamalh07 ] Je suis sous linux et avec la console je n'arrive pas a afficher les caractere speciaux comme le è,é...j'ai essayer ceci sans resultat:#include "stdio Caractere ASCII [ par RM50Man ] POuvez vous me donner une explicaton concrètes pour transformer un chiffre en caractere et la valeur ascii d'une lettre!!Donnez moi un exemple simple


Nos sponsors


Sondage...

Comparez les prix

CalendriCode

Février 2010
LMMJVSD
1234567
891011121314
15161718192021
22232425262728

Consulter la suite du CalendriCode

 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 0,702 sec (3)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales