K
kpg
Hi all,
What is the 'best' way to wait for data from a serial port.
I'm using ,net 2.0 system.io.port, and I have a working class that listenes
to the serial port's data received event.
I want to implement a WaitForResponse method that will send a command to
the com port and wait for the completed reply (a CR terminates a reply).
I don't want to sit there and poll it in a loop, and sleeping seems
inefficient to me (how long to sleep?). Concerning the sleep, I'm assuming
here that the com port is running on a different thread so if I sleep my
thread it will continue - is that correct?
Here are the highlights of the serial port class:
_RXBuffer as string
Public Sub New()
AddHandler comPort.DataReceived, AddressOf myDataReceived
End Sub
Public Sub WriteData(ByVal buffer As String)
comPort.Write(buffer)
End Sub
Private Sub comPort_DataReceived(ByVal sender As Object, _
ByVal e As SerialDataReceivedEventArgs)
Dim buffer As String = comPort.ReadExisting()
_RXBuffer &= buffer
End Sub
I want to add:
Public Sub WaitForResponse(ByVal buffer As String)
comPort.Write(buffer)
'wait here until there is some data to inspect
'then test if data has a CR
'is so exit sub
End Sub
This code must have been written a million times by now. Either my google
skills are impaired or it's a closely guarded secret :0
Thanks,
kpg
What is the 'best' way to wait for data from a serial port.
I'm using ,net 2.0 system.io.port, and I have a working class that listenes
to the serial port's data received event.
I want to implement a WaitForResponse method that will send a command to
the com port and wait for the completed reply (a CR terminates a reply).
I don't want to sit there and poll it in a loop, and sleeping seems
inefficient to me (how long to sleep?). Concerning the sleep, I'm assuming
here that the com port is running on a different thread so if I sleep my
thread it will continue - is that correct?
Here are the highlights of the serial port class:
_RXBuffer as string
Public Sub New()
AddHandler comPort.DataReceived, AddressOf myDataReceived
End Sub
Public Sub WriteData(ByVal buffer As String)
comPort.Write(buffer)
End Sub
Private Sub comPort_DataReceived(ByVal sender As Object, _
ByVal e As SerialDataReceivedEventArgs)
Dim buffer As String = comPort.ReadExisting()
_RXBuffer &= buffer
End Sub
I want to add:
Public Sub WaitForResponse(ByVal buffer As String)
comPort.Write(buffer)
'wait here until there is some data to inspect
'then test if data has a CR
'is so exit sub
End Sub
This code must have been written a million times by now. Either my google
skills are impaired or it's a closely guarded secret :0
Thanks,
kpg