Нашел из какого-то старого проекта кусок.
Инициализация:
Код
#define BUFSIZE 200
HANDLE Comm;
char Data[BUFSIZE];
Comm = CreateFile(
"COM1", // pointer to name of the file
GENERIC_READ|GENERIC_WRITE, // access (read-write) mode
0, // share mode
0, // pointer to security attributes
OPEN_EXISTING, // how to create
FILE_ATTRIBUTE_NORMAL, // file attributes
0 // handle to file with attributes to copy
);
BuildCommDCB(
"baud=115200 parity=N data=8 stop=1", // pointer to device-control string
&CommDCB // pointer to device-control block
);
CommDCB.fOutX = true;
CommDCB.fOutxDsrFlow = false;
CommDCB.fDtrControl = DTR_CONTROL_DISABLE;
CommDCB.fDsrSensitivity = false;
CommDCB.fRtsControl = RTS_CONTROL_DISABLE;
CommDCB.StopBits = ONESTOPBIT;
CommDCB.XonChar = 'Q'-0x40;
CommDCB.XoffChar = 'S'-0x40;
CommDCB.EofChar = -1;
CommTO.ReadIntervalTimeout = 0;
CommTO.ReadTotalTimeoutMultiplier = 0;
CommTO.ReadTotalTimeoutConstant = 100;
CommTO.WriteTotalTimeoutMultiplier = 0;
CommTO.WriteTotalTimeoutConstant = 0;
i = SetCommTimeouts(
Comm, // handle to communications device
&CommTO // address of communications time-out structure
);
i = SetCommState(
Comm, // handle to communications device
&CommDCB // address of device-control block structure
);
затем периодически вызывать это
Код
DWORD i;
ReadFile(Comm, Data, BUFSIZE, &i, 0);
if(i!=0)
{
пришло i байтов данных, обрабатываем
}