And this is the code I came up with...
Unfortunately it doesn't display anything. App just starts display "True" and no data is being displayed.
I am monitoring Arduino platform which I know is sending data constantly.
Code:
Imports System.IO.Ports
Imports System.Text
Public Class ArduinoSerial
Public Event ReceivedSerialData(ByVal data As String)
WithEvents Serial As SerialPort
Dim data As String
Private Sub ArduinoSerial_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Serial = My.Computer.Ports.OpenSerialPort("COM3", 9600)
TextBox.AppendText(Serial.IsOpen & vbCrLf)
End Sub
Private Sub Serial_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles Serial.DataReceived
RaiseEvent ReceivedSerialData(data)
End Sub
Public Sub DisplaySerialData(ByVal data) Handles Me.ReceivedSerialData
TextBox.AppendText(data & vbCrLf)
End Sub
End Class
I am really new into the vb.net. I am sure there are some horrible mistakes with this code but I am kind of stuck for the past 2 days on this one. Maybe someone could point me in the right direction. Thanks!
First let me say you have found in this forum a great resource when it comes to all things vb. There is a wealth of knowledge and experience that already has been shared. I suggest you use the search function on this site and do a search for "serial port" and derivatives.
In saying that part of your problem is here.
Code:
Private Sub Serial_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles Serial.DataReceived
RaiseEvent ReceivedSerialData(data)
End Sub
What sort of data are you expecting from your serial port?
bytes? strings of human readable text?
Also Im sure other more knowledgable people will be along to help as well.
Thank you so much for your help.
I revised the code below. I looked around and did some modifications to the code but unfortunately it still doesn't work. When I debug the program the event is not even raised. It seems like the data is not coming through.
When I test with Arduino software (Serial Monitor) I can see that the data is actually transmitted. So I don't know where to begin looking for a solution...
According the: http://www.arduino.cc/en/Serial/Print
"Numbers are printed using an ASCII character for each digit".
My arduino platform is sending only 1 or 0
Serial.print(1) gives "1".
Code:
Imports System.IO.Ports
Imports System.Text
Public Class ArduinoSerial
Public Event ReceivedSerialData(ByVal data As String)
WithEvents Serial As SerialPort
Dim data As String
Private Sub ArduinoSerial_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Serial = My.Computer.Ports.OpenSerialPort("COM3", 9600, Parity.None, 8, StopBits.One)
Serial.Encoding = System.Text.Encoding.ASCII
TextBox.AppendText(Serial.IsOpen & vbCrLf)
End Sub
Private Sub Serial_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles Serial.DataReceived
If Serial.BytesToRead = 0 Then Exit Sub
Dim buf() As Byte = New Byte() {}
Array.Resize(buf, Serial.BytesToRead)
Serial.Read(buf, 0, buf.Length)
data = buf.ToString
RaiseEvent ReceivedSerialData(data)
End Sub
Public Sub DisplaySerialData(ByVal data) Handles Me.ReceivedSerialData
TextBox.AppendText(data & vbCrLf)
End Sub
End Class
Like I said above I see that Arduino is sending data over COM3. (attached screenshot)
It would be simpler if you simply dropped a SerialPort control from the toolbox onto the design surface. When you do that, the event handler is built for you. If you want to do this in code, the correct declaration would be
Code:
Private WithEvents SerialPort As New System.IO.Ports.SerialPort
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
With SerialPort
.PortName = "Com2" 'for example
.BaudRate = 9600
.RtsEnable = True
.Open()
'etc.
End With
End Sub
Private Sub SerialPort_DataReceived(sender As Object, e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort.DataReceived
Dim DataFromPort As String = SerialPort.ReadExisting
Debug.Print(DataFromPort)
End Sub
Now, this is pretty simple-minded, and is only a starting point. If you go to my website, www.hardandsoftware.net and download Enhanced SerialPort (this is free), I have example code that is more complete. The example code can be used with the built-in SerialPort object, as long as you do not call any of the Enhanced SerialPort extensions -- however, you might as well use Enhanced SerialPort, since it is just a wrapper for SerialPort, and does not remove any functions.
Dick
Richard Grier, Consultant, Hard & Software
Microsoft MVP (Visual Basic)