How to add an event handler for serial port timeout?
-
I have searched high and low for an answer to this one. I'm sure someone has run into a similar situation. Basically, I set up a serial port with a ReadTimeout of 10 seconds; open it; send a "command" character to a serially attached device and expect an answer within this period of time. If data is received, the serialPort_DataReceived event handler fires and reads the data. However, I want to be able to notify the operator of a problem, should the external device not reply within 10 seconds. This could indicate the device is not attached or not powered up. Appreciate the help, Steve.
-
I have searched high and low for an answer to this one. I'm sure someone has run into a similar situation. Basically, I set up a serial port with a ReadTimeout of 10 seconds; open it; send a "command" character to a serially attached device and expect an answer within this period of time. If data is received, the serialPort_DataReceived event handler fires and reads the data. However, I want to be able to notify the operator of a problem, should the external device not reply within 10 seconds. This could indicate the device is not attached or not powered up. Appreciate the help, Steve.
thompsons wrote:
I have searched high and low
Not high and low enough then. It took me 10 seconds to find this[^] The SerialPort class provides a timeout mechanism on its Read methods. When the read timeout is exceeded, a TimeoutException gets thrown. :)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
-
thompsons wrote:
I have searched high and low
Not high and low enough then. It took me 10 seconds to find this[^] The SerialPort class provides a timeout mechanism on its Read methods. When the read timeout is exceeded, a TimeoutException gets thrown. :)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
Thanks for the reply. I also found that; it did not directly answer my question. This implies that I need to make a call to serialPort.ReadLine() before any notification occurs in the case of a timeout. One of the reasons folks join these forums is because they want answers in plain English and not the somewhat vague documentation offered on MSDN. Sorry I asked such a silly question that caused your sarcasm to manifest itself. Regards, Steven.
-
Thanks for the reply. I also found that; it did not directly answer my question. This implies that I need to make a call to serialPort.ReadLine() before any notification occurs in the case of a timeout. One of the reasons folks join these forums is because they want answers in plain English and not the somewhat vague documentation offered on MSDN. Sorry I asked such a silly question that caused your sarcasm to manifest itself. Regards, Steven.
The quality of the question directly dictates the quality of the answer. If you don't start a read operation, you're not going to get a notification that anything showed up. What if your code isn't ready to recieve yet, but there's pending data? That's why it works this way.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008 -
Thanks for the reply. I also found that; it did not directly answer my question. This implies that I need to make a call to serialPort.ReadLine() before any notification occurs in the case of a timeout. One of the reasons folks join these forums is because they want answers in plain English and not the somewhat vague documentation offered on MSDN. Sorry I asked such a silly question that caused your sarcasm to manifest itself. Regards, Steven.
So you want to get an event because data that you did not request did not arrive in time? That is not how Windows (or any other OS I am aware of) works. You can get an event when something unexpected happens (e.g. DataReceived, ErrorReceived) or you can get an Exception when something you did order did not succeed for some reason. If you are not satisfied by the DataReceived events (and I can imagine several scenarios where it is insufficient), then you must create your own thread and have that perform synchronous read operations, with or without timeout. :)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
-
I have searched high and low for an answer to this one. I'm sure someone has run into a similar situation. Basically, I set up a serial port with a ReadTimeout of 10 seconds; open it; send a "command" character to a serially attached device and expect an answer within this period of time. If data is received, the serialPort_DataReceived event handler fires and reads the data. However, I want to be able to notify the operator of a problem, should the external device not reply within 10 seconds. This could indicate the device is not attached or not powered up. Appreciate the help, Steve.
My normal way to do SerialPort uses an AutoResetEvent to do the timeout control. The general format is something like this:
dim port as SerialPort
dim ReceiveResponse as AutoResetEventSub SendCommand(data() as Byte)
sendTries = 0while sendTries <= MaxTries
sendTries += 1
port.Write(data)
if ReceiveResponse.Wait
'you got a response
else
'you didn't get a response in time
end if
end while
End Subsub PortDataReceivedHandler(sender as object, e as DataReceivedEventArgs)
if processIncomingData() then
ReceiveResponse.Set()
end
end subObviously, this is missing a lot of actual code, but it should point you in the right direction.