Hello all,
I need to both send and recieve data from vb to arduino and back.
Sending the data seems to work but I have no idea how to make vb read the serial port... I have tried adding a timer to do so but all it does is freeze my application.
This is my code to read from the serial port when something gets sent:
Sub EventHandler(sender As Object, e As System.IO.Ports.SerialDataReceivedEventArgs) Handles Arduino.DataReceived
Dim datareceived As Boolean = False
Dim recStr as String = ""
While Not datareceived
Dim recVal As Integer
recVal = Arduino.ReadByte()
If recVal = 10 Or recVal = 13 Then
datareceived = True
Else
recStr = recStr + Chr(recVal)
End If
End While
End Sub
For this you need a serialport named Arduino and that's it, this should make it work perfectly (it works for me)
EDIT: you have to use println() for this, if you use print() it get in an infinite loop!
I have tried your code but it did not work...
I know I can send data to the arduino thru VB because I was able to control the led (turn it on and off...)
On the arduino side, all I have is a serial.println(integer here)...
And when I use the serial monitor I do get the propper value printed.
It is only when I am using vb that nothing happens
could it be because I am sending stuff and shortly after trying to recieve??
could you please post some example code, since now it is hard to tell what you might be doing wrong.
The code i posted works, but you have to set the correct baudrate, the parity etc to make it working.
And another thing, you say you read the serial port just before you send to it, does that mean you print on command of the PC? like if the pc sends 'A' then the arduino has to send the int ?
this is the complete code to receive data:
Imports System.IO.Ports
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
With SerialPort1
.Close()
.PortName = "COM3"
.BaudRate = 19200
.Parity = Parity.None
.DataBits = 8
.StopBits = StopBits.One
.ReceivedBytesThreshold = 1
.Open()
End With
End Sub
Sub EventHandler(sender As Object, e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
Dim datareceived As Boolean
Dim recStr As String
datareceived = False
recStr = ""
While Not datareceived
Dim recVal As Integer
recVal = SerialPort1.ReadByte()
If recVal = 10 Or recVal = 13 Then
datareceived = True
Else
recStr = recStr + Chr(recVal)
End If
End While
TextBox1.Text = TextBox1.Text + vbNewLine + recStr
End Sub
End Class
and the arduino code:
int cnt = 0;
int recVal;
void setup() {
Serial.begin(19200);
}
void loop() {
cnt++;
recVal = Serial.read();
Serial.println(cnt);
}
Public Class Form1
Shared _continue As Boolean
Shared _serialPort As SerialPort
Dim sendvar As Integer
Dim somevariable As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
diesizelabel.Hide()
diecavitylabel.Hide()
SerialPort1.Close()
SerialPort1.PortName = "com5" 'change com port to match your Arduino port
SerialPort1.BaudRate = 9600
SerialPort1.DataBits = 8
SerialPort1.Parity = Parity.None
SerialPort1.StopBits = StopBits.One
SerialPort1.Handshake = Handshake.None
SerialPort1.Encoding = System.Text.Encoding.Default 'very important!
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Stopautomaticcontrol.Click
'sending 5002 is auto control stop
SerialPort1.Open()
SerialPort1.Write("5002")
SerialPort1.Close()
MessageBox.Show("Variable sent to the Microcontroller is: 5002")
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
somevariable = 0
'moving to the left 1 step
somevariable = 5011
'variable to be sent is 5011
sendvar = somevariable
'sending the integer
SerialPort1.Open()
SerialPort1.Write(sendvar)
SerialPort1.Close()
MessageBox.Show(sendvar, "Variable sent to the Microcontroller is:")
Sub EventHandler(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
Dim datareceived As Boolean = False
Dim recStr As String = ""
While Not datareceived
Dim recVal As Integer
recVal = SerialPort1.ReadByte()
If recVal = 10 Or recVal = 13 Then
datareceived = True
Else
recStr = recStr + Chr(recVal)
End If
End While
MessageBox.Show(recStr, "Message from the Microcontroller")
End Sub
on the arduino side:
int a = 0;
int b = 0;
int c = 0;
int d = 0;
int val = 0;
void setup() {
Serial.begin(9600);
pinMode(13, OUTPUT);
}
void loop() {
if (Serial.available() == 4) {
a = Serial.read();
b = Serial.read();
c = Serial.read();
d = Serial.read();
val = a*1000 + b*100 + c*10 + d;
Serial.println(val);
if(val == 5011){
digitalWrite (13, HIGH);
}
if (val == 5002){
digitalWrite (13, LOW);
}
}
Serial.flush();
}
so the arduino is recieving the trasmitions because the led operates as expected...
Why do you do if (Serial.available() == 4) { ? that is just too explicit to function. You'd better use just if (Serial.available() > 1) {. This is explicit enough to make it work, but not restricted.
change the SerialPort1.Datareceived to SerialPort1.dataReceived (notice the lowercase d from data)
where do you close this sub:
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click ?
since i don't see an End Sub anywhere
i think the rest of the code OK.. try these changes, and tell us what hapened
EDIT: and btw, if your port is closed, it will ont receive anything, will it? try to delete those SerialPort1.Open() and .Close() commands and maybe it will function
Since it needs to be activated inorder to be able to receive st..
Why do you do if (Serial.available() == 4) { ? that is just too explicit to function. You'd better use just if (Serial.available() > 1) {. This is explicit enough to make it work, but not restricted.
I am using it that way to make it a filter to only accept 4 characters.
BTW, my problem was indeed the port being closed!! THANK YOU SO MUCH.
Now, it works fine but everytime I get a communication from the Arduino a get not only one message box but two: the first one has the variable that I have sent (great that was the goal) but the second one that comes up (not supposed to come up at all) has nothing in it.
Do you this it is the arduino side that sends an extra empty string or is it the vb side that is messing up??
change MsgBox(recVal or whatever variable here,,"received value) instead of MsgBox(recVal, "received value")
since the title is the third optional command, it might be something messing up with that. the second one, the one you give "title" is the button command...
maxwee2001:
So should I open the port as soon as I load the form or wait until I actually need to communicate with the Arduino?
in my understanding,
you could do either.
just make sure you aren't opening and closing it unnecessarily.
also make sure you at least do it once before communicating.
and do close it once you done communicating.