User`s manual
ThinkCore DA-660 WinCE User’s Manual Programming Examples
5-4
if (rCom == NULL)
{
printf("Fail to create read port\n");
CloseHandle(wCom);
return 2;
}
/* for a thread to handle receiving */
if (CreateThread( NULL, 0, comReadThread, (LPDWORD) rCom, NULL, 0 )==NULL)
{
printf("Fail to create a receiving thread\n");
CloseHandle(wCom);
CloseHandle(rCom);
return 3;
}
for (i=0; i< MAX_DATA_LEN;i++)
buffer[i] = (unsigned char) ('a'+i%26);
PurgeComm(wCom,PURGE_TXCLEAR | PURGE_TXABORT);
while(loop++ < 100)
{
if (WriteFile(wCom, buffer, MAX_DATA_LEN, &rtn, NULL)==0)
{
printf("Fail to write\n");
break;
}
WaitForSingleObject(waitH, 100);
}
CloseHandle(wCom);
CloseHandle(rCom);
CloseHandle(waitH);
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPTSTR lpCmdLine, int nCmdShow)
{
comPair(L"COM3", L"COM4", 38400);
return 0;
}
Windows® API function CreateFile(…) opens a named file corresponding to a serial port. For any
serial port from “COM10” or after, the file name must be prefixed by $device\\. For “COM1” to
“COM9”, the $device\\ prefix is optional. API function GetCommState(..) is the standard function
to get the current parameters of a serial port, including its baudrate, data bits (bytesize), parity, and
stop bits. In contrast to GetCommState(), API function SetCommState(...) controls serial
communication by setting these parameters. Use other parameters including flow control, DTR
signal, and RTS signal to fine tune the serial port. Be sure to set the flow control correctly or else
you will lose data.