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 :

Problème de communication


Sujet :

C#

  1. #1
    Membre du Club
    Femme Profil pro
    Étudiant
    Inscrit en
    Mars 2015
    Messages
    131
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 33
    Localisation : France, Sarthe (Pays de la Loire)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mars 2015
    Messages : 131
    Points : 46
    Points
    46
    Par défaut Problème de communication
    bonjour,
    j'ai une question, j'ai mis en oeuvre une interface de communication en c# qui permet d'envoyer et recevoir des données entre mon pc et une commande d'axe via un port série.
    et la avant d'envoyer ou de recevoir les données avec les bouton envoyer/recevoir, je dois d’abord positionne la MCS32Ex sur le menu de transfert (F1>F1>F6) sur le dialog 80 (la c'est fait manuellement) ensuit le transfert doit commencer.
    car mon code il attend pas dés que je clique sur le bouton envoyer il envoie directe sur le port.

    comment on peux coder ça ?????

    voila le code que j'ai fait :

    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
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
     
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.IO.Ports;
    using System.IO;
    using System.Threading;
     
    namespace Communicatio_1
    {
        public partial class Form1 : Form
        {
            public byte[] bBufferBytes; //buffer a envoyer
            public delegate void myDelegate(byte sData);
            //public event SerialDataReceivedEventHandler DataReceived;
            public enum LogMsgType { Incoming, Outgoing, Normal, Warning, Error };
            public bool RtsEnable ;//{ get; set; };
            public bool DtrEnable ;//{ get; set; }
            //private bool s;
     
     
     
            public Form1()
            {
                InitializeComponent();
                this.Refresh();
     
            }
     
     
            private void Form1_Load(object sender, EventArgs e) //l'événement Form_Load nous lire tous les ports série disponibles et nous assigner les valeurs à la liste déroulante.
            {
                //on commence par lister les voies séries présentes
                String[] ports = SerialPort.GetPortNames(); //fonction statique
                //string [] ports = System.IO.Ports.SerialPort.GetPortNames ();
                for (int i = 0; i < ports.Length; i++) //on ajoute les ports au combo box
                {
                    portBox.Items.Add(ports[i]);
     
                }
     
            }
     
     
            private void button_charger_Click(object sender, EventArgs e)
            {
                openFileDialog1.Title = "Chargement";// Titre
                openFileDialog1.FilterIndex = 1;
     
                if (openFileDialog1.ShowDialog(this) == DialogResult.OK) // Ouverture boite de dialogue OpenFile
                {
                    textBox1.Text = string.Empty; // On vide le TextBox
                    // backgroundWorker1.RunWorkerAsync();// executer le Background worker doWork()
                    // timer1.Start();
     
                    StreamReader sr = new StreamReader(openFileDialog1.OpenFile(), Encoding.Default); // Ouverture du fichier sélectionné
                    // son nom est dans openFileDialog1.FileName
     
                    try
                    {
                        string data = sr.ReadLine();
     
                        // bBufferBytes = System.Text.Encoding.ASCII.GetBytes(data); //Place le contenu dans un buffer d'octets (ASCII: 1 char = 1 octet) (UNICODE: 1 Char = 2 octets)
                        while (data != null)
                        {
                            bBufferBytes = System.Text.Encoding.ASCII.GetBytes(data); //Place le contenu dans un buffer d'octets (ASCII: 1 char = 1 octet) (UNICODE: 1 Char = 2 octets)
     
                            textBox1.AppendText(data + "\r\n");
     
                            data = sr.ReadLine();
     
                            //  serialPort1.Write(bBufferBytes, 0, bBufferBytes.Length); // Ecrit le buffer complet sur le SerialPort
                            //  StatusLabel1.Text = "Transfert en cours...";
                        }
     
                    }
     
                    finally
                    {
                        if (sr != null)
                            sr.Close();
                        // StatusLabel1.Text = "Transfert Fini ";
                    }
     
                }
            }
     
            private void button_connecter_Click(object sender, EventArgs e)
            {
                serialPort1.PortName = portBox.SelectedItem.ToString();
                serialPort1.BaudRate = 9600;
                serialPort1.Parity = Parity.None;
                serialPort1.StopBits = StopBits.One;
                serialPort1.DataBits = 8;
                serialPort1.Handshake = Handshake.None;
                serialPort1.PortName = portBox.Text;
                RtsEnable = false;
                DtrEnable = false;
     
     
                // serialPort1.Open();
                serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);
                serialPort1.PinChanged += new SerialPinChangedEventHandler(serialPort1_PinChanged);
                try
                {
                    serialPort1.Open();  //Ouvrez le SerialPort
                    bool blnCTS = serialPort1.CtsHolding;// lire CTS
                    bool blnDSR = serialPort1.DsrHolding;// lire DSR
                    bool blnCD = serialPort1.CDHolding;// lire CD
                    StatusLabel1.Text = "serialPort1 : " + portBox.Text + " Opened";//Mettre à jour le StatusStrip
                    MessageBox.Show("Opened");
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Failed: " + ex.Message);
                }
     
                button_connecter.Enabled = false;
                button_deconnecter.Enabled = true;
                textBox1.ReadOnly = false;
                textBox2.ReadOnly = false;
                radioButton.Enabled = true;
                radioButton.BackColor = Color.Black;
     
            }
     
            private void button_deconnecter_Click(object sender, EventArgs e)
            {
                try
                {
                    serialPort1.Close();  //fermer le SerialPort
                    StatusLabel1.Text = "serialPort1 : " + portBox.Text + " Closed"; //Mettre à jour le StatusStrip
                    MessageBox.Show("Closed");
     
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Failed: " + ex.Message);
                }
     
                button_connecter.Enabled = true; // activer le button connecter
                button_deconnecter.Enabled = false;// desctiver le bitton deconnecter
                textBox1.ReadOnly = true;
                textBox2.ReadOnly = true;
                radioButton.BackColor = Color.White;
            }
     
            private void button_recevoir_Click(object sender, EventArgs e)
            {
     
                StatusLabel1.Text = "Appuyez sur 'F6' de la commande numérique puis patientez...";
                textBox2.SelectedText = string.Empty;
                //serialPort1.PinChanged += new SerialPinChangedEventHandler(serialPort1_PinChanged);
                //textBox2.AppendText(DataReceived.ToString());
            }
     
     
            private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
            {
                //string vide
                string value = "";
                //tant qu'il y a des données entrée on continue
                while (serialPort1.BytesToRead > 0)
                {
                    // on crée un char de 255 caractere
                    char[] buffer = new char[255];
                    //lecture du buffer jusqu'a sa longeur buffer.Length
                    int longueur_bytes_read = serialPort1.Read(buffer, 0, buffer.Length);
                    //tes characteres sont contenus dans buffer.
                    //on remplit notre string value byte par byte par notre tableau de charactere buffer, le += fait que l'on écrase pas les données qui sont inserées, mais on les ajoutes.
                    for (int i = 0; i < longueur_bytes_read; i++)
                    {
                        value += buffer[i];
                    }
                }
                //la methode toString passe tes bytes en characteres lisibles.
                safeInput(value.ToString());
            }
     
            private void safeInput(string msg)
            {
                MethodInvoker method = delegate
                             {
                                 // on efface la textbox
                                 textBox2.SelectedText = string.Empty;
                                 // on met les données dedans en string (conversion  char->string)
                                 textBox2.AppendText(msg.ToString());
                             };
                if (InvokeRequired)
                    BeginInvoke(method);
                else
                    method.Invoke();
     
            }
     
            private void button_envoyer_Click(object sender, EventArgs e)
            {
                StatusLabel1.Text = "Appuyez sur 'F6' de la commande numérique puis patientez...";
     
                StreamReader sr = new StreamReader(openFileDialog1.OpenFile(), Encoding.Default); 
                try
                {
                    string data = sr.ReadLine();
     
                    while (data != null)
                    {
                        bBufferBytes = System.Text.Encoding.ASCII.GetBytes(data); //Place le contenu dans un buffer d'octets (ASCII: 1 char = 1 octet) (UNICODE: 1 Char = 2 octets)
                        serialPort1.Write(bBufferBytes, 0, bBufferBytes.Length); // Ecrit le buffer complet sur le SerialPort
                        backgroundWorker1.RunWorkerAsync();// executer le Background worker doWork()
                        data = sr.ReadLine();
     
                      // StatusLabel1.Text = "Transfert en cours...";
     
                    }
     
                }
     
                finally
                {
                    if (sr != null)
                        sr.Close();
                    StatusLabel1.Text = "Transfert Fini ";
                }
     
            }
     
            private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
            {
                for (int i = 0; i <= 100; i++ )
                {
     
                    backgroundWorker1.ReportProgress(i);
                    System.Threading.Thread.Sleep(100);
                }
            }
     
            private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
            {
                progressBar2.Value = e.ProgressPercentage;
                StatusLabel1.Text = e.ProgressPercentage.ToString()+ "%" + "Télechargement en court...";
     
            }
     
            private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
            {
                StatusLabel1.Text = ("Télechargement Fini");
            }
     
            private void serialPort1_PinChanged(object sender, SerialPinChangedEventArgs e)
            {
                // Si les trois signaux d'entrée changent au-dessus dudit cet événement PinChanged sera trriggered
     
                bool b;
     
                switch (e.EventType)
                {
     
                    case System.IO.Ports.SerialPinChange.CDChanged:
     
                        b = serialPort1.CDHolding;
     
                        break;
     
                    case System.IO.Ports.SerialPinChange.CtsChanged:
     
                        b = serialPort1.CtsHolding;
     
                        break;
     
                    case System.IO.Ports.SerialPinChange.DsrChanged:
     
                        b = serialPort1.DsrHolding;
     
                        break;
     
                    default:
     
                        break;
     
                }
            }
     
            private void button_sauvegarder_Click(object sender, EventArgs e)
            {
     
        } 
     
     
     
            }
     
        }

  2. #2
    Expert confirmé

    Homme Profil pro
    Chef de projet NTIC
    Inscrit en
    Septembre 2006
    Messages
    3 580
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Chef de projet NTIC
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Septembre 2006
    Messages : 3 580
    Points : 5 195
    Points
    5 195
    Par défaut
    et pourquoi ne pas demander directement au constructeur (SERAD) ?
    The Monz, Toulouse
    Expertise dans la logistique et le développement pour
    plateforme .Net (Windows, Windows CE, Android)

  3. #3
    Membre du Club
    Femme Profil pro
    Étudiant
    Inscrit en
    Mars 2015
    Messages
    131
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 33
    Localisation : France, Sarthe (Pays de la Loire)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mars 2015
    Messages : 131
    Points : 46
    Points
    46
    Par défaut
    salut,
    j'ai demander il m'ont répondu Pour l'utilisation avec la MCS il est impératif que le signal RTS (contrôle de flux) du PC reste désactivé, sinon elle ne recevra rien.
    la j'ai met en oeuvre le pinChanged du Data received mais il se passe rien.

  4. #4
    Expert confirmé

    Homme Profil pro
    Chef de projet NTIC
    Inscrit en
    Septembre 2006
    Messages
    3 580
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Chef de projet NTIC
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Septembre 2006
    Messages : 3 580
    Points : 5 195
    Points
    5 195
    Par défaut
    je ne vois pas dans ton code de :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    serialPort1.RtsEnable = false;
    The Monz, Toulouse
    Expertise dans la logistique et le développement pour
    plateforme .Net (Windows, Windows CE, Android)

  5. #5
    Membre régulier
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Février 2011
    Messages
    72
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : Industrie

    Informations forums :
    Inscription : Février 2011
    Messages : 72
    Points : 107
    Points
    107
    Par défaut
    Bonjour,
    Pour que le RtsEnable soit pris en compte, il faut definir le Handshake XonXoff.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
                serialPort1.Handshake = Handshake.XOnXOff;

  6. #6
    Membre du Club
    Femme Profil pro
    Étudiant
    Inscrit en
    Mars 2015
    Messages
    131
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 33
    Localisation : France, Sarthe (Pays de la Loire)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mars 2015
    Messages : 131
    Points : 46
    Points
    46
    Par défaut
    Bonjour,
    je vous remerciez pour l'aide.
    j'ai met en oeuvre les deux réponse mais si toujours la même chose.
    je ne comprend pas

    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
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
     
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.IO.Ports;
    using System.IO;
    using System.Threading;
     
    namespace Communicatio_1
    {
        public partial class Form1 : Form
        {
            public byte[] bBufferBytes; //buffer a envoyer
            public delegate void myDelegate(byte sData);
            //public event SerialDataReceivedEventHandler DataReceived;
            public enum LogMsgType { Incoming, Outgoing, Normal, Warning, Error };
            public bool RtsEnable ;//{ get; set; };
            public bool DtrEnable ;//{ get; set; }
            //private bool s;
     
     
     
            public Form1()
            {
                InitializeComponent();
                this.Refresh();
     
            }
     
     
            private void Form1_Load(object sender, EventArgs e) //l'événement Form_Load nous lire tous les ports série disponibles et nous assigner les valeurs à la liste déroulante.
            {
                //on commence par lister les voies séries présentes
                String[] ports = SerialPort.GetPortNames(); //fonction statique
                //string [] ports = System.IO.Ports.SerialPort.GetPortNames ();
                for (int i = 0; i < ports.Length; i++) //on ajoute les ports au combo box
                {
                    portBox.Items.Add(ports[i]);
     
                }
     
            }
     
     
            private void button_charger_Click(object sender, EventArgs e)
            {
                openFileDialog1.Title = "Chargement";// Titre
                openFileDialog1.FilterIndex = 1;
     
                if (openFileDialog1.ShowDialog(this) == DialogResult.OK) // Ouverture boite de dialogue OpenFile
                {
                    textBox1.Text = string.Empty; // On vide le TextBox
                    // backgroundWorker1.RunWorkerAsync();// executer le Background worker doWork()
                    // timer1.Start();
     
                    StreamReader sr = new StreamReader(openFileDialog1.OpenFile(), Encoding.Default); // Ouverture du fichier sélectionné
                    // son nom est dans openFileDialog1.FileName
     
                    try
                    {
                        string data = sr.ReadLine();
     
                        // bBufferBytes = System.Text.Encoding.ASCII.GetBytes(data); //Place le contenu dans un buffer d'octets (ASCII: 1 char = 1 octet) (UNICODE: 1 Char = 2 octets)
                        while (data != null)
                        {
                            bBufferBytes = System.Text.Encoding.ASCII.GetBytes(data); //Place le contenu dans un buffer d'octets (ASCII: 1 char = 1 octet) (UNICODE: 1 Char = 2 octets)
     
                            textBox1.AppendText(data + "\r\n");
     
                            data = sr.ReadLine();
     
                            //  serialPort1.Write(bBufferBytes, 0, bBufferBytes.Length); // Ecrit le buffer complet sur le SerialPort
                            //  StatusLabel1.Text = "Transfert en cours...";
                        }
     
                    }
     
                    finally
                    {
                        if (sr != null)
                            sr.Close();
                        // StatusLabel1.Text = "Transfert Fini ";
                    }
     
                }
            }
     
            private void button_connecter_Click(object sender, EventArgs e)
            {
                serialPort1.PortName = portBox.SelectedItem.ToString();
                serialPort1.BaudRate = 9600;
                serialPort1.Parity = Parity.None;
                serialPort1.StopBits = StopBits.One;
                serialPort1.DataBits = 8;
                serialPort1.Handshake = Handshake.XOnXOff;
                serialPort1.PortName = portBox.Text;
               serialPort1.RtsEnable = false;
                serialPort1.DtrEnable = false;
     
     
                // serialPort1.Open();
                serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);
                serialPort1.PinChanged += new SerialPinChangedEventHandler(serialPort1_PinChanged);
                try
                {
                    serialPort1.Open();  //Ouvrez le SerialPort
                    bool blnCTS = serialPort1.CtsHolding;// lire CTS
                    bool blnDSR = serialPort1.DsrHolding;// lire DSR
                    bool blnCD = serialPort1.CDHolding;// lire CD
                    StatusLabel1.Text = "serialPort1 : " + portBox.Text + " Opened";//Mettre à jour le StatusStrip
                    MessageBox.Show("Opened");
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Failed: " + ex.Message);
                }
     
                button_connecter.Enabled = false;
                button_deconnecter.Enabled = true;
                textBox1.ReadOnly = false;
                textBox2.ReadOnly = false;
                radioButton.Enabled = true;
                radioButton.BackColor = Color.Black;
     
            }
     
            private void button_deconnecter_Click(object sender, EventArgs e)
            {
                try
                {
                    serialPort1.Close();  //fermer le SerialPort
                    StatusLabel1.Text = "serialPort1 : " + portBox.Text + " Closed"; //Mettre à jour le StatusStrip
                    MessageBox.Show("Closed");
     
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Failed: " + ex.Message);
                }
     
                button_connecter.Enabled = true; // activer le button connecter
                button_deconnecter.Enabled = false;// desctiver le bitton deconnecter
                textBox1.ReadOnly = true;
                textBox2.ReadOnly = true;
                radioButton.BackColor = Color.White;
            }
     
            private void button_recevoir_Click(object sender, EventArgs e)
            {
     
                StatusLabel1.Text = "Appuyez sur 'F6' de la commande numérique puis patientez...";
                textBox2.SelectedText = string.Empty;
                //serialPort1.PinChanged += new SerialPinChangedEventHandler(serialPort1_PinChanged);
                //textBox2.AppendText(DataReceived.ToString());
            }
     
     
            private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
            {
                //string vide
                string value = "";
                //tant qu'il y a des données entrée on continue
                while (serialPort1.BytesToRead > 0)
                {
                    // on crée un char de 255 caractere
                    char[] buffer = new char[255];
                    //lecture du buffer jusqu'a sa longeur buffer.Length
                    int longueur_bytes_read = serialPort1.Read(buffer, 0, buffer.Length);
                    //tes characteres sont contenus dans buffer.
                    //on remplit notre string value byte par byte par notre tableau de charactere buffer, le += fait que l'on écrase pas les données qui sont inserées, mais on les ajoutes.
                    for (int i = 0; i < longueur_bytes_read; i++)
                    {
                        value += buffer[i];
                    }
                }
                //la methode toString passe tes bytes en characteres lisibles.
                safeInput(value.ToString());
            }
     
            private void safeInput(string msg)
            {
                MethodInvoker method = delegate
                             {
                                 // on efface la textbox
                                 textBox2.SelectedText = string.Empty;
                                 // on met les données dedans en string (conversion  char->string)
                                 textBox2.AppendText(msg.ToString() + "\r\n");
                             };
                if (InvokeRequired)
                    BeginInvoke(method);
                else
                    method.Invoke();
     
            }
     
     
            private void button_envoyer_Click(object sender, EventArgs e)
            {
                StatusLabel1.Text = "Appuyez sur 'F6' de la commande numérique puis patientez...";
               // backgroundWorker1.RunWorkerAsync();// executer le Background worker doWork()
                progressBar2.Visible = true;
                progressBar2.Minimum = 1;
                progressBar2.Maximum = bBufferBytes.Length;
                progressBar2.Value = 1;
                progressBar2.Step = 1;
     
                StreamReader sr = new StreamReader(openFileDialog1.OpenFile(), Encoding.Default); 
     
                try
                {
                    string data = sr.ReadLine();
     
                    while (data != null)
                    {
                        bBufferBytes = System.Text.Encoding.ASCII.GetBytes(data); //Place le contenu dans un buffer d'octets (ASCII: 1 char = 1 octet) (UNICODE: 1 Char = 2 octets)
                        serialPort1.Write(bBufferBytes, 0, bBufferBytes.Length); // Ecrit le buffer complet sur le SerialPort
     
                        data = sr.ReadLine();
     
                      // StatusLabel1.Text = "Transfert en cours...";
     
                    }
                   /* for (int x = 1; x <= data.Length; x++)
                    {
                        // Copy the file and increment the ProgressBar if successful.
     
                        if (serialPort1.Write((data[x - 1]) == true))
                       {
                            // Perform the increment on the ProgressBar.
                            progressBar2.PerformStep();
                        }
                    }*/
     
                }
     
                finally
                {
                    if (sr != null)
                        sr.Close();
                    StatusLabel1.Text = "Transfert Fini ";
                }
     
            }
     
            private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
            {
                for (int i = 0; i <= 100; i++ )
                {
     
                    backgroundWorker1.ReportProgress(i);
                    System.Threading.Thread.Sleep(100);
                }
     
     
            }
     
            private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
            {
                progressBar2.Value = e.ProgressPercentage;
                StatusLabel1.Text = e.ProgressPercentage.ToString()+ "%" + "Télechargement en court...";
     
            }
     
            private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
            {
                StatusLabel1.Text = ("Télechargement Fini");
            }
     
            private void serialPort1_PinChanged(object sender, SerialPinChangedEventArgs e)
            {
                // Si les trois signaux d'entrée changent au-dessus dudit cet événement PinChanged sera trriggered
     
                bool b;
     
                switch (e.EventType)
                {
     
                    case System.IO.Ports.SerialPinChange.CDChanged:
     
                        b = serialPort1.CDHolding;
     
                        break;
     
                    case System.IO.Ports.SerialPinChange.CtsChanged:
     
                        b = serialPort1.CtsHolding;
     
                        break;
     
                    case System.IO.Ports.SerialPinChange.DsrChanged:
     
                        b = serialPort1.DsrHolding;
     
                        break;
     
                    default:
     
                        break;
     
                }
            }
     
     
        } 
     
     
     
     
     
     
     
            }
     
        }

  7. #7
    Expert confirmé

    Homme Profil pro
    Chef de projet NTIC
    Inscrit en
    Septembre 2006
    Messages
    3 580
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Chef de projet NTIC
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Septembre 2006
    Messages : 3 580
    Points : 5 195
    Points
    5 195
    Par défaut
    Sans avoir le matériel, impossible d'aller plus loin pour moi.

    Et je suppose que tu n'es pas à Toulouse ? si ?
    The Monz, Toulouse
    Expertise dans la logistique et le développement pour
    plateforme .Net (Windows, Windows CE, Android)

  8. #8
    Membre du Club
    Femme Profil pro
    Étudiant
    Inscrit en
    Mars 2015
    Messages
    131
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 33
    Localisation : France, Sarthe (Pays de la Loire)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mars 2015
    Messages : 131
    Points : 46
    Points
    46
    Par défaut
    Bonjour,
    non je suis pas de Toulouse.
    je travaille avec MCS23EX SERAD http://www.serad.fr/Commande-d-axes-MCS32ex.htm qui est relie a une boite Dialog 80 .

  9. #9
    Membre du Club
    Femme Profil pro
    Étudiant
    Inscrit en
    Mars 2015
    Messages
    131
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 33
    Localisation : France, Sarthe (Pays de la Loire)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mars 2015
    Messages : 131
    Points : 46
    Points
    46
    Par défaut
    j'ai trouver ce bout de code mais si en C++

    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
     
     //*** RTS / CTS
                    if (bRTS_CTS==TRUE)
                    {
                                   ControlBlock.fRtsControl = RTS_CONTROL_HANDSHAKE;
                                   ControlBlock.fOutxCtsFlow = TRUE;
                    }
                    else
                    {
                                   ControlBlock.fRtsControl = RTS_CONTROL_ENABLE;
                                   ControlBlock.fOutxCtsFlow = FALSE;
                    }
     
     
                    //*** DSR/DTS
                    ControlBlock.fDtrControl = DTR_CONTROL_ENABLE;
                    ControlBlock.fOutxDsrFlow = FALSE;
     
                    //*** XON / XOFF Off
                    ControlBlock.fOutX = FALSE;
                    ControlBlock.fInX = FALSE;
    tu peux m'aider à le convertir en c# peut être ça va marcher ?

  10. #10
    Expert confirmé

    Homme Profil pro
    Chef de projet NTIC
    Inscrit en
    Septembre 2006
    Messages
    3 580
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Chef de projet NTIC
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Septembre 2006
    Messages : 3 580
    Points : 5 195
    Points
    5 195
    Par défaut
    je ne sais pas ce qu'est l'object controlBlock.

    Désolé, mais sur de la com Série, sans un équipement équivalent (surtout dans le cadre d'un appareil spécifique comme le MCS), il m'est impossible d'aider
    d'avantage...
    The Monz, Toulouse
    Expertise dans la logistique et le développement pour
    plateforme .Net (Windows, Windows CE, Android)

  11. #11
    Membre régulier
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Février 2011
    Messages
    72
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : Industrie

    Informations forums :
    Inscription : Février 2011
    Messages : 72
    Points : 107
    Points
    107
    Par défaut
    https://msdn.microsoft.com/en-us/lib...=vs.85%29.aspx
    ce site pourra peut être d'aider à faire la relation.
    je rejoins theMonz31 sans matériel c'est difficile de t'aider!
    Juste un conseil, assure toi que tout est dans le bon protocole avant tout.

  12. #12
    Membre du Club
    Femme Profil pro
    Étudiant
    Inscrit en
    Mars 2015
    Messages
    131
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 33
    Localisation : France, Sarthe (Pays de la Loire)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mars 2015
    Messages : 131
    Points : 46
    Points
    46
    Par défaut
    salut,
    je vous remercie bien pour votre aide, si vrai que sans le matériel devant sois c'est un peux difficile

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Problème de communication par sockets
    Par Linio dans le forum Entrée/Sortie
    Réponses: 33
    Dernier message: 06/05/2006, 18h50
  2. problème de communication
    Par yassinchaouki dans le forum Général JavaScript
    Réponses: 10
    Dernier message: 20/04/2006, 10h46
  3. Problème de communication client/serveur
    Par alex6891 dans le forum Développement
    Réponses: 10
    Dernier message: 09/03/2006, 13h12
  4. Problème de communications entre threads
    Par maya09 dans le forum Windows
    Réponses: 1
    Dernier message: 22/02/2006, 22h18
  5. [WinService][C#] Problème de communication
    Par Pfeffer dans le forum Windows Forms
    Réponses: 2
    Dernier message: 08/07/2005, 15h54

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