Reading the serial port in Visual Basic 2008??
I have tried many options for this, but can come up with nothing that is compatable with my current setup. I have this code (as a template):
I need to have a function that waits until a serial signal is received in the USB port and then stores the following string of bytes into a String. Can someone help get this working with my current code?
Thanks,
Micro
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Check out my new website!!
Use the Propeller icon!!
Follow me on Twitter! Search "Microcontrolled"
Imports System.IO Public Class frmMain Dim WithEvents SerialPort As New IO.Ports.SerialPort ' serial port declare Dim port as string Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load port = InputBox("Select the COM port you wish to open","COM port","COM9") SerialPort.ReadTimeout = 100 SerialPort.PortName = port SerialPort.BaudRate = "9600" ' 38400 SerialPort.Parity = IO.Ports.Parity.None ' no parity SerialPort.DataBits = 8 ' 8 bits SerialPort.StopBits = IO.Ports.StopBits.One ' one stop bit SerialPort.ReadTimeout = 1000 ' milliseconds so times out in 1 second if no response SerialPort.Open() ' open the port SerialPort.DiscardInBuffer() ' clear the input buffer SerialPort.Write("Hello World") SerialPort.Close() End Sub
I need to have a function that waits until a serial signal is received in the USB port and then stores the following string of bytes into a String. Can someone help get this working with my current code?
Thanks,
Micro
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Check out my new website!!
Use the Propeller icon!!

Follow me on Twitter! Search "Microcontrolled"
Comments
http://forums.parallax.com/showthread.php?p=908941
Imports System.IO
Imports Strings = Microsoft.VisualBasic ' so can use things like left( and right( for strings
2) for reading bytes, a buffer at the beginning of frmmain
Dim InPacket(0 To 2000) As Byte
3) if you get sick of typing in the com port every time you start - put it in a startup form
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
and you can set the timer interval here too
Timer1.Interval = 150 ' check for new characters every 150ms
4) a timer - and you need to disable the timer while the timer does things in case it causes reentry code
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
' collect bytes from the serial port
Timer1.Enabled = False
.... your code ...
Timer1.Enabled = True
End Sub
5) call this subroutine from your timer
Sub ReadSerialPort()
Dim BytesToRead As Integer ' may as well get in batches
Dim i as integer
Do
If SerialPort1.BytesToRead = 0 Then Exit Do ' no more bytes
BytesToRead = SerialPort1.BytesToRead
If BytesToRead > 2000 Then BytesToRead = 2000
SerialPort1.Read(InPacket, 0, BytesToRead) ' read in a packet
For i = 1 To BytesToRead
.. process the bytes here, turn them into a string etc ...
Next i
Loop
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
www.smarthome.viviti.com/propeller
I know how difficult it can be to choose the right examples and to avoid mistakes but its a great self teaching experience.
There are so many options for reading and writing data to and from the serial port that it really becomes application dependent.
When you say you want to store the serial data from USB to a string I am assuming the string is coming from the Prop to a VB application. If that is the case it happens to be one of the easier examples of VB receiving data. In the code below what you refer to as a signal I have called the Header , the Header is supposed to be a unique string that tells VB that what immediately follows is the string data to store or in this example display. The instruction used to read both the string data and the Header is the serial port ReadLine , Readline reads from the serial port buffer until it reaches a NewLine character ( 10 ) , the NewLine acts as the string terminator. So say you want to transmit "Hello World" from Prop to PC you would transmit two strings "Header" and "Hello World" each terminated with a NewLine like this "Header",10,"Hello World",10 . To contradict that a little what I would prefer to transmit is 10,"Header",10,"Hello World",10 .
Here's a basic example thats enclosed in the DataReceived event of your serial port . One thing you must bear in mind is that the DataReceived event runs in a thread separate from the main thread this prevents you from directly accessing Controls created in the main thread from the DataReceived event ( in this example DataReceived can not directly access the RichTextBox ) . You would normally use a Delegate but to keep this example as simple as possible insert the following line into Form1 Load event CheckForIllegalCrossThreadCalls=False , this will prevent exceptions
·Create your application as you normally would and add a RichTextBox to display the incoming data.
Good luck with your Prop / VB documents , I will continue to follow withe interest
Jeff T.