Hi there all, greetings from Oz :-)
I have an application I wrote sometime ago in VB6 (Enterprise). It involves talking to a RS232 device and receiving packets of data (of varying size) that are encapsulated by the traditional STX/ETX.
Now that I need to add a few new features to the application I have decided to rewrite it using VB.Net (VS2010).
My problem is that the data is appearing at the DataReceived Event is malformed or disjointed.
For example I have created a small test program to demonstrate this behavior, code listed below.
If I use a terminal program (RealTerm) to send the standard 'Hello World' what I receive in the debug window is ....
H
ello
World
I have tried setting a Thread.Sleep(100) command as the first line in the DataReceived event, this alleviates the problem in this example but in the long term it would not suffice as the data length varies up to 10 lines of 14 chars each. Or it could be just one 14 char line. Setting the sleep even higher may cause issues with the next packet of data.
Looking or waiting for the ETX is not an option either as it may not arrive and in this case the packet is to be discarded and its guaranteed that the next packet will be the same packet with the ETX included. Essentially there is some checking and if the data is not completely received it is sent again.
Hoping someone can assist me with this.
My test programming listing below ...
A simple form with a textbox, multiline on, scrollbars vert.
Public Class frmMain
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
With rs232
.BaudRate = 4800
.PortName = "COM6"
.StopBits = IO.Ports.StopBits.One
.Parity = IO.Ports.Parity.None
.DataBits = 8
rs232.Open()
End With
End Sub
Private Sub rs232_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles rs232.DataReceived
Dim strBuffer As String
CheckForIllegalCrossThreadCalls = False
strBuffer = rs232.ReadExisting
Debug.Print(strBuffer)
If InStr(strBuffer, Chr(2)) Then rs232.Write("#")
txtData.Text = txtData.Text & strBuffer & vbCrLf
End Sub
End Class
Cheers
Mike S