我想使用C ++在com端口中发送命令,但这不起作用
HANDLE hComm = CreateFile(L"\\\\.\\COM1", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
unsigned char data[5]{0x02, 0x44, 0x43, 0x03, 0x06};
unsigned char data2[1]{0x05};
DWORD dwWritten;
dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
dcbSerialParams.BaudRate = CBR_9600;
dcbSerialParams.ByteSize = 8;
dcbSerialParams.StopBits = ONESTOPBIT;
dcbSerialParams.Parity = NOPARITY;
SetCommState(hComm, &dcbSerialParams);
WriteFile(hComm, &data, (int) sizeof(data), &dwWritten, NULL);
WriteFile(hComm, &data2, (int) sizeof(data2), &dwWritten, NULL);
DWORD bytesRead[10];
ReadFile(hComm, &input, 5, bytesRead, NULL);
CloseHandle(hComm);
最佳答案
Qt包含一个处理串行端口的模块,该模块比Win32 API易于使用。有关更多信息,请参见https://doc.qt.io/qt-5/qtserialport-index.html。
关于c++ - 无法将数据发送到com端口,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53574601/