在 Window CE 程式終端機的程式碼樣本
// Window CE 5.0 Sample code for LXE Window CE terminal to send data out of COM port 1
//Serial port handler, use COM1
HANDLE hComm = CreateFile(_T("COM1:"),
GENERIC_READ | GENERIC_WRITE,
0, NULL, OPEN_EXISTING, 0, NULL );
//Serial port setting 9600, N, 8, 0
DCB commParam;
INT portNo = 1;
INT baud = 9600;
INT parity = NOPARITY;
INT databits = 8;
INT stopbits = 0;
commParam.BaudRate = baud;
commParam.fBinary = TRUE;
commParam.fParity = TRUE;
commParam.ByteSize = databits;
commParam.Parity = NOPARITY;
commParam.StopBits = stopbits;
commParam.fOutxCtsFlow = FALSE;
commParam.fOutxDsrFlow = FALSE;
commParam.fDtrControl = DTR_CONTROL_ENABLE;
commParam.fDsrSensitivity = FALSE;
commParam.fTXContinueOnXoff = TRUE;
commParam.fOutX = FALSE;
commParam.fInX = FALSE;
commParam.fErrorChar = FALSE;
commParam.fNull = FALSE;
commParam.fRtsControl = RTS_CONTROL_ENABLE;
commParam.fAbortOnError = FALSE;
if (!SetCommState(hComm, &commParam))
{
return FALSE;
}
COMMTIMEOUTS CommTimeOuts;
GetCommTimeouts(hComm, &CommTimeOuts);
CommTimeOuts.ReadIntervalTimeout = MAXDWORD;
CommTimeOuts.ReadTotalTimeoutMultiplier = 0;
CommTimeOuts.ReadTotalTimeoutConstant = 0;
CommTimeOuts.WriteTotalTimeoutMultiplier = 10;
CommTimeOuts.WriteTotalTimeoutConstant = 1000;
if(!SetCommTimeouts(hComm, &CommTimeOuts))
{
return FALSE;
}
SetCommMask (hComm, EV_RXCHAR);
SetupComm(hComm, 512,512);
PurgeComm(hComm,PURGE_TXCLEAR | PURGE_RXCLEAR);
// Following code sends Chinese character to LED
//create variable for store GB char, six bytes, three Chinese words
char *string1 = "******";
//测 , HEX is B2E2, DEC is 0178, 0226
string1[0] = (char)178;
string1[1] = (char)226;
//试 , HEX is CAD4, DEC is 0202, 0212
string1[2] = (char)202;
string1[3] = (char)212;
//中 , HEX is D6D0, DEC is 0214, 0208
string1[4] = (char)214;
string1[5] = (char)208;
//trigger to output
char *end = "*";
end[0] = (char)13;
DWORD dwNumBytesWritten;
//Display Chinese GB, the GB char stored on string1.
ASSERT (hComm != INVALID_HANDLE_VALUE);
WriteFile (hComm,
string1,
sizeof(string1),
&dwNumBytesWritten,
NULL);
//output integer“13” to trigger LED display the data
WriteFile (hComm,
end,
1,
&dwNumBytesWritten,
NULL);
//Close serial port
CloseHandle(hComm);
// Following code sends numeric character to LED
//Serial port handler, use COM1
HANDLE hComm = CreateFile(_T("COM1:"),
GENERIC_READ | GENERIC_WRITE,
0, NULL, OPEN_EXISTING, 0, NULL );
//Serial port setting
DCB commParam;
INT portNo = 1;
INT baud = 9600;
INT parity = NOPARITY;
INT databits = 8;
INT stopbits = 0;
commParam.BaudRate = baud;
commParam.fBinary = TRUE;
commParam.fParity = TRUE;
commParam.ByteSize = databits;
commParam.Parity = NOPARITY;
commParam.StopBits = stopbits;
commParam.fOutxCtsFlow = FALSE;
commParam.fOutxDsrFlow = FALSE;
commParam.fDtrControl = DTR_CONTROL_ENABLE;
commParam.fDsrSensitivity = FALSE;
commParam.fTXContinueOnXoff = TRUE;
commParam.fOutX = FALSE;
commParam.fInX = FALSE;
commParam.fErrorChar = FALSE;
commParam.fNull = FALSE;
commParam.fRtsControl = RTS_CONTROL_ENABLE;
commParam.fAbortOnError = FALSE;
if (!SetCommState(hComm, &commParam))
{
return FALSE;
}
COMMTIMEOUTS CommTimeOuts;
GetCommTimeouts(hComm, &CommTimeOuts);
CommTimeOuts.ReadIntervalTimeout = MAXDWORD;
CommTimeOuts.ReadTotalTimeoutMultiplier = 0;
CommTimeOuts.ReadTotalTimeoutConstant = 0;
CommTimeOuts.WriteTotalTimeoutMultiplier = 10;
CommTimeOuts.WriteTotalTimeoutConstant = 1000;
if(!SetCommTimeouts(hComm, &CommTimeOuts))
{
return FALSE;
}
SetCommMask (hComm, EV_RXCHAR);
SetupComm(hComm, 512,512);
PurgeComm(hComm,PURGE_TXCLEAR | PURGE_RXCLEAR);
//Display number "0123456789"
WriteFile (hComm,
"0123456789",
sizeof("0123456789"),
&dwNumBytesWritten,
NULL);
//output to LED
WriteFile (hComm,
end,
1,
&dwNumBytesWritten,
NULL);
//Close serial port
CloseHandle(hComm);
// Following code sends alpha character to LED
//Serial port handler, use COM1
HANDLE hComm = CreateFile(_T("COM1:"),
GENERIC_READ | GENERIC_WRITE,
0, NULL, OPEN_EXISTING, 0, NULL );
//Serial port setting
DCB commParam;
INT portNo = 1;
INT baud = 9600;
INT parity = NOPARITY;
INT databits = 8;
INT stopbits = 0;
commParam.BaudRate = baud;
commParam.fBinary = TRUE;
commParam.fParity = TRUE;
commParam.ByteSize = databits;
commParam.Parity = NOPARITY;
commParam.StopBits = stopbits;
commParam.fOutxCtsFlow = FALSE;
commParam.fOutxDsrFlow = FALSE;
commParam.fDtrControl = DTR_CONTROL_ENABLE;
commParam.fDsrSensitivity = FALSE;
commParam.fTXContinueOnXoff = TRUE;
commParam.fOutX = FALSE;
commParam.fInX = FALSE;
commParam.fErrorChar = FALSE;
commParam.fNull = FALSE;
commParam.fRtsControl = RTS_CONTROL_ENABLE;
commParam.fAbortOnError = FALSE;
if (!SetCommState(hComm, &commParam))
{
return FALSE;
}
COMMTIMEOUTS CommTimeOuts;
GetCommTimeouts(hComm, &CommTimeOuts);
CommTimeOuts.ReadIntervalTimeout = MAXDWORD;
CommTimeOuts.ReadTotalTimeoutMultiplier = 0;
CommTimeOuts.ReadTotalTimeoutConstant = 0;
CommTimeOuts.WriteTotalTimeoutMultiplier = 10;
CommTimeOuts.WriteTotalTimeoutConstant = 1000;
if(!SetCommTimeouts(hComm, &CommTimeOuts))
{
return FALSE;
}
SetCommMask (hComm, EV_RXCHAR);
SetupComm(hComm, 512,512);
PurgeComm(hComm,PURGE_TXCLEAR | PURGE_RXCLEAR);
//Display char "ABCDEF"
WriteFile (hComm,
"ABCDEF",
sizeof("ABCDEF"),
&dwNumBytesWritten,
NULL);
//output to LED
WriteFile (hComm,
end,
1,
&dwNumBytesWritten,
NULL);
//Close serial port
CloseHandle(hComm);
//End of Sample code |