Docstoc

serial programming-Writing to a Serial Port

Document Sample
serial programming-Writing to a Serial Port Powered By Docstoc
					Writing to a Serial Port

The WriteFile function transfers data through the serial connection to another device.

Before calling this function, an application must open and configure a serial port.

Because Windows CE does not support overlapped I/O, the primary thread or any thread that
creates a window should not try to write a large amount of data to a serial port. Such threads are
blocked and cannot manage message queues.

An application can simulate overlapped I/O by creating multiple threads to handle read/write
operations. To coordinate threads, an application calls the WaitCommEvent function to block
threads until specific communication events occur.

For more information about communication events, see Using Communication Events.

To write to a serial port

     1. Pass the port handle to the WriteFile function in the hFile parameter.

        The CreateFile function returns this handle when an application opens a port.

     2. Specify a pointer to the data to be written in lpBuffer.

        Often this data is binary data or a character array.

     3. Specify the number of bytes to write in nNumberOfBytesToWrite.

        The entire buffer can be passed to the driver.

     4. Specify in lpNumberOfBytesWritten a pointer to a location where WriteFile will store the
        number of bytes actually written; then look at this location to determine if the data
        transferred.
     5. Be sure that lpOverlapped is NULL.

The following code example shows how to transfer data using the WriteFile function.

DWORD dwError,
      dwNumBytesWritten;

WriteFile (hPort,                        //   Port handle
           &Byte,                        //   Pointer to the data to write
           1,                            //   Number of bytes to write
           &dwNumBytesWritten,           //   Pointer to the number of bytes
                                         //   written
               NULL                      //   Must be NULL for Windows CE
);

				
DOCUMENT INFO
Shared By:
Stats:
views:66
posted:7/31/2010
language:English
pages:1
Chandra Sekhar Chandra Sekhar http://
About My name is chandra sekhar, working as professor