Add this to your vb.net project to have access to your com ports. I use this to recieve data from my Arduino MEGA. Visual Studio 2008
Imports System.IO
Imports System.IO.Ports
Imports System.Threading
Now you have the ability to connect, but connect to what? No problem just ask Windows!
For i As Integer = 0 To _
My.Computer.Ports.SerialPortNames.Count - 1
cbbComPorts.Items.Add( _
My.Computer.Ports.SerialPortNames(i))
Next
And you get a list of ports on your computer. cbbComPorts is a combobox I store the list in.
Now you have a port how do you get it open?
Try
With SerialPort1
.PortName = cbbComPorts.Text
.BaudRate = 9600
.Parity = IO.Ports.Parity.None
.DataBits = 8
.StopBits = IO.Ports.StopBits.One
End With
SerialPort1.Open()
lblMessage.Text = cbbComPorts.Text & " connected."
Catch ex As Exception
MsgBox(ex.ToString)
End Try
NOTE: be sure to use TRY, this will take care of any cases where VB can't open the port for whatever reason.
Now that you have the port open what do you do? In this case you would use readline, there are other commands but this command will read data until a CR/LF is sent.
'Grab data from the serial port - data from MEGA is TEMP, value future plans are for other readings to be passed.
Dim NewTemp() As String = Split(SerialPort1.ReadLine, ",")
Gee that is all well and good but how the heck do you know when something is being sent over?
Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
Your code goes Here ----
End SUb
PLEASE NOTE - any code you use to process the data needs to be called inside this Sub. I use this code to do Real Time Tempature graphing and it works like a charm. My MEGA reads a DS18S20 and sends the tempature to my PC.
Being a good programmer if you open something make sure you close it when finished with it.(notice the use of TRY again)
Try
SerialPort1.Close()
lblMessage.Text = SerialPort1.PortName & " disconnected."
Catch ex As Exception
MsgBox(ex.ToString)
End Try
Here is the Arduino code that sends the data over Serial.
void loop(){
sensors.requestTemperatures(); // Send the command to get temperatures
Serial.print("TEMP,");
Serial.println(sensors.getTempFByIndex(0),2);
delay(750);
Enjoy!
RTFM!!!!!