IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C# Discussion :

découper un message reçu par port COM


Sujet :

C#

  1. #1
    Membre du Club
    Profil pro
    Inscrit en
    Mai 2007
    Messages
    118
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Mai 2007
    Messages : 118
    Points : 45
    Points
    45
    Par défaut découper un message reçu par port COM
    Bonjour,

    dans le cadre d'un projet je dois recevoir 6 bytes via le port com. Ces 6 bytes sont tout à fait indépendant les un des autres.
    Le but final est de séparer ces 6 bytes pour les affichez chacun dans une textbox particulière :
    byte0 -> textbox1
    byte1 -> textbox2
    ...
    byte5 -> textbox6

    Je reçois convenablement l'ensemble des bytes dans une seule textbox
    Je voudrais les séparer pour les afficher chacun dans la textbox appropriée, comment pourrais-je faire?

    Merci de votre aide

  2. #2
    Membre habitué Avatar de lasrevinu
    Inscrit en
    Février 2010
    Messages
    215
    Détails du profil
    Informations forums :
    Inscription : Février 2010
    Messages : 215
    Points : 134
    Points
    134
    Par défaut
    Salut,
    je me suis remis à faire du port serie,
    et je voulais avoir une précision,
    est-ce que tu interroge ton materiel ou pas ?
    (tu fait du serialPort.write ?)

  3. #3
    Membre habitué Avatar de lasrevinu
    Inscrit en
    Février 2010
    Messages
    215
    Détails du profil
    Informations forums :
    Inscription : Février 2010
    Messages : 215
    Points : 134
    Points
    134
    Par défaut
    La raison de ma question est simple,
    en communication serie, il y a deux approche,

    l'approche "Terminal", tu n'interroge pas ton materiel, tu recoit des infos tu peux en envoyer, mais il n'en fera rien (exactement comme le terminal windows par exemple)

    l'approche "client-serveur", ou "question reponse",
    tu interroge ton materiel et il te repond.

    la programmation differe dans les deux cas,
    pour l'approche terminal, il faut un dataReceived event qui se declenchera des qu'il y a un message en entrée.

    pour l'approche question reponse,
    la classe serial port comporte des methode super efficace,
    le readExisting, le readTo, le ReadChar etc..
    et pas besoin de dataReceived event handler

    il faut avoir ca en tete quoi..

    voila pour l'info.

  4. #4
    Membre du Club
    Profil pro
    Inscrit en
    Mai 2007
    Messages
    118
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Mai 2007
    Messages : 118
    Points : 45
    Points
    45
    Par défaut
    Salut,

    je vais réfléchir à ce problème

    J'ai une autre question rien à voir avec ce post.
    Quand je reçois des données du port série, je reçois par exemple ?, *, ...

    Comment obtenir la valeur décimale 63, 42, ...

    voici le code
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
     
            private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
            {
                string value = "";
                while (serialPort1.BytesToRead > 0)
                {
                    char[] buffer = new char[255];
                    int bytes_read = serialPort1.Read(buffer, 0, buffer.Length);
     
                    for (int i = 0; i < bytes_read; i++)
                    {
                        value += buffer[i];
                    }
                }
                safeInput(value.ToString());
            }
     
     
     
                    textBox1.Invoke(new EventHandler(delegate
                    {
     
     
                        //textBox1.SelectedText = string.Empty;
                        this.textBox1.Text = "";
                        textBox1.AppendText(String.Format(msg));
                        textBox1.ScrollToCaret();
                        //textBox1.Text = msg[0].ToString();
     
                        //textBox2.SelectedText = string.Empty;
                        this.textBox2.Text = "";
                        textBox2.AppendText(msg.ToString());
                        textBox2.ScrollToCaret();
                        //textBox2.Text = msg[1].ToString();
     
                        //textBox3.SelectedText = string.Empty;
                        this.textBox3.Text = "";
                        textBox3.AppendText(msg.ToString());
                        textBox3.ScrollToCaret();
                        //textBox3.Text = msg[2].ToString();
     
                        //textBox4.SelectedText = string.Empty;
                        this.textBox4.Text = "";
                        textBox4.AppendText(msg.ToString());
                        textBox4.ScrollToCaret();
                        //textBox4.Text = msg[3].ToString();
     
                    }));
            }
    merci bcp

  5. #5
    Membre habitué Avatar de lasrevinu
    Inscrit en
    Février 2010
    Messages
    215
    Détails du profil
    Informations forums :
    Inscription : Février 2010
    Messages : 215
    Points : 134
    Points
    134
    Par défaut
    utilises-tu des seriealPort.write?
    en resumé c'etait ca ma question

    pour les charactere qui te sont renvoyé
    ca doit etre un probleme de formatage,

    essaie
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    serialPort.encoding = encoding.ASCII;
    si tu sait pas de quel encodage il s'agit, essaie un peu tout.

    voila

  6. #6
    Membre du Club
    Profil pro
    Inscrit en
    Mai 2007
    Messages
    118
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Mai 2007
    Messages : 118
    Points : 45
    Points
    45
    Par défaut
    Salut,

    non, j'utilise pas serialport.write
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
     
      private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
            {
                string value = "";
                while (serialPort1.BytesToRead > 0)
                {
                    char[] buffer = new char[6];
                    int bytes_read = serialPort1.Read(buffer, 0, buffer.Length);
     
     
                    for (int i = 0; i < bytes_read; i++)
                    {
                        value += buffer[i];
                    }
                }
                safeInput(value.ToString());
            }
     
     
            private void safeInput(string msg)
            {
     
                    textBox1.Invoke(new EventHandler(delegate
                    {
     
     
                        //textBox1.SelectedText = string.Empty;
                        this.textBox1.Text = "";
                        textBox1.AppendText(String.Format(msg));
                        //textBox1.Text = msg[0].ToString();
     
                        //textBox2.SelectedText = string.Empty;
                        this.textBox2.Text = "";
                        textBox2.AppendText(msg.ToString());
                        //textBox2.Text = msg[1].ToString();
     
                        //textBox3.SelectedText = string.Empty;
                        this.textBox3.Text = "";
                        textBox3.AppendText(msg.ToString());
                        //textBox3.Text = msg[2].ToString();
     
                        //textBox4.SelectedText = string.Empty;
                        this.textBox4.Text = "";
                        textBox4.AppendText(msg.ToString());
                        //textBox4.Text = msg[3].ToString();
     
                    }));
            }
    Je ne fait que recevoir via le port COM pas d'envois de données.

    j'ai testé ton "encoding..." mais je reçois le code ascii de la donnée, pas la valeur decimale.
    J'ai testé avec
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    textBox4.AppendText(convert.ToInt32(msg));
    mais ça va pas Textbox.text demande un string ^^

    bon je débute dons je test plein de choses...
    Mais là, je sais pas faire.

    D'ailleur pour séparer les données, je n'y suis tjs pas arrivé. Je veux dire que je reçois tjs l'ensemble des données dans une seule textbox. Je ne les distribuent pas...

    j'ai testé comme suit :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
     
     private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
            {
                string value = "";
                while (serialPort1.BytesToRead > 0)
                {
                    char[] buffer = new char[255];
                    int bytes_read = serialPort1.Read(buffer, 0, buffer.Length);
     
     
                    for (int i = 0; i < bytes_read; i++)
                    {
                        value += buffer[i];
                    }
                }
                safeInput(value.ToString());
            }
     
     
            private void safeInput(string msg)
            {
     
                    textBox1.Invoke(new EventHandler(delegate
                    {
     
     
                        //textBox1.SelectedText = string.Empty;
                        this.textBox1.Text = "";
                        //textBox1.AppendText(String.Format(msg));
                        textBox1.Text = msg[0].ToString();
     
                        //textBox2.SelectedText = string.Empty;
                        this.textBox2.Text = "";
                        //textBox2.AppendText(msg.ToString());
                        textBox2.Text = msg[1].ToString();
     
                        //textBox3.SelectedText = string.Empty;
                        this.textBox3.Text = "";
                        //textBox3.AppendText(msg.ToString());
                        textBox3.Text = msg[2].ToString();
     
                        //textBox4.SelectedText = string.Empty;
                        this.textBox4.Text = "";
                        //textBox4.AppendText(msg.ToString());
                        textBox4.Text = msg[3].ToString();
     
                    }));
            }
    Le programme compile, mais le programme s'arrête en mettant :
    L'index se trouve en dehors du tableau.
    Je suis près du but je suppose...j'en suis certain, mais je bloque ^^

    Merci de votre aide

  7. #7
    Membre habitué Avatar de lasrevinu
    Inscrit en
    Février 2010
    Messages
    215
    Détails du profil
    Informations forums :
    Inscription : Février 2010
    Messages : 215
    Points : 134
    Points
    134
    Par défaut
    quel est la longeur de ce que tu recois?
    si par exemple je recois un mot de 18 charactere je fait comme ceci:

    je crée une methode qui admet comme parametre un string
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    public string[] CoordParser(string msg)
            {
                //je Parse (decoupe) 18 charactere
                string X= msg.Substring(0, 6);
                string Y= msg.Substring(6, 6);
                string Z= msg.Substring(12, 6);
     
                //Je met les 3 strings dans un tableau 
                string[] XYZ = new string[3];
                XYZ[0] = X;
                XYZ[1] = Y;
                XYZ[2] = Z;
     
                return XYZ;
            }
    pour recuperer ton premier message tu ecris XYZ[0] ..
    voila

  8. #8
    Membre du Club
    Profil pro
    Inscrit en
    Mai 2007
    Messages
    118
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Mai 2007
    Messages : 118
    Points : 45
    Points
    45
    Par défaut
    Bonsoir,

    je m'envois 4 bytes suivants :
    1
    2
    3
    4

    j'essaye de les séparer et de le mettre chacun dans une textbox.

    Juste une petite remarque, un mot est un ensemble de 2 bytes (en électronique) mais c'est un détail (c'est juste pour dire que je t'apprend qq chose, toi qui m'a tant appris )

    J'ai répondu a ta question, je vais essayer ta méthode. Mais si tu veux, tu peux m'aider ;-)

  9. #9
    Membre du Club
    Profil pro
    Inscrit en
    Mai 2007
    Messages
    118
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Mai 2007
    Messages : 118
    Points : 45
    Points
    45
    Par défaut
    Salut,

    j'ai testé ta solution et ça compilais.
    J'ai lancé le programme(en m'envoyant la chaine 1234 toutes les secondes).

    Les 1, 2, 3 et 4 se sont bien positionner puis le programme s'est arrêté avec le message d'erreur

    L'index et la longueur doivent faire référence à un emplacement situé dans la chaîne.
    Voici le code utilisé

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
     
            private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
            {
                string value = "";
                while (serialPort1.BytesToRead > 0)
                {
                    char[] buffer = new char[6];
                    int bytes_read = serialPort1.Read(buffer, 0, buffer.Length);
     
     
                    for (int i = 0; i < bytes_read; i++)
                    {
                        value += buffer[i];
                    }
                }
                safeInput(value.ToString());
            }
     
     
            private void safeInput(string msg)
            {
     
                    textBox1.Invoke(new EventHandler(delegate
                    {
     
     
                        ////textBox1.SelectedText = string.Empty;
                        //this.textBox1.Text = "";
                        ////textBox1.AppendText(String.Format(msg));
                        //textBox1.Text = msg[0].ToString();
     
                        ////textBox2.SelectedText = string.Empty;
                        //this.textBox2.Text = "";
                        //textBox2.AppendText(msg.ToString());
                        ////textBox2.Text = msg[1].ToString();
     
                        ////textBox3.SelectedText = string.Empty;
                        //this.textBox3.Text = "";
                        //textBox3.AppendText(msg.ToString());
                        ////textBox3.Text = msg[2].ToString();
     
                        ////textBox4.SelectedText = string.Empty;
                        //this.textBox4.Text = "";
                        ////textBox4.AppendText(msg.ToString());
                        //textBox4.Text = msg[3].ToString();
     
                        string W = msg.Substring(0, 1);
                        string X = msg.Substring(1, 1);
                        string Y = msg.Substring(2, 1);
                        string Z = msg.Substring(3, 1);
     
                        string[] WXYZ = new string[4];
                        WXYZ[0] = W;
                        WXYZ[1] = X;
                        WXYZ[2] = Y;
                        WXYZ[3] = Z;
                        textBox1.Text = WXYZ[0];
                        textBox2.Text = WXYZ[1];
                        textBox3.Text = WXYZ[2];
                        textBox4.Text = WXYZ[3];
     
                    }));
            }
    Merci de ton aide, on touche presque le but!

  10. #10
    Membre habitué Avatar de lasrevinu
    Inscrit en
    Février 2010
    Messages
    215
    Détails du profil
    Informations forums :
    Inscription : Février 2010
    Messages : 215
    Points : 134
    Points
    134
    Par défaut
    C'est normal qu'il t'affiche le message d'erreur
    ton message sera en fait: 123412341234 etc...
    ton code prends en considération uniquement les premiers 1234

    sache que pour libérer le buffer de lecture,
    une méthode serialPort.DiscardInBuffer() existe...
    a toi de voir si tu en as besoin
    salut

  11. #11
    Membre du Club
    Profil pro
    Inscrit en
    Mai 2007
    Messages
    118
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Mai 2007
    Messages : 118
    Points : 45
    Points
    45
    Par défaut
    salut,

    j'ai testé, en regardant l'emplacement ou mettre cette méthode et en regardant la msdn.

    voici mon code
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
     
            private void button1_Click(object sender, EventArgs e)
            {
                if (comboBox1.Visible == true)
                {
                    MessageBox.Show("Veuillez choisir le port COM spécifique et appuyer sur OK pour continuer!", "Mauvaise procédure", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                }
                else
                {
     
                    if (buttonConnectDeconnect.Text == "Connecté")
                    {
                        buttonConnectDeconnect.Text = "Déconnecté";
                        label11.ForeColor = Color.Red;
                        buttonConnectDeconnect.BackColor = Color.Red;
     
                        if (serialPort1.IsOpen)
                        {
                            serialPort1.Close();
                            label11.Text = "Port " + comboBox1.Text + " Fermé";
                        }
     
                    }
                    else
                    {
                        buttonConnectDeconnect.Text = "Connecté";
                        label11.ForeColor = Color.Green;
                        buttonConnectDeconnect.BackColor = Color.Green;
     
                        //Serial source
                        serialPort1 = new SerialPort((string)comboBox1.SelectedItem, 9600, Parity.None, 8, StopBits.One);
                        //serialPort1 = new SerialPort("COM4", 9600, Parity.None, 8, StopBits.One);
                        serialPort1.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
                      //  serialPort1.DtrEnable = true;
                      //  serialPort1.RtsEnable = true;
                        serialPort1.ReadTimeout = 500;
     
                        //Ouverture du port
                        if (!serialPort1.IsOpen)
                        {
                            serialPort1.Open();
                        }
                    }
                }
            }
     
            private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
            {
                string value = "";
                while (serialPort1.BytesToRead > 0)
                {
                    char[] buffer = new char[255];
                    int bytes_read = serialPort1.Read(buffer, 0, buffer.Length);
     
     
                    for (int i = 0; i < bytes_read; i++)
                    {
                        value += buffer[i];
                    }
                }
                safeInput(value.ToString());
     
            }
     
     
            private void safeInput(string msg)
            {
     
                    textBox1.Invoke(new EventHandler(delegate
                    {
     
     
                        ////textBox1.SelectedText = string.Empty;
                        //this.textBox1.Text = "";
                        ////textBox1.AppendText(String.Format(msg));
                        //textBox1.Text = msg[0].ToString();
     
                        ////textBox2.SelectedText = string.Empty;
                        //this.textBox2.Text = "";
                        //textBox2.AppendText(msg.ToString());
                        ////textBox2.Text = msg[1].ToString();
     
                        ////textBox3.SelectedText = string.Empty;
                        //this.textBox3.Text = "";
                        //textBox3.AppendText(msg.ToString());
                        ////textBox3.Text = msg[2].ToString();
     
                        ////textBox4.SelectedText = string.Empty;
                        //this.textBox4.Text = "";
                        ////textBox4.AppendText(msg.ToString());
                        //textBox4.Text = msg[3].ToString();
     
                        string W = msg.Substring(0, 1);
                        string X = msg.Substring(1, 1);
                        string Y = msg.Substring(2, 1);
                        string Z = msg.Substring(3, 1);
     
                        //serialPort1.DiscardOutBuffer();
     
                        //reset le buffer de réception
                        serialPort1.DiscardInBuffer();
     
                        string[] WXYZ = new string[3];
                        WXYZ[0] = W;
                        WXYZ[1] = X;
                        WXYZ[2] = Y;
                        WXYZ[3] = Z;
                        textBox1.Text = WXYZ[0];
                        textBox2.Text = WXYZ[1];
                        textBox3.Text = WXYZ[2];
                        textBox4.Text = WXYZ[3];
     
                    }));
            }
    je ne sais pas pq il di tjs que l'indexe est en dehors des limites du tableau!!!!

    pourtant c'est là qu'il doit être être (je pense)...je me trompe peut-être

  12. #12
    Membre habitué Avatar de lasrevinu
    Inscrit en
    Février 2010
    Messages
    215
    Détails du profil
    Informations forums :
    Inscription : Février 2010
    Messages : 215
    Points : 134
    Points
    134
    Par défaut
    Je vois que tu n'as pas lu mon dernier post...
    ok
    essaye la chose suivante pour bien comprendre le fonctionnement de cette histoire,
    envoie toi uniquement 4characteres "1234" par exemple,
    et là, il sera content!
    apres,
    ...
    fait ca deja

    (lorsqu'un message est posté ca serai sympa de le lire )
    salut

  13. #13
    Membre du Club
    Profil pro
    Inscrit en
    Mai 2007
    Messages
    118
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Mai 2007
    Messages : 118
    Points : 45
    Points
    45
    Par défaut
    voila, il ne râle pas... et tout s'affiche comme il se doit!
    c'est donc lors du 5ème byte reçu que le problème intervient...

    là il faut vider le buffer de réception je suppose

  14. #14
    Membre habitué Avatar de lasrevinu
    Inscrit en
    Février 2010
    Messages
    215
    Détails du profil
    Informations forums :
    Inscription : Février 2010
    Messages : 215
    Points : 134
    Points
    134
    Par défaut
    Hé hé.. il rale pas, et c tant mieux
    oui, tu peux vider le buffer
    essaye avec serialPort.DiscardInBuffer() apres la lecture de chaque bloc,
    n'oublie pas de vider tes textBox.
    salut

  15. #15
    Membre du Club
    Profil pro
    Inscrit en
    Mai 2007
    Messages
    118
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Mai 2007
    Messages : 118
    Points : 45
    Points
    45
    Par défaut
    Alors les 4 premier bytes reçus sont ok nickel, parfait
    les suivants ça va pas.

    je ne sais pas où ça coince!

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
     
         private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
            {
                string value = "";
                while (serialPort1.BytesToRead > 0)
                {
                    char[] buffer = new char[255];
     
                        int bytes_read = serialPort1.Read(buffer, 0, buffer.Length);
     
                        for (int i = 0; i < bytes_read; i++)
                        {
                            value += buffer[i];
                        }
     
                        safeInput(value.ToString());
                }
     
             }
     
     
            private void safeInput(string msg)
            {
     
                    textBox1.Invoke(new EventHandler(delegate
                    {
     
     
                        ////textBox1.SelectedText = string.Empty;
                        //this.textBox1.Text = "";
                        ////textBox1.AppendText(String.Format(msg));
                        //textBox1.Text = msg[0].ToString();
     
                        ////textBox2.SelectedText = string.Empty;
                        //this.textBox2.Text = "";
                        //textBox2.AppendText(msg.ToString());
                        ////textBox2.Text = msg[1].ToString();
     
                        ////textBox3.SelectedText = string.Empty;
                        //this.textBox3.Text = "";
                        //textBox3.AppendText(msg.ToString());
                        ////textBox3.Text = msg[2].ToString();
     
                        ////textBox4.SelectedText = string.Empty;
                        //this.textBox4.Text = "";
                        ////textBox4.AppendText(msg.ToString());
                        //textBox4.Text = msg[3].ToString();
     
                        //serialPort1.DiscardInBuffer();
     
                        // on vide les textboxs
                         textBox1.SelectedText = string.Empty;
                         this.textBox1.Text = "";
                         textBox2.SelectedText = string.Empty;
                         this.textBox2.Text = "";
                         textBox3.SelectedText = string.Empty;
                         this.textBox3.Text = "";
                         textBox4.SelectedText = string.Empty;
                         this.textBox4.Text = "";
     
                        // séparation du message en 4 morceaux
                        string W = msg.Substring(0, 1);
                        string X = msg.Substring(1, 1);
                        string Y = msg.Substring(2, 1);
                        string Z = msg.Substring(3, 1);
     
                        serialPort1.DiscardInBuffer();
     
                        // mise des morceaux du message dans un tableau
                        string[] tableau_valeur = new string[4];
                        tableau_valeur[0] = W;
                        tableau_valeur[1] = X;
                        tableau_valeur[2] = Y;
                        tableau_valeur[3] = Z;
     
     
                        // mise en textbox approprié des valeur inscrite dans le tableau!
                        textBox1.Text = tableau_valeur[0];
                        textBox2.Text = tableau_valeur[1];
                        textBox3.Text = tableau_valeur[2];
                        textBox4.Text = tableau_valeur[3];
     
                    }));
            }
    je ne sais pas où mettre le "vidage du buffer"...c'est ça le truc
    sisi, je lis tes posts, promis ;-)

  16. #16
    Membre habitué Avatar de lasrevinu
    Inscrit en
    Février 2010
    Messages
    215
    Détails du profil
    Informations forums :
    Inscription : Février 2010
    Messages : 215
    Points : 134
    Points
    134
    Par défaut
    essaye voir avec un petit thread.sleep(500)
    juste apres le vidage du buffer.

  17. #17
    Membre du Club
    Profil pro
    Inscrit en
    Mai 2007
    Messages
    118
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Mai 2007
    Messages : 118
    Points : 45
    Points
    45
    Par défaut
    salut,

    j'ai testé, mais ce ne sont que les 4 premiers bytes qui se mettent à la bonne place,
    le reste n'est pas bon...le programme s'arrête et le message est :
    L'index et la longueur doivent faire référence à un emplacement situé dans la chaîne.
    Est-ce que le vidage de buffer est à la bonne place?

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
     
            private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
            {
                string value = "";
                while (serialPort1.BytesToRead > 0)
                {
                    char[] buffer = new char[255];
     
                        int bytes_read = serialPort1.Read(buffer, 0, buffer.Length);
     
                        for (int i = 0; i < bytes_read; i++)
                        {
                            value += buffer[i];
                        }
     
     
                        safeInput(value.ToString());
     
                }
     
             }
     
     
            private void safeInput(string msg)
            {
     
                    textBox1.Invoke(new EventHandler(delegate
                    {
     
     
                        ////textBox1.SelectedText = string.Empty;
                        //this.textBox1.Text = "";
                        ////textBox1.AppendText(String.Format(msg));
                        //textBox1.Text = msg[0].ToString();
     
                        ////textBox2.SelectedText = string.Empty;
                        //this.textBox2.Text = "";
                        //textBox2.AppendText(msg.ToString());
                        ////textBox2.Text = msg[1].ToString();
     
                        ////textBox3.SelectedText = string.Empty;
                        //this.textBox3.Text = "";
                        //textBox3.AppendText(msg.ToString());
                        ////textBox3.Text = msg[2].ToString();
     
                        ////textBox4.SelectedText = string.Empty;
                        //this.textBox4.Text = "";
                        ////textBox4.AppendText(msg.ToString());
                        //textBox4.Text = msg[3].ToString();
     
                        //serialPort1.DiscardInBuffer();
     
                        // séparation du message en 4 morceaux
                         textBox1.SelectedText = string.Empty;
                         this.textBox1.Text = "";
                         textBox2.SelectedText = string.Empty;
                         this.textBox2.Text = "";
                         textBox3.SelectedText = string.Empty;
                         this.textBox3.Text = "";
                         textBox4.SelectedText = string.Empty;
                         this.textBox4.Text = "";
     
                        string W = msg.Substring(0, 1);
                        string X = msg.Substring(1, 1);
                        string Y = msg.Substring(2, 1);
                        string Z = msg.Substring(3, 1);
     
                        //Thread.Sleep(500);
                        serialPort1.DiscardInBuffer();
                        Thread.Sleep(500);
     
                        // mise des morceaux du message dans un tableau
                        string[] tableau_valeur = new string[4];
                        tableau_valeur[0] = W;
                        tableau_valeur[1] = X;
                        tableau_valeur[2] = Y;
                        tableau_valeur[3] = Z;
     
                        // mise en textbox approprié des valeur inscrite dans le tableau!
                        textBox1.Text = tableau_valeur[0];
                        textBox2.Text = tableau_valeur[1];
                        textBox3.Text = tableau_valeur[2];
                        textBox4.Text = tableau_valeur[3];
     
                    }));
            }
    Merci

  18. #18
    Membre habitué Avatar de lasrevinu
    Inscrit en
    Février 2010
    Messages
    215
    Détails du profil
    Informations forums :
    Inscription : Février 2010
    Messages : 215
    Points : 134
    Points
    134
    Par défaut
    tu envoie tes données à quelle frequence ?

  19. #19
    Membre du Club
    Profil pro
    Inscrit en
    Mai 2007
    Messages
    118
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Mai 2007
    Messages : 118
    Points : 45
    Points
    45
    Par défaut
    je les envois à du 9600 bauds...

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
     serialPort1 = new SerialPort((string)comboBox1.SelectedItem, 9600, Parity.None, 8, StopBits.One);

  20. #20
    Membre habitué Avatar de lasrevinu
    Inscrit en
    Février 2010
    Messages
    215
    Détails du profil
    Informations forums :
    Inscription : Février 2010
    Messages : 215
    Points : 134
    Points
    134
    Par défaut
    Non je ne demandais pas ca,
    tu as dit que tu envoyais tes données toutes les secondes?
    c bien ca ?
    explicite comment tu envoie ses données
    est-ce que c'est pour le test que tu t'envoie des données ou c ta machine qui le fait?

Discussions similaires

  1. (Labview) acquisition de données par port COM d'un ordi
    Par howtosayhello dans le forum LabVIEW
    Réponses: 0
    Dernier message: 01/09/2010, 23h52
  2. Problème envoi caractère par port COM
    Par Briceba dans le forum Windows Forms
    Réponses: 1
    Dernier message: 08/09/2008, 09h29
  3. [VB6]Code Manchester par port com
    Par alamaison62 dans le forum VB 6 et antérieur
    Réponses: 1
    Dernier message: 02/02/2006, 09h21

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo