Serial 9-Pin programming

Hi,

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?

Background:

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.
BrentCAMAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

grg99Commented:
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.

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

http://www.sssm.com/howtos/win422.html

...S
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

SteHCommented:
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.
0
_nn_Commented:
>> 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.
0
BrentCAMAuthor Commented:
Hi,

Thanks for all of your quick responses.

grg99:
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.

snehanshu:
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!

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

_nn_:
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.
0
SteHCommented:
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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
_nn_Commented:
>> 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 :

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

In conclusion, SteH's proposal is the only practicable one.
0
BrentCAMAuthor Commented:
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!
0
snehanshuCommented:
Thanks for nothing!
:)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.