HANDLE hCom;
hCom = CreateFile( "\\\\.\\COM1", //COM1
GENERIC_READ | GENERIC_WRITE,
0, // comm devices must be opened w/exclusive-access
NULL, // no security attributes
OPEN_EXISTING, // comm devices must use OPEN_EXISTING
0, // not overlapped I/O
NULL // hTemplate must be NULL for comm devices
);
if(hCom == INVALID_HANDLE_VALUE)
{
//error
}
写端口:
DWORD dwWritten;
char buf[1024]="date you want to send";
if(!WriteFile(hCom,buf,strlen(buf),&dwWritten,NULL)) {
// error
}
FlushFileBuffers(hCom);
printf("%d bytes written\n", dwWritten);