Offline
Newbie
Karma: 0
Posts: 30
|
 |
« on: April 04, 2012, 04:31:52 pm » |
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.
Any ideas on how to make it work??
Thanks.
|
|
|
|
|
Logged
|
|
|
|
|
Copenhagen / Denmark
Offline
Edison Member
Karma: 5
Posts: 2338
Do it !
|
 |
« Reply #1 on: April 05, 2012, 03:07:14 am » |
The serial port class has a datarecieved event. You can use that to know when data has arrived
You will also need to look into delegates because the code servicing the event is running in another thread than your code.
|
|
|
|
|
Logged
|
|
|
|
|
Belgium
Offline
Full Member
Karma: 0
Posts: 182
|
 |
« Reply #2 on: April 05, 2012, 04:33:00 am » |
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!
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 30
|
 |
« Reply #3 on: April 05, 2012, 09:22:07 am » |
Thanks Steen for you answer. 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?? I make sure to close the port right after I send! I am truly puzzeled! Any help will be appreciated. thanks.
|
|
|
|
|
Logged
|
|
|
|
|
Belgium
Offline
Full Member
Karma: 0
Posts: 182
|
 |
« Reply #4 on: April 05, 2012, 10:24:54 am » |
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); }
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 30
|
 |
« Reply #5 on: April 05, 2012, 11:02:23 am » |
Here is the vb code: 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... I do not however get anything in return... Thanks a lot for your help!
|
|
|
|
|
Logged
|
|
|
|
|
Belgium
Offline
Full Member
Karma: 0
Posts: 182
|
 |
« Reply #6 on: April 05, 2012, 11:31:59 am » |
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..
|
|
|
|
« Last Edit: April 05, 2012, 11:33:40 am by Steen »
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 30
|
 |
« Reply #7 on: April 05, 2012, 12:46:00 pm » |
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?? Thanks again for your help!!
|
|
|
|
|
Logged
|
|
|
|
|
Belgium
Offline
Full Member
Karma: 0
Posts: 182
|
 |
« Reply #8 on: April 05, 2012, 01:00:45 pm » |
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...
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #9 on: April 05, 2012, 02:10:55 pm » |
I make sure to close the port right after I send! Doing so resets the Arduino. Is that what you really want to do?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 30
|
 |
« Reply #10 on: April 05, 2012, 06:28:04 pm » |
no i do not want to reset it. that means that I should not close the port??
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #11 on: April 05, 2012, 06:40:34 pm » |
that means that I should not close the port?? Opening and closing the serial port resets the Arduino. You want to open the port once, then wait for the Arduino to finish resetting. Close the port only in the Form_FormClosed event.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 30
|
 |
« Reply #12 on: April 05, 2012, 06:43:18 pm » |
Thank you I did not know that. So should I open the port as soon as I load the form or wait until I actually need to communicate with the Arduino?
|
|
|
|
|
Logged
|
|
|
|
|
India
Offline
Newbie
Karma: 0
Posts: 18
|
 |
« Reply #13 on: April 06, 2012, 12:46:40 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 30
|
 |
« Reply #14 on: April 06, 2012, 08:34:41 am » |
Thank you, you have been extreamly helpful!
|
|
|
|
|
Logged
|
|
|
|
|
|