-
12.3.2.
Setting the Com Port Parameters
Function Description:
Com Port communication has a variety of different possible settings with regards to speed, data
correction, flow control, and more. It is necessary to match settings on both ends of the
communication for proper correspondence, and for RS485 it is necessary to be able to change the
RTS line between sending and receiving data. The best practice here is to gather the default
settings into a DCB data structure with GetCommState, then change the key items and reset the
port with SetCommTimeouts. See line 131 of the sample program and the Microsoft definition of the
DCB struct linked above.
Function call:
void GetCommState(HANDLE hFile, LPDCB lpDCB);
Parameters(Input):
hFile:
HANDLE
: the Handle to the com port returned by CreateFile.
Parameters(Output):
lpDCB:
LPDCB
: the DCB structure that the function will write all current settings to.
Function call:
void SetCommState(HANDLE hFile, LPDCB lpDCB);
Parameters(Input):
hFile:
HANDLE
: the Handle to the com port returned by CreateFile.
lpDCB:
LPDCB
: the DCB structure that the function will write all current settings to.
For additional control, the port's timeout settings can be altered with SetCommTimeouts and
GetCommTimeouts. This is done exactly like the State settings, except using a _COMMTIMEOUTS
data struct instead of DCB. See line 137 of the sample program for an example (this will not be
necessary for most applications.)
12.3.3.
Writing Data To The Com Port
Function Description:
Before writing the port must be set to transmit mode by raising the RTS pin. When that pin is low
the port is in receive mode. Direct control of the pin is possible through the Windows API
EscapeCommFunction. With the pin high, data can be sent through the port via the WriteFile
function. See line 222 of the sample program for an example.
Function call:
BOOL EscapeCommFunction (HANDLE hFile, DWORD dwFunc);
Parameters(Input):
hFile:
HANDLE
: The handle to the com port.
dwFunc:
DWORD
: defines what action to take. To raise the RTS line (getting ready
to transmit) use SETRTS, to lower it use CLRRTS (after transmitting to be ready to receive.)
Function call:
BOOL WriteFile (HANDLE hFile, LPCVOID lpBuffer, DWORD nNumberOfBytesToWrite,
LPDWORD lpNumberOfBytesWritten, LPOVERLAPPED lpOverlapped);
Parameters(Input):
hFile:
HANDLE
: Handle to the com port to write to.
lpBuffer:
LPCVOID
: Pointer to buffer containing data to write.
nNumberofBytesToWrite: DWORD
:
The
exact number of bytes you wish to
write from the buffer. Often this will be "_tcsclen(lpBuffer)" specific the length of the buffer is to be
written.
lpOverlapped:
LPOVERLAPPED
: Not Supported, set to Null.
Parameters(Output):
lpNumberOfBytesWritten:
LPDWORD
: pointer to a DWORD that will contain the
number of bytes written. Useful to verify that the operation was successful, 0 will indicate a failure.
Содержание MR650
Страница 1: ...MR650 Programming Manual V 1 12 1 16 2008 ...
Страница 7: ... 7 13 FUNCTION KEY SETTING ON REGISTRY 65 14 UPDATE NOTES 66 ...
Страница 16: ... it is for internal use send messages ...
Страница 33: ... Be aware to release handles when program ends ...