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 :

communication pc arduino


Sujet :

C#

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Homme Profil pro
    Enseignant
    Inscrit en
    Mars 2016
    Messages
    33
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Enseignant
    Secteur : Enseignement

    Informations forums :
    Inscription : Mars 2016
    Messages : 33
    Par défaut communication pc arduino
    Bonjour,
    je cherche à piloter 4 servomoteurS depuis un win form avec 4 track bar
    j'ai donc l'architecture suivante pc---usb---arduino----4 servomoteurs

    Pour piloter un servomoteur pas de problème voila le code arduino

    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
     
    #include <Servo.h>
     
    Servo articulationa;
     
    int val=0;
     
     
    void setup() {
     Serial.begin(9600);
     articulationa.attach(3);
     
     }
     
    void loop() {
     
     
          }
     
    void serialEvent() {
       val=Serial.parseInt();
        articulationa.write(val);
          }

    et le code c# (à l'écran deux boutons (connecté) et (déconnecté) permettant d'ouvrir le port de communication , une checkbox permettant de sélectionner la trackbar et la trackbar permettant de déplacer le servomoteur)

    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
     
    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;
     
    namespace commandearticulaire
    {
        public partial class Form1 : Form
        {
            SerialPort port = new SerialPort();
            public string data;
            public Form1()
            {
                InitializeComponent();
                        }
     
                    private void label1_Click(object sender, EventArgs e)  { }
     
            private void trackBar1_Scroll(object sender, EventArgs e)
            {
                if (port.IsOpen & checkBox1.Checked)
                {                      
                    port.WriteLine(trackBar1.Value.ToString());
                    valarti2.Text = "angleepaule2=" + trackBar1.Value.ToString(); } }
     
            private void Form3_FormClosing(object sender, FormClosingEventArgs e)
            {            {
                    if (port.IsOpen) port.Close();
                }        }
     
            private void Form1_Load(object sender, EventArgs e)
            {        }
     
            private void connecté_Click(object sender, EventArgs e)
            {   port.PortName = "COM4";
                port.BaudRate = 9600;
                port.Open();
                if (port.IsOpen)
                {
                    connecté.Enabled = false;
                    déconnecté.Enabled = true;   }      }
     
            private void déconnecté_Click(object sender, EventArgs e)
            {
                port.Close();
                if (!port.IsOpen)
                {   connecté.Enabled = true;
                    déconnecté.Enabled = false;  }    }
     
     
     
            private void checkBox1_Click(object sender, EventArgs e)
            { }
     
            }
    Le problème c'est que je ne sais pas comment faire pour ensuite transformer ce programme de manière à piloter deux servomoteurs :
    le problème comment sélectionner le servomoteur à piloter?

  2. #2
    Membre averti
    Homme Profil pro
    Enseignant
    Inscrit en
    Mars 2016
    Messages
    33
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Enseignant
    Secteur : Enseignement

    Informations forums :
    Inscription : Mars 2016
    Messages : 33
    Par défaut
    Bon j'ai essayé d'associer l'évènement trackbar scroll à un protocole d'envoi (1erbit : début message 2ème bit envoie, 3eme bit patte de l'arduino, 4eme bit valeur de déplacement servomoteur)

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
     
    private void trackBar1_Scroll(object sender, EventArgs e)
            {
                if (port.IsOpen & checkBox1.Checked)
                {       
    int a = trackBar1.Value;
     byte[] buffer = new byte[4];
    		buffer[0] = Convert.ToByte(16);
    		buffer[1] = Convert.ToByte(127);
    		buffer[2] = Convert.ToByte(3);
    		buffer[3] = Convert.ToByte(a);
     
                    port.Write(buff,0,5);
                    valarti2.Text = "angleepaule2=" + trackBar1.Value.ToString(); } }
    associé à ce programme arduino
    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
    #include <Servo.h>
    Servo articulationa;
    Servo articulationb;
     
    //Setup message bytes
     
    byte inputByte_0;
     
    byte inputByte_1;
     
    byte inputByte_2;
     
    byte inputByte_3;
     
     
    //Setup
     
    void setup() {
     articulationa.attach(3);
    articulationa.attach(4);
      Serial.begin(9600);
    }
     
    //Main Loop
     
    void loop() {
     
      //Read Buffer
      if (Serial.available() == 5) 
      {
        //Read buffer
        inputByte_0 = Serial.read();
        delay(100);    
        inputByte_1 = Serial.read();
        delay(100);      
        inputByte_2 = Serial.read();
        delay(100);      
        inputByte_3 = Serial.read();
        delay(100);
     
      }
      //Check for start of Message
      if(inputByte_0 == 16)
      {       
           //Detect Command type
           switch (inputByte_1) 
           {
              case 127:
                 //Set PIN and value
                 switch (inputByte_2)
                {
                  case 3:
                   articulationa.write(inputByte_3);                  // sets the servo position according to the scaled value 
                     delay(10);
                      break;
               case 4:
                     articulationa.write(inputByte_3);                  // sets the servo position according to the scaled value 
                   delay(10);
                         break;
                    } 
                break;
     
            } 
            //Clear Message bytes
            inputByte_0 = 0;
            inputByte_1 = 0;
            inputByte_2 = 0;
            inputByte_3 = 0;
            inputByte_4 = 0;
     
      }
     
    }
    mais rien ne marche

  3. #3
    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
    Par défaut
    bonjour

    c'est pourtant ce qu'il faut faire :

    d'un coté, tu envoies une trame via le port série (par exemple, toujours 5 octets) et coté Arduino, tu devrais plutôt lire 5 octets
    comme ça, tu es sur d'avoir ta trame de commande

    une fois que tu as récupéré les 5 octets coté Arduino, c'est juste à toi de savoir la signification de ces 5 octets (ou plus, ou moins suivant le contexte).

    Rien de bien compliqué

    tu peux testé en remplissant un buffer de 5 octets contenant les instructions et quand le programme arduino saura correctement géré le buffer de commande,
    tu pourras basculer sur le transfert coté PC ==> Arduino.

    La première chose a t'assurer quand on teste de la COM série est que les données envoyées sont bien reçues.. une fois celà testé, tu pourras avancer !

  4. #4
    Membre averti
    Homme Profil pro
    Enseignant
    Inscrit en
    Mars 2016
    Messages
    33
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Enseignant
    Secteur : Enseignement

    Informations forums :
    Inscription : Mars 2016
    Messages : 33
    Par défaut
    Je te remercie de ta remarque
    Sur le principe nous sommes d'accord il faut mettre au point un protocole
    Cependant je pense que le dysfonctionnement provient des types de variables échangées j'ai essayé ça:
    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
     
    #include <Servo.h>
    char receivedChars[4];
    Servo articulationa;
    boolean newData = false;
     
    void setup() {
        Serial.begin(9600);
      }
     
    void loop() {
        recvWithStartEndMarkers();
        moveservo();
    }
     
    void recvWithStartEndMarkers() {
        static boolean recvInProgress = false;
        static byte ndx = 0;
        char startMarker = '<';
        char endMarker = '>';
        char rc;
     
     // if (Serial.available() > 0) {
        while (Serial.available() > 0 && newData == false) {
            rc = Serial.read();
     
            if (recvInProgress == true) {
                if (rc != endMarker) {
                    receivedChars[ndx] = rc;
                    ndx++;
                    if (ndx >= 4) {
                        ndx =4 - 1;
                    }
                }
                else {
                    receivedChars[ndx] = '\0'; // terminate the string
                    recvInProgress = false;
                    ndx = 0;
                    newData = true;
                }
            }
     
            else if (rc == startMarker) {
                recvInProgress = true;
            }
        }
    }
     
    void moveservo() {int angle;
        if (newData == true) {
          if (receivedChars[1]==('3'))
          {
          int angle = receivedChars[2] - 48;
     
     
     
          articulationa.write(angle);}
     
            newData = false;
        }
    }
    associé au c# 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
     
     
    private void trackBar1_Scroll(object sender, EventArgs e)
            {
                if (port.IsOpen & checkBox1.Checked)
                {       
    int a = trackBar1.Value;
    char[] value = trackBar1.Value.ToString().ToCharArray();
     byte[] buffer = new byte[4];
    		buffer[0] = '<';
    		buffer[1] = '3';
    		buffer[2] = value[0];
    		buffer[3] = '>';
     
                    port.Write(buff,0,4);
                    valarti2.Text = "angleepaule2=" + trackBar1.Value.ToString(); } }
    mais ça ne marche toujours pas,
    je tourne en rond

  5. #5
    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
    Par défaut
    déjà première chose pour nous aider dans ton aide

    Est-ce que tu reçois des choses coté Arduino ?

    si tu ne reçois rien, peut-être un "flush" coté SerialPort du PC pourrait aider ?

  6. #6
    Membre averti
    Homme Profil pro
    Enseignant
    Inscrit en
    Mars 2016
    Messages
    33
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Enseignant
    Secteur : Enseignement

    Informations forums :
    Inscription : Mars 2016
    Messages : 33
    Par défaut
    Merci, de votre aide
    mais en changeant de stratégie cad en utilisant uniquement ce qui marchiat dans mon prmier script j'y suis arrivé
    le code n'est sans doute pas hyper performant mais ca marche
    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
     
    #include <Servo.h>
    Servo articulationa;
    Servo articulationb;
    Servo articulationc;
    Servo articulationd;
    Servo choix = articulationa;
    boolean cmd = false;
    int val = 0;
     
    void setup() {
      articulationa.attach(3);
      articulationb.attach(4);
      articulationc.attach(5);
      articulationd.attach(6);
      Serial.begin(9600);
    }
     
     
     
    void loop() {
      while (Serial.available()) {
          val=Serial.parseInt();
          if (Serial.read() == '\n') {
          if(cmd==false) {
            switch( val ) {
             case 3 :  choix = articulationa; break;
              case 4 :  choix = articulationb; break;
              case 5 :  choix = articulationc; break;
              case 6 :  choix = articulationd; break;
              default : break;
            }
            cmd=true;        
          }
          else {
            choix.write(val);
            cmd=false;
          }
       }
      }
    };

Discussions similaires

  1. [WD17] communication arduino et windev
    Par fredcatlou dans le forum WinDev
    Réponses: 29
    Dernier message: 25/07/2016, 10h32
  2. [Python 3.X] Communication Serie Python - Arduino : Quelques questions avec Tkinter
    Par litepixel dans le forum Tkinter
    Réponses: 1
    Dernier message: 04/08/2015, 10h42
  3. Communication arduino avec VB.net
    Par chrifi dans le forum Arduino
    Réponses: 1
    Dernier message: 24/05/2013, 13h49
  4. Communication avec Arduino
    Par Stéphanie.Gautier dans le forum EDI et Outils pour Java
    Réponses: 1
    Dernier message: 23/05/2013, 22h07
  5. Bug communication serie avec arduino (busy)
    Par Invité dans le forum MATLAB
    Réponses: 2
    Dernier message: 09/07/2012, 09h39

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