HANDLE hCom;
hCom = CreateFile(_T("COM3"),//COM1口
GENERIC_READ|GENERIC_WRITE, //允许读和写
0, //独占方式
NULL,
OPEN_EXISTING, //打开而不是创建
0, //同步方式
NULL);
if(hCom == INVALID_HANDLE_VALUE)
{
return FALSE;
}
DCB dcb;
GetCommState(hCom, &dcb);
//波特率 110 300 600 1200 2400 4800 9600 14400 19200 38400 56000 57600 115200 128000 256000
dcb.BaudRate = CBR_9600;
//数据位 5 6 7 8
dcb.ByteSize = 8;
//校验位
//none无校验 NOPARITY
//even偶校验 EVENPARITY
//odd奇校验 ODDPARITY
//mark符号校验 MARKPARITY
//space空格校验 SPACEPARITY
dcb.Parity = NOPARITY;
//停止位
//1位 ONESTOPBIT
//1.5位 ONE5STOPBITS
//2位 TWOSTOPBITS
dcb.StopBits = ONESTOPBIT;
if (!SetCommState(hCom, &dcb))
{
return FALSE;
}
COMMTIMEOUTS TimeOut;
memset(&TimeOut, 0, sizeof(TimeOut));
TimeOut.ReadIntervalTimeout = MAXDWORD;
Se
VC++:串口同步通信
最新推荐文章于 2025-04-29 09:32:26 发布