Programming Applications for PC Serial Ports

Posted by hob Mon, 10 Sep 2007 12:28:00 GMT
1. The Problem

On the serial ports of PCs, asynchronous communication is mostly used. This means, every piece of information, which is normally one byte with 7 or 8 bits of information, is sent individually. There are start-bits at the beginning of each character and there are stop-bits at the end of each character. Information sent mostly contains multiple characters, forming a block.
The problem now is, that neither the hardware nor the software driver can know how many characters form such a block.
We discuss here how applications handle this type of traffic.

2. Applications We Have Seen

We have seen applications of other vendors and how they work. The have a buffer of a certain length, for example two kilobytes. They set a very short timeout and receive data in a loop.
In Windows, ReadFile() is done, giving the buffer and its entire length.
The ReadFile() returns when either the entire buffer has been filled by the driver or when the timeout has elapsed.
Now in the loop, there is mostly no data received, and the ReadFile() returns after the short timeout saying it has received nothing.
So these applications constantly give a certain load to the machine they are running on. Another disadvantage of this technique is, that there is also a delay before the application gets the data that have been received, because the buffer normally does not get filled; there are just some bytes received.
This delay is theoretically half of the time of the timeout set.

3. HOB Approach for Receiving Data with the Serial Interface

In the HOB approach, the serial interface is set for no timeout, or a very long timeout. When data is received by the application, ReadFile() is started, giving a buffer of one byte length. Then the ReadFile() operation returns each and every time a single character has been received, and only then.
During times when no data is received over the serial line, this application does not consume any CPU cycles.
When data are received, CPU load is higher, since there is a context switch for every byte received. When data are received, they can be immediately processed by the application without any delay.

no comments |

You must be registered in order to write comments. To register as a new user click here.

If you're already registered, please leave a comment here

Leave a comment

tp://')) %>
Powered by typo