Arduino and Visual Basic Part 2 - Receiving Data From The Arduino - Martyn Currey
Arduino and Visual Basic Part 2 - Receiving Data From The Arduino - Martyn Currey
Martyn Currey
Mostly Arduino stuff
This post continues from Arduino and Visual Basic Part 1: Receiving Data From the Arduino
In the previous post we received a stream of data from the Arduino and displayed it inside a Visual Basic
text box. This is all well and good but we did not know what the data was, we simply received it and
displayed it.
The next step is to send data that has some kind of meaning and display it in an appropriate field. This
could be a temperature, a wind speed, a switch state or anything else. In the following example I am
using a 1 wire temperature probe (it’s actually got 2 wires…), a potentiometer and a button switch.
Commands or Codes
To identify which values are which we will use a simple code attached to the actual value:
B for button switch
P for potentiometer
T for temperature
So we know when we have a complete data segment or code we will use start and end markers, “<” and
“>”. The Visual Basic app will ignore anything not inside the markers.
The button switch only has 2 states HIGH or LOW / pressed or not pressed so a single character can be
used for the value. In this example H for HIGH and L for LOW. The full codes are:
<BH>, button switch HIGH or pressed
<BL>, button swtich LOW or not pressed
The potentiometer gives a value between 0 and 1023. To make things a little easier the example uses
ascii characters for the value (“1000″ instead of 1000) and a fixed length of 4 characters; “0000” to
http://www.martyncurrey.com/arduino-and-visual-basic-part-2-receiving-data-from-the-arduino-part-2/ 1/26
1/8/2018 Arduino and Visual Basic Part 2: Receiving Data From the Arduino | Martyn Currey
“1023”. Of course this means the value has to be converted to a string before sending.
<Pnnnn>, P for potentiometer and nnnn = the value of the potentiometer
<P0000>, potentiometer value of zero
<P1023>, potentiometer value of 1023
In line with keeping things simple, an integer is used to store the value of the temperature probe (you
would normally use a float). This means we can treat it the same as the potentiometer and use a string
of 4 characters for the value; “0000” to “1023”
<Tnnnn>, T for temperature and nnnn = the value of the temperature probe
<T0504>, temperature probe value of 504
<T0400>, temperature probe value of 400
http://www.martyncurrey.com/arduino-and-visual-basic-part-2-receiving-data-from-the-arduino-part-2/ 2/26
1/8/2018 Arduino and Visual Basic Part 2: Receiving Data From the Arduino | Martyn Currey
Circuit Diagram
Arduino Sketch
The Arduino sketch polls the values of the pins and then, if the value has changed, creates the code and
then sends the code out over the serial connection.
/*
*
* Sketch Arduino to Visual Basic 002 - Receiving Data From the Arduino
*
* Read pin state / value and send to a host computer over a serial connection.
* This is a one way communication; Arduino to a host computer No data is received from the
http://www.martyncurrey.com/arduino-and-visual-basic-part-2-receiving-data-from-the-arduino-part-2/ 3/26
1/8/2018 Arduino and Visual Basic Part 2: Receiving Data From the Arduino | Martyn Currey
This is a one way communication; Arduino to a host computer. No data is received from the
*
* Pins
* A0 - a 2 wire temperature probe (sending raw data only. Not the actual temperature)
* A1 - a potentiometer
* D4 - button switch
* The above can be changed to something else
*
*
* It should noted that no data is sent until something changes
* The sketch can be expanded so that an initial value is sent
*
*/
void setup()
{
// set the button switch pin to input
pinMode(buttonSwitchPin, INPUT);
void loop()
{
if (newTempVal != oldTempVal)
{
oldTempVal = newTempVal;
formatNumber( newTempVal 4);
http://www.martyncurrey.com/arduino-and-visual-basic-part-2-receiving-data-from-the-arduino-part-2/ 4/26
1/8/2018 Arduino and Visual Basic Part 2: Receiving Data From the Arduino | Martyn Currey
if (newButtonSwitchState != oldButtonSwitchState)
{
oldButtonSwitchState = newButtonSwitchState;
if (oldButtonSwitchState == true)
{
Serial.print("<BH>");
}
else
{
Serial.print("<BL>");
}
if (DEBUG) { Serial.println(""); }
delay (100);
http://www.martyncurrey.com/arduino-and-visual-basic-part-2-receiving-data-from-the-arduino-part-2/ 5/26
1/8/2018 Arduino and Visual Basic Part 2: Receiving Data From the Arduino | Martyn Currey
strcat(numberString,tempString);
Load the sketch and open the serial monitor. You should get something like the following:
http://www.martyncurrey.com/arduino-and-visual-basic-part-2-receiving-data-from-the-arduino-part-2/ 6/26
1/8/2018 Arduino and Visual Basic Part 2: Receiving Data From the Arduino | Martyn Currey
http://www.martyncurrey.com/arduino-and-visual-basic-part-2-receiving-data-from-the-arduino-part-2/ 7/26
1/8/2018 Arduino and Visual Basic Part 2: Receiving Data From the Arduino | Martyn Currey
Turn the potentiometer and you should see something similar to:
Now that the Arduino side is working the Visual Basic app is next.
We can use the Visual Basic app from Arduino and Visual Basic Part 1: Receiving Data From the
Arduino as the starting point but we need to make some changes. Unlike the previous example, the data
is now inside start and end markers and we we need to determine what data we are receiving.
http://www.martyncurrey.com/arduino-and-visual-basic-part-2-receiving-data-from-the-arduino-part-2/ 8/26
1/8/2018 Arduino and Visual Basic Part 2: Receiving Data From the Arduino | Martyn Currey
http://www.martyncurrey.com/arduino-and-visual-basic-part-2-receiving-data-from-the-arduino-part-2/ 9/26
1/8/2018 Arduino and Visual Basic Part 2: Receiving Data From the Arduino | Martyn Currey
The form is the same one used in the previous example. New labels have been added to show the
temperature, the potentiometer value and the switch state. I have also included labels to show the timer
interval and the number of commands processed.
http://www.martyncurrey.com/arduino-and-visual-basic-part-2-receiving-data-from-the-arduino-part-2/ 10/26
1/8/2018 Arduino and Visual Basic Part 2: Receiving Data From the Arduino | Martyn Currey
I have left the text box and use it to display the current command. You can also use the text box for
debugging.
The code
'
' Arduino and Visual Basic Part 2: Receiving Data From the Arduino
' An example of receiving data from an Arduino using start and end markers
'
Imports System
Imports System.IO.Ports
comPORT = ""
For Each sp As String In My.Computer.Ports.SerialPortNames
comPort_ComboBox.Items.Add(sp)
Next
End Sub
SerialPort1.Open()
connect_BTN.Text = "Dis-connect"
http://www.martyncurrey.com/arduino-and-visual-basic-part-2-receiving-data-from-the-arduino-part-2/ 11/26
1/8/2018 Arduino and Visual Basic Part 2: Receiving Data From the Arduino | Martyn Currey
comPort_ComboBox.Enabled = False
Timer1.Enabled = True
Timer_LBL.Text = "Timer: ON"
Else
MsgBox("Select a COM port first")
End If
Else
Timer1.Enabled = False
SerialPort1.Close()
connect_BTN.Text = "Connect"
comPort_ComboBox.Enabled = True
End If
End Sub
'stop the timer (stops this function being called while it is still working
Timer1.Enabled = False
Timer_LBL.Text = "Timer: OFF"
' get any new data and add the the global variable receivedData
receivedData = ReceiveSerialData()
http://www.martyncurrey.com/arduino-and-visual-basic-part-2-receiving-data-from-the-arduino-part-2/ 12/26
1/8/2018 Arduino and Visual Basic Part 2: Receiving Data From the Arduino | Martyn Currey
Return Incoming
End If
Catch ex As TimeoutException
Return "Error: Serial Port read timed out."
End Try
End Function
Function parseData()
pos1 = receivedData.IndexOf("<") + 1
pos2 = receivedData.IndexOf(">") + 1
'occasionally we may not get complete data and the end marker will be in front o
' for exampe "55><T0056><"
' if pos2 < pos1 then remove the first part of the string from receivedData
If (pos2 < pos1) Then
receivedData = Microsoft.VisualBasic.Mid(receivedData, pos2 + 1)
pos1 = receivedData.IndexOf("<") + 1
pos2 = receivedData.IndexOf(">") + 1
End If
Else
' we have both start and end markers
http://www.martyncurrey.com/arduino-and-visual-basic-part-2-receiving-data-from-the-arduino-part-2/ 13/26
1/8/2018 Arduino and Visual Basic Part 2: Receiving Data From the Arduino | Martyn Currey
buttonSwitchValue_lbl.Text = "HIGH"
End If
End If ' (newCommand(0) = "B")
commandCount = commandCount + 1
commandCountVal_lbl.Text = commandCount
End While
End Function
End Class
comPORT = ""
For Each sp As String In My.Computer.Ports.SerialPortNames
comPort_ComboBox.Items.Add(sp)
Next
End Sub
The program then waits for the user to select a COM port and hit the Connect button. If the Connect
button is clicked without a COM port selected an error message is displayed. If a COM port has been
selected then the serial port attributes are set and the program tries to open the port. If successful the
Connect button text is change to “Dis-connect” and the timer started. The timer is used to check for
http://www.martyncurrey.com/arduino-and-visual-basic-part-2-receiving-data-from-the-arduino-part-2/ 14/26
1/8/2018 Arduino and Visual Basic Part 2: Receiving Data From the Arduino | Martyn Currey
incoming data. The timer is set to 100ms. This means there are 10 checks a second to see if there is
any new serial data.
If there is an existing connection (the Connect button text is not equal to “Connect”) then the serial port
is closed.
SerialPort1.Open()
connect_BTN.Text = "Dis-connect"
comPort_ComboBox.Enabled = False
Timer1.Enabled = True
Timer_LBL.Text = "Timer: ON"
Else
MsgBox("Select a COM port first")
End If
Else
Timer1.Enabled = False
SerialPort1.Close()
connect_BTN.Text = "Connect"
comPort_ComboBox.Enabled = True
End Sub
When the timer function is called, any new serial data is added to a buffer in the form of a global variable
called receivedData. Adding the new data to a buffer like this ensures we get full commands. Only
checking the latest received data means we are likely to miss commands. The received data could be
fragmented like this; “34<“, “T011″, “1><BH><P0245><BL><T01″.
http://www.martyncurrey.com/arduino-and-visual-basic-part-2-receiving-data-from-the-arduino-part-2/ 15/26
1/8/2018 Arduino and Visual Basic Part 2: Receiving Data From the Arduino | Martyn Currey
If the buffer contains a start marker (“<“) and also an end marker(“>”) then we have a complete
command (not 100% true!) and the function parseData() is called. Because receivedData is a global
variable we do not need to pass it to the parseData() function.
'stop the timer (stops this function being called while it is still working
Timer1.Enabled = False
Timer_LBL.Text = "Timer: OFF"
' get any new data and add the the global variable receivedData
receivedData = ReceiveSerialData()
Because the received data buffer may have more than one command the function loops through the
received data removing the first complete command from the buffer on each loop. The positions of the
start and end markers are used to determine if we still have a complete command.
pos1 = receivedData.IndexOf("<") + 1
pos2 = receivedData.IndexOf(">") + 1
Using the positions of the first start and end markers the first command can be copied to the variable
newCommand.
http://www.martyncurrey.com/arduino-and-visual-basic-part-2-receiving-data-from-the-arduino-part-2/ 16/26
1/8/2018 Arduino and Visual Basic Part 2: Receiving Data From the Arduino | Martyn Currey
Once we have the command we can check which one it is and update the form.
commandCount = commandCount + 1
commandCountVal_lbl.Text = commandCount
Downloads
Arduino_to_Visual_Basic_002_-_Receiving_Data_From_the_Arduino -
Arduino Sketch 1.73 KB
Download (http://www.martyncurrey.com/?wpdmdl=2509)
This entry was posted in Arduino + Visual Basic by Martyn. Bookmark the permalink
[http://www.martyncurrey.com/arduino-and-visual-basic-part-2-receiving-data-from-the-arduino-
part-2/] .
http://www.martyncurrey.com/arduino-and-visual-basic-part-2-receiving-data-from-the-arduino-part-2/ 17/26
1/8/2018 Arduino and Visual Basic Part 2: Receiving Data From the Arduino | Martyn Currey
20 THOUGHTS ON “ARDUINO AND VISUAL BASIC PART 2: RECEIVING DATA FROM THE ARDUINO”
Dan Farber
on June 5, 2016 at 6:36 am said:
Thank you for this article- I still did not read it but it looks good- I will try it pretty soon-
again thanks for you effort
Pingback: Arduino and Visual Basic Part 1: Receiving Data From the Arduino | Martyn
Currey
Dan Farber
on July 15, 2016 at 11:06 am said:
I ran the program and sometimes it works good and sometimes it gets stuck.
The VB says (after it compiles the program) that there might be a null exeption when
running the program since the parse() function may not return a value for all cases. Is
there a cure for that?
Appreciate an answer to my E- mail
Thanks a lot
Martyn
on July 15, 2016 at 1:29 pm said:
Hi Dan,
I haven’t used this for a while but I never had an issue in the past and I use the
same code on my dropController project which runs without a problem.
The parseData() does not return a value. It looks for the start and end markers
and then takes the data they contain. If it finds data it checks what the data is.
http://www.martyncurrey.com/arduino-and-visual-basic-part-2-receiving-data-from-the-arduino-part-2/ 18/26
1/8/2018 Arduino and Visual Basic Part 2: Receiving Data From the Arduino | Martyn Currey
Can you post a more specific error and possible show the program line in the
code.
Tasos
on January 27, 2017 at 11:59 am said:
The null you see from the vb could be an empty com port. Or not existing tha port
By the way it is very good example and very well comment, it helps a lot !!!! Well done!!!
Kind regards
Tasos
John
on March 15, 2017 at 6:43 pm said:
Hi Martyn,
I have a problem and was wondering if you could guide what the issue could be?
Setup:
I setup a board with 2 switches, Switch A and Switch B. Switch A = Pin D2 and Switch
B = Pin D4. Used the same code of yours in the vb software to monitor both pins. In the
arduino, I programmed them as AH, AL (for Switch A) and BH, BL (for Switch B).
Attached 10K ohm resistor against Switch A as per your diagram and again one more
10K ohm resistor against Switch B in the same manner and polarity.
Power Source: USB from the laptop (no external power).
Problem:
Both the pin outputs keep swapping between H and L after every few seconds (ranges
http://www.martyncurrey.com/arduino-and-visual-basic-part-2-receiving-data-from-the-arduino-part-2/ 19/26
1/8/2018 Arduino and Visual Basic Part 2: Receiving Data From the Arduino | Martyn Currey
between 3 seconds to 15 seconds but averages at about 6-8 seconds). Could there be
an issue with the circuitry? By turning on/off the switch – i am still not able to make out if
its affecting the signals as the output keeps swapping between H and L for both pins
every few seconds.
John
on March 15, 2017 at 7:03 pm said:
Hi Martyn,
Here is a sample output with timestamp. You can notice the difference in
seconds. This keeps repeating for ever.
00:30:40 – Command = AL
00:30:40 – Command = BL
00:30:47 – Command = AH
00:30:47 – Command = BH
00:30:50 – Command = AL
00:30:50 – Command = BL
00:30:58 – Command = AH
00:30:58 – Command = BH
00:31:02 – Command = AL
00:31:02 – Command = BL
00:31:09 – Command = AH
00:31:09 – Command = BH
00:31:13 – Command = AL
00:31:13 – Command = BL
00:31:20 – Command = AH
00:31:21 – Command = BH
00:31:24 – Command = AL
00:31:24 – Command = BL
Martyn
on March 16, 2017 at 7:01 am said:
http://www.martyncurrey.com/arduino-and-visual-basic-part-2-receiving-data-from-the-arduino-part-2/ 20/26
1/8/2018 Arduino and Visual Basic Part 2: Receiving Data From the Arduino | Martyn Currey
Try different switches if you can. If not, remove the switches and replace
with a short length of wire and then use the wire as a switch.
John
on March 16, 2017 at 1:01 pm said:
Hi Martyn,
Thanks for your guidance. Yes, I verified everything that you said.
The circuit is not moving, its static on a table. Infact, I dont have
switches at the moment as I am still prototyping. I am using two
short wires instead as a switch.
I will address both issues above if possible and post the outcome
soon. Thanks a lot for your guidance though and it was helpful to
start looking in the right direction towards trouble shooting the
issue.
Also, Yes, Output in Serial Monitor was also behaving the same
way as explained yesterday.
http://www.martyncurrey.com/arduino-and-visual-basic-part-2-receiving-data-from-the-arduino-part-2/ 21/26
1/8/2018 Arduino and Visual Basic Part 2: Receiving Data From the Arduino | Martyn Currey
Regards,
John.
John
on March 16, 2017 at 1:11 pm said:
Regards,
John.
Martyn
on March 16, 2017 at 1:15 pm said:
John
http://www.martyncurrey.com/arduino-and-visual-basic-part-2-receiving-data-from-the-arduino-part-2/ 22/26
1/8/2018 Arduino and Visual Basic Part 2: Receiving Data From the Arduino | Martyn Currey
Hi Mark,
http://www.martyncurrey.com/arduino-and-visual-basic-part-2-receiving-data-from-the-arduino-part-2/ 23/26
1/8/2018 Arduino and Visual Basic Part 2: Receiving Data From the Arduino | Martyn Currey
Cheers.
Regards,
John.
Rick Paul
on May 1, 2017 at 5:49 pm said:
Thank you so much for this posting. It was exactly what I needed to start a project. Your
sharing is greatly appreciated.
http://www.martyncurrey.com/arduino-and-visual-basic-part-2-receiving-data-from-the-arduino-part-2/ 24/26
1/8/2018 Arduino and Visual Basic Part 2: Receiving Data From the Arduino | Martyn Currey
fauzi
on May 24, 2017 at 3:13 pm said:
halo sir,
I want to ask something about this project, i made something a little different from the
program you created, i just use push button without potentiometer etc, i set the program
on arduino to send serial data “” if pushbutton one is pressed and ” “If not pressed, I try
to simplify the program you have created with the sole purpose of changing the text
label and changing the fill color of a shape I have created using the program in the
timer1 section as follows:
ReceivedData = ReceiveSerialData ()
Tbxinput.Text & = receivedData
If (receivedData = “”) Then
Lbllampu1.Text = “lampu1: On”
Lampu1.FillColor = Color.Blue
End If
End Sub
No error message appears but the program does not work as I like(the text label is
unchanged and the fill color does not change), can you show me where the mistake I
made and how it should be, please help me, thanks
Martyn
on May 25, 2017 at 1:20 pm said:
Don’t use “” use something with an actual value like “Lamp: Off”. On the Arduino, keep
the switch state in a variable and only send “Lamp: Off” when the state changes to off.
fauzi
on May 25, 2017 at 2:51 pm said:
http://www.martyncurrey.com/arduino-and-visual-basic-part-2-receiving-data-from-the-arduino-part-2/ 25/26
1/8/2018 Arduino and Visual Basic Part 2: Receiving Data From the Arduino | Martyn Currey
I’m sorry sir, I do not mean to write “” (empty) in the comment field, but I write
(“”), if I write “” + (inside), the word in it suddenly disappears after the comment
is sent and I do not know why Like that, sorry I did not realize it
Back to my program, actually what I mean in the “” sign is not empty, but there is
a word if push button on press and if pushbutton is removed and with the
program I use above does not work as desire
fauzi
on May 25, 2017 at 2:52 pm said:
fauzi
on May 25, 2017 at 2:58 pm said:
I just change the serial data I send from arduino because if the write in this
comment always disappear: P, sorry sir I repeat the question
I send the data serial “1H” if pushbutton is pressed, and “1L” if pushbutton is
released, the serial data is received and read in the textbox, and I want to use it
to change the text and change the shape color,
ReceivedData = ReceiveSerialData ()
Tbxinput.Text & = receivedData
If (receivedData = “1H”) Then
Lbllampu1.Text = “lampu1: On”
Lights1.FillColor = Color.Blue
End If
End Sub
No error message appears but the program does not work as I like (the text
label is unchanged and the fill color does not change), can you show me where
the mistake I made and how it should be, please help me, thanks
Martyn
on May 26, 2017 at 3:29 am said:
http://www.martyncurrey.com/arduino-and-visual-basic-part-2-receiving-data-from-the-arduino-part-2/ 26/26