Solved

Serial 9-Pin programming

Posted on 2003-10-21
10
1,290 Views
Last Modified: 2012-05-04
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.
0
Comment
Question by:BrentCAM
  • 3
  • 2
  • 2
  • +2
10 Comments
 
LVL 22

Expert Comment

by:grg99
Comment Utility
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
 
LVL 5

Expert Comment

by:snehanshu
Comment Utility
0
 
LVL 5

Expert Comment

by:snehanshu
Comment Utility
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
 
LVL 13

Expert Comment

by:SteH
Comment Utility
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
 
LVL 16

Expert Comment

by:_nn_
Comment Utility
>> 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:BrentCAM
Comment Utility
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
 
LVL 13

Accepted Solution

by:
SteH earned 50 total points
Comment Utility
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
 
LVL 16

Assisted Solution

by:_nn_
_nn_ earned 50 total points
Comment Utility
>> 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
 

Author Comment

by:BrentCAM
Comment Utility
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
 
LVL 5

Expert Comment

by:snehanshu
Comment Utility
Thanks for nothing!
:)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now