VB2005 Serial Port

Joined
Jan 28, 2009
Messages
9
Location
AZ
Programming Experience
Beginner
Still experimenting with the Serial Port component on VB2005 and I have been having issues with my data received event. I set my received bytes threshold to 38 bytes which is the number of bytes that I am expecting every 1 sec and when I get data I read it in my byte array. I use threading.thread.sleep and when I process this data I clear my port buffer using serialport.discardinbuffer method. Now my problem, it appears that ever so many odd seconds I get a problem with my stream and I end up having 44 sometimes 45 bytes coming in and I loose about two seconds of data im wondering if this is something with my read method or if its just the stream being delayed, any experienced programmers have any idea whats going on?
 
Hello.

Hard to say. I wouldn't use Thread.Sleep, there's no need for that if you use the DataReceived Event...also this could be a timing issue which can be caused by that. I also experienced that the DiscardBuffer methods can take pretty long depending on the OS and hardware (Windows XP and normal Serial Port about some milliseconds...Windows 2003 Server and NPort Server up to 250ms...which is not, at least I think so, a problem of the Framework 'cause the DiscardBuffer methods are leading directly to the API-Call purgeCom()).

Also some source-code would be of help to determine what's the problem on this. But try how it works without the sleep.

Bobby
 
Private Sub com1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs)
Dim returnStr As String
Dim intRead As Integer = 0
Dim btCnt As Integer = SerialPort1.BytesToRead
Dim InBuff(btCnt - 1) As Byte
Dim pRet As Integer

Application.DoEvents()
Threading.Thread.Sleep(1)

intRead = SerialPort1.Read(InBuff, 0, btCnt)

RECIP = IPAddress.Parse(txtIP.Text)
RecPortInt = txtPort.Text
udpClient.Connect(RECIP, RecPortInt)
bytCommand = InBuff 'Encoding.ASCII.GetBytes(txtInfo.Text)
pRet = udpClient.Send(bytCommand, bytCommand.Length)


CheckForIllegalCrossThreadCalls = False
txtInfo.Text &= returnStr & vbCrLf
SerialPort1.DiscardInBuffer()
End Sub
 
Hello.

I tried to reqrite the code a little:
VB.NET:
Private Sub com1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
	Dim returnStr As String = String.empty
	Dim InBuff(SerialPort1.BytesToRead - 1) As Byte
	Dim pRet As Integer = 0

	Trace.TraceInformation("COM1 just read: " & SerialPort1.Read(InBuff, 0, InBuff.Length).ToString("N0") & " Bytes of Data")
	
	'probably not needed...since it clears automatically what we read...just try without it
	'SerialPort1.DiscardInBuffer()
	
	'sending what we just read...
	RECIP = IPAddress.Parse(txtIP.Text)
	RecPortInt = txtPort.Text
	udpClient.Connect(RECIP, RecPortInt)
	bytCommand = InBuff 'Encoding.ASCII.GetBytes(txtInfo.Text)
	pRet = udpClient.Send(bytCommand, bytCommand.Length)

	CheckForIllegalCrossThreadCalls = False
	txtInfo.Text &= returnStr & Enviromnent.NewLine
End Sub

I'm not sure if this changes anything. But there should be now Debug-Prints which are allowing you to follow how many bytes were read each time.

Bobby
 
I ended up finding out my problem it was partially the modems dropping out and partially me clearing the buffer before my data received event would receive the incoming data.
 
serial port

send data:

If COMPort.IsOpen Then
COMPort.Close()
End If
Try
COMPort.BaudRate = 9600
COMPort.Parity = Parity.None
COMPort.DataBits = 8
COMPort.StopBits = 1
COMPort.Open()
COMPort.Write("data")
catch ex as exception
end try

recieve data:

Dim sdata As String = ""
Dim i As Integer = 0
Try
If COMPort.IsOpen Then
recieve = recieve & COMPort.ReadExisting
If Len(recieve) > 7 Then
For i = 0 To Len(recieve) - 1
sdata = sdata & recieve(i).ToString
Next
textbox1.text =sdata.tostring
catch ex as exception
end try
 
Back
Top