I have opened COM1 for I/O and need to input data. How do I check for data without being stuck if there if the port has not received any data?

The compiler is lcc-win32 running under Windows XP.


Recommended Answers

All 6 Replies

Why don't you create a separate thread to read and wait for data in the COM port? When data is available, you can pass it to the parent process using a callback function. I think that is the easiest. This is called Multi-threaded IO. The thread that you are doing the IO read/write is waiting for the data. But the other threads will continue execution. You do the GUI updates in your main thread and the Port reading in the secondary thread.

There is another method called Overlapped I/O. You do this by setting the overlapped attribute of the file handle that reads the COM port. This is done by specifying the FILE_FLAG_OVERLAPPED flag on the CreateFile or other call that creates the file handle. See the Win32 API function references for CreateFile, ReadFile, WriteFile, and GetOverlappedResult.

re: Why don't you create a separate thread ...

Because I have never done that and mostly because I have a simple application. I need to emulate a simple piece of hardware that is sometimes not available. It uses serial I/O and a few discrete signals so I cannot get stuck checking either one.

I haven't done any Windows GUI codeing either, but I guess I had better think about that for a while.

Meanwhile, is there a simple call I can use to form the basis of function bool is_character_waiting( com_port);

Thanks for your time.

Meanwhile, is there a simple call I can use to form the basis of function bool is_character_waiting( com_port);

Not that I know of. But I always use the File management functions, so I have not looked for anything else.

What function are you using to read the Serial Port now?

even if there was a function like is_character_waiting, you will always have to go back and check it's return value. This is like looping back on a blocking function until data is read. So nothing will be achieved. The easiest way is to use a separate thread. Check the Windows API reference for CreateThread or _beginthread. It is not as difficult as it sounds to be. I have heard that beginners find _beginthread easier.

Re: So nothing will be achieved.

Not so at all. A few years ago I wrote the software to drive a remote access controller with serial I/O, several card readers, A/D input, and discrete I/O (parellel I/O) under MS-DOS. I used a PC-104 built by Arcom and their freebe compiler. Using their custom calls to check a port, I wrote this in a single thread and it was reliable and fairly fast. And I never had to debug the thing 'cause it worked right the first time.

Now I need to do some I/O and just need a simple way of asking: Any data there?

You are probably right about multithreading, but I am not certain I want to invest that much time in this project. I will take a look at your links and see what is there.

thanks for the time you took to respond.

BTW: This controller had five serial I/O ports. (One four port card and the standard Com1 on the PC-104 computer board) One each for two magnetic card readers, one for a console terminel to monitor operations (vt-220 terminal), one for communications with the master controller, and the last was encrypted communications with the master controller. I checked each port in turn and if it had data, I read it and dealt with it. It did a lot of I/O and never had a problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.