0

I'm writing program that should control a piece of scientific hardware over COM-port. The program itself is written in wxWidgets and uses ctb library. To test, it before I connect it to 300kâ‚Ĵ equipment, I use com0com (Null-modem emulator) to forward COM2 port. To emulate my hardware I use wxTerminal (COM3). Altogether it works nice. One can debug not only in VS or DB but also see the whole data transfer in wxTerminal.

Now to my problem. I use to send data to COM-port ctb::SerialPort::Write() function.

device->Write( (char*)line.c_str(), line.size() );

However, if I disconnect the connection on the side of wxTerminal (i.e. COM2->NULL) than program hangs in this function.

It's obvious that I should add some function to test if my equipment is still there, but to do it I need to send data-packet to it and expect some answer. So I'm back to the Write().

"Just in case" I've also tried ctb::IOBase::Writev (char ∗ buf, size_t len, unsigned int timeout_in_ms) with timeout set to 100ms and I've still got program hanging in the same line. It's actually expected behavior as in this case timeout means only that the connection line is blocked till whole buffer is transferred or timeout is reached.

Connecting of wxTerminal to COM3 leads to un-freezing of debugger or stand-alone program. The Sun is shining, the birds are singing.

Can somebody give me a hint how to overcome my problem? I'd appreciate if comments would be restrained to wxWidgets-world - I really do not want to re-write whole program with other toolkit.

Kris_R
  • 2,738
  • 3
  • 20
  • 21
  • This is not a well-known library; you might get better support if you contact the author. Perhaps you could try a different serial port library. Boost.Asio (http://www.boost.org/doc/libs/release/doc/html/boost_asio.html) has support for serial ports. See this question (http://stackoverflow.com/questions/267752/boost-asio-serial-port-need-help-with-io) for a link to an example (in one of the answers). – Emile Cormier Jan 06 '12 at 19:39
  • Odd, everybody thinks something happy should happen when they jerk a connector out of a serial port while it is powered and in use. But they never jerk a running hard drive and expect *that* to work. Chase the timeout argument implementation, it is no doubt meant to detect this. Or use the connector screws. – Hans Passant Jan 06 '12 at 19:49

1 Answers1

0

If you COM port library does not provide effective timeouts on write block, (presumably because of hardware flow-control), you could implement your own by threading off the write. You could use a couple of events/semaphores/condvar/whatever. One to signal to the thread that there is something in a buffer to send and another that you can wait on with a timeout that is signaled by the thread after it has sent the buffer. If the 'ack' wait times out, your COM port is stuck and you can pop up some 'Check cable' messageBox. I don't know what other calls your port lib supports, so I don't know how you could implement flushes/retries.

Martin James
  • 24,453
  • 3
  • 36
  • 60