Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Serial 9-Pin programming

Posted on 2003-10-21
Medium Priority
Last Modified: 2012-05-04

I'm using Visual C++ 6.0 and the Win32 API (not MFC), and trying to detect if there is or is not a serial device connected to COM 1? How can this be achieved?


I have a Sony VTR connected to a PC via an RS232(PC COM1 port)/RS422(VTR RS422 In port) cable.  Sending and receiving data is done through the "WriteFile", "WaitCommEvent" (with the EV_RXCHAR mask), and "ReadFile" API commands in that order.  When the device is connected everything seems to work fine.  But when the device is off, "WriteFile" still suceeds writing the required number of bytes (I don't know why this would suceed if the device was off!) and the program hangs on the "WaitCommEvent" as it it never satisfied because no bytes are received.

How do I get around this problem?

How can I tell if a device (not a modem) is connected/on without the program hanging?

I've done a lot of research and still can't find the answer!

Many thanks in advance.
Question by:BrentCAM
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
LVL 22

Expert Comment

ID: 9595091
There's some API for reading the status of the handshaking lines.  Write a little display option in your program that continually displays the status if the incoming handshaking lines.   You should see some of them come up when you plug in the cable.


Expert Comment

ID: 9596634

Expert Comment

ID: 9596762
Or perhaps does this help? (To send some sony commands and see if you got the expected result)


Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

LVL 13

Expert Comment

ID: 9596999
I think when hardware handshaking is enabled for the port the write should time out when no device is connected. Just make sure the sony VTR uses hardware handshake.
LVL 16

Expert Comment

ID: 9597053
>> But when the device is off, "WriteFile" still suceeds writing the required number of bytes (I don't know why this would suceed if the device was off!)

WriteFile() returns success upon putting the passed bytes in the output queue of the serial driver, NOT when the bytes are actually sent. To make sure those bytes were sent, you could call ClearCommError() and check the cbOutQue field in the returned COMSTAT structure (there are other methods, like waiting for EV_TXEMPTY). But that wouldn't guarantee that the sent bytes have been correctly received on the other end, because if you've set up the link without any handshaking, the bytes will be sent anyway, and nobody will get them. That's one of the reasons why ...

>> and the program hangs on the "WaitCommEvent" as it it never satisfied because no bytes are received.

... you should never assume that something will come back. Use a timeout other than INFINITE and handle the case.

Author Comment

ID: 9599324

Thanks for all of your quick responses.

I looked at the API for reading the status of the handshaking
lines and used GetCommModemStatus.  This worked to tell if a
cable was plugged into COM 1 on the PC, but unfortunately doesn't
tell you if the device at the other end of the cable is switched
on, and that is what I know realised is what I I need to know.

Unfortunately the link you provided doesn't answer this question,
although I tried using the _stat function, but was no help.
The sssm.com link is useful but they don't mention what they used
to wait for a response to come back.  I might have to try and get
that info from them!

I have enabled hardware handshaking and it seems to support it
but unfortunately didn't make any diffence.

I didn't realise that cbOutQue held the number of bytes that
were actually sent.  I did the write, and then checked
cbOutQue using ClearCommError, but the value was still 0.
On a succesfull write this value is also 0.

I'm sure that this is not as difficult as I'm making out.  So
I suppose the question has been revised to: How can I tell if
a device at the end of the cable is on or off?

Many thanks.
LVL 13

Accepted Solution

SteH earned 200 total points
ID: 9599428
In that case I would suggest to send the bytes and wait for a predefined time and then llok whether the correct answer has arrived. The waiting should be done using WaitCommEvent with a timeout. In case of no timeout the data can be verified. Otherwise the device is off.
LVL 16

Assisted Solution

_nn_ earned 200 total points
ID: 9599617
>> I have enabled hardware handshaking and it seems to support it but unfortunately didn't make any diffence.

I think there's no way it can work, as RS422 doesn't seem to have means to handle any of the handshaking pins of the RS232 interface. See :

specifically http://www.rs422.com.ar/images/cable232_422.GIF

In conclusion, SteH's proposal is the only practicable one.

Author Comment

ID: 9599777
Ok, I'm new to serial programming, but what you've said makes sense to me.  WaitCommEvent unfortunately never times out and therefore I think the following I've just found on the net is the solution...

"The problem is that in this example the call to WaitCommEvent() will block (not return to the calling procedure) until there is something for it to read. "

"The workaround to this is to use overlapped I/O. In overlapped I/O the call to WaitCommEvent() won't block if there is no data - it will return immediately. However, you must create an event (message) by using the CreateEvent() API"

Many thanks to everybody that contributed!

Expert Comment

ID: 9599874
Thanks for nothing!

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question