工作中,我们有时候会用到串口通信,这里给分享一个串口通信库SerialPortCommonLib,
使用实例如下所示:
//打开串口与发送数据:
const char *portName = "Com27";
m_port.init(portName);
m_port.open();
if (m_port.isOpen())
{
CString strMsg = L"AT";
m_port.writeData(strMsg, strMsg.GetLength());
}
m_port.close();
//接收数据
void CSerialPortSampleDlg::onReadEvent(const char *portName, unsigned int readBufferLen)
{
//接受数据
char *data = new char[readBufferLen + 1];
m_port.readData(data, readBufferLen);
data[readBufferLen] = '\0';
delete[]data;
data = nullptr;
}
下载地址: