AN197 Silicon_Laboratories, AN197 Datasheet - Page 4

no-image

AN197

Manufacturer Part Number
AN197
Description
Serial Communications Guide FOR THE Cp210x
Manufacturer
Silicon_Laboratories
Datasheet
AN197
4. Transmitting Data Across the COM Port
Once the COM port is successfully opened and configured, data can be written or read.
4.1. Writing Data
There are several things that need to happen in a write, so it is a good idea to create a function for the writes to be
called whenever a write must occur. Here is an example of a write function:
bool WriteData(HANDLE handle, BYTE* data, DWORD length, DWORD* dwWritten)
{
}
The parameters passed in to this function are the handle to an open COM port, a pointer to an array of bytes that
will be written, the number if bytes that are in the array, and a pointer to a variable to store and return the number of
bytes written.
Two local variables are declared at the beginning of the function: a bool named success that will store the success
of the write (this is initialized to false, and only set true when the write succeeds) and an overlapped object o which
is passed to the WriteFile() function and alerts if the transfer is complete or not (this is always initialized to {0}
before the hEvent is assigned). Creating an event with the CreateEvent(NULL, FALSE, FALSE, NULL) function
sets the hEvent property of o to prepare it to be passed to the WriteFile() function (more information on
CreateEvent()
createevent.asp).
Next, the WriteFile() function is called with the handle, data, length of the data, and variable to store the amount of
data that was written (more information on WriteFile() is located at
default.asp?url=/library/en-us/fileio/base/writefile.asp). If this function returns successfully, a non-zero value is
returned. If the function fails, it returns 0. The if statement will determine if the write succeeded and if it did not, the
last error is retrieved to see if there really was an error or the write just wasn't finished. If ERROR_IO_PENDING is
returned then object o is then waited on until either the write finishes or fails (if something other than
ERROR_IO_PENDING is returned by the GetLastError() function, then there is the possibility of surprise removal;
see “8. Application Design Notes” for comments on surprise removal). When the wait is over, the result is obtained
so that the amount of bytes written is updated. The success variable will then be assigned with the appropriate
value, and the handle of o.hEvent is closed. Then the amount of bytes written is checked, and finally the function
returns the success of the write, which will be true if the write successfully completed.
4
bool success= false;
OVERLAPPED o= {0};
o.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
if (!WriteFile(handle, (LPCVOID)data, length, dwWritten, &o))
{
}
else
if (*dwWritten != length)
CloseHandle(o.hEvent);
return success;
if (GetLastError() == ERROR_IO_PENDING)
success = true;
success = false;
if (WaitForSingleObject(o.hEvent, INFINITE) == WAIT_OBJECT_0)
if (GetOverlappedResult(handle, &o, dwWritten, FALSE))
success = true;
is
located
at
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/
Rev. 0.6
http://msdn.microsoft.com/library/

Related parts for AN197