locked
SerialPort Class RRS feed

  • Question

  • I have an application that uses serial communication to control a machine. When I suddenly send commands to oft, I understand that the buffer may become full and I might not be able to send anymore data. I've noticed that, because I have implemented some exception handling and I always catch a TimeOutException when I try to write to the serial port. The Timeout value must be 5 ms, due to the properties of the machine and I am not thinking about changing it.

    Anyways, once I catch a TimeOutException, I am not able to write to the serial port anymore, even if I repeat trying. Since the first time, the program keeps catching TimeOutExceptions undefinetely. If I restart the program, without restarting the machine, it works fine. That means, that the problem can be solve in my program without the need to start again the machine.

    How can I continue writing to the serial port, without having to restart the program?

     

    Monday, July 17, 2006 2:09 PM

Answers

  • Maybe you should consider a Timeout exception a catastrophic failure.  You'd never know how much data actually got written.  Sending data to the machine at a rate that is higher than the serial port can send it doesn't make sense.  Claiming you can't deal with a 5 msec timeout when you know the serial port can send it fast enough doesn't either.  At 9600 baud, sending more than 5 characters already takes longer than 5 msec.  To avoid the time-outs, either pace the rate you send data or make the transmit buffer larger...
    Tuesday, July 18, 2006 1:11 AM

All replies

  • Maybe you should consider a Timeout exception a catastrophic failure.  You'd never know how much data actually got written.  Sending data to the machine at a rate that is higher than the serial port can send it doesn't make sense.  Claiming you can't deal with a 5 msec timeout when you know the serial port can send it fast enough doesn't either.  At 9600 baud, sending more than 5 characters already takes longer than 5 msec.  To avoid the time-outs, either pace the rate you send data or make the transmit buffer larger...
    Tuesday, July 18, 2006 1:11 AM
  • I will consider what you said about increasing the buffer size. However, that only reduces the risk but it does not eliminate it.

    Another thing, every time that I send a chain of characters (they are not long), I always expect an answer from the machine, in order to take into consideration the checksum and so on. That means I always know how much data actually got written.

    Nevertheless, my real doubt is, how can I recover from a TimeOutException. I have used try{} and catch{} and it perfectly avoids the program from crashing. But once I get a TimeOutException, the program keeps catching that exception everytime that I try to write in the serialport. How can I continue sending characters?

    Tuesday, July 18, 2006 5:35 AM