Bonjour à tous,
Je suis débutant avec l'univers Arduino (j'ai obtenu ma carte la semaine dernière).
j'ai pour projet de créer un petit soft en VB qui me permettrais de communiquer avec l'Arduino Leonardo.
malheureusement je n'arrive pas à recevoir les données de l'Arduino, (en recherchant sur le net, j'ai eu l'impression que ce sujet a été traité des milliers de fois mais je n'ai pas trouvé de réponse).
j'aimerais, si c'est possible, un exemple basique de communication, Arduino vers VB.
je vous fournis le code que j'utilise pour l'instant (il doit y avoir pas mal d'erreurs...désolé mais je commence juste à m'intéresser à la programmation VB, arduino etc...)
Code Arduino
#include <Servo.h>
int ledPin = 13;
int potentiometre = 0;
int octetReception=0;
char caractereRecu=0;
int compt=0;
String chaineReception="";
int compteur = 0;
Servo Hitec322HD;
char tab[4];
int posServo = 0;
void setup()
{
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
Hitec322HD.attach(3);
Hitec322HD.write(90);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
}
void loop() {
while (Serial.available() <= 0) {
Serial.print(30);
delay(300);
}
while (Serial.available()>0) {
octetReception=Serial.read();
compt=compt+1;
if (octetReception==46) {
if (chaineReception == "ON")
{
digitalWrite(ledPin,HIGH);
}
if (chaineReception == "OFF")
{
digitalWrite(ledPin,LOW);
}
if (chaineReception != "OFF" && chaineReception != "ON" && chaineReception != "SEND")
{
chaineReception.toCharArray(tab, 4);
posServo = atoi(tab);
Hitec322HD.write(posServo);
}
if (chaineReception == "SEND")
{
Serial.write(1234);
digitalWrite(ledPin,HIGH);
delay(1000);
digitalWrite(ledPin,LOW);
}
chaineReception="";
compt=0;
delay(100);
break;
}
else {
caractereRecu=char(octetReception);
chaineReception=chaineReception+caractereRecu;
}
}
}
Code VB
Imports System.IO
Imports System.IO.Ports
Imports System.Threading
Public Class Form1
Shared _continue As Boolean
Shared _serialPort As SerialPort
Dim positionServo As String = 90
Dim Led As Integer = 0
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
SerialPort1.Close()
SerialPort1.PortName = "com3" 'change com port to match your Arduino port
SerialPort1.BaudRate = 115200
SerialPort1.DataBits = 8
SerialPort1.Parity = Parity.None
SerialPort1.StopBits = StopBits.One
SerialPort1.Handshake = Handshake.None
SerialPort1.Encoding = System.Text.Encoding.Default 'very important!
posServo.Text = positionServo & "°"
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonOn.Click
SerialPort1.Open()
SerialPort1.Write("ON.")
SerialPort1.Close()
' SerialPort1.Open()
' SerialPort1.Write("1")
' SerialPort1.Close()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonOff.Click
SerialPort1.Open()
SerialPort1.Write("OFF.")
SerialPort1.Close()
' SerialPort1.Open()
' SerialPort1.Write("0")
' SerialPort1.Close()
End Sub
Private Sub Send_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles decrementer.Click
If (positionServo > 0) Then
positionServo = positionServo - 1
posServo.Text = positionServo & "°"
End If
SerialPort1.Open()
SerialPort1.Write(positionServo & ".")
SerialPort1.Close()
End Sub
Private Sub Decrementer10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles decrementer10.Click
If (positionServo >= 10) Then
positionServo = positionServo - 10
posServo.Text = positionServo & "°"
End If
SerialPort1.Open()
SerialPort1.Write(positionServo & ".")
SerialPort1.Close()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles incrementer.Click
If (positionServo < 180) Then
positionServo = positionServo + 1
posServo.Text = positionServo & "°"
End If
SerialPort1.Open()
SerialPort1.Write(positionServo & ".")
SerialPort1.Close()
End Sub
Private Sub Incrementer10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles incrementer10.Click
If (positionServo <= 170) Then
positionServo = positionServo + 10
posServo.Text = positionServo & "°"
End If
SerialPort1.Open()
SerialPort1.Write(positionServo & ".")
SerialPort1.Close()
End Sub
'Sub SerialPort1_DataReceived(ByVal sender As System.Object, _
' ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _
' Handles SerialPort1.DataReceived
'
' Input.Text = SerialPort1.ReadByte()
'
'
'End Sub
Sub Receive_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Receive.Click
SerialPort1.Open()
SerialPort1.Write("SEND.")
SerialPort1.Close()
End Sub
Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
'SerialPort1.Open()
Input.Text = Str(SerialPort1.ReadByte())
'SerialPort1.Close()
End Sub
End Class
Merci à tous ceux qui me lirons et milles mercis à ceux qui m'aiderons à trouver une solution.
Je vous donne également le code source.