首先,在工程文件里面,
QT += serialport
在头文件里面,
#include
1、配置打开串口
QSerialPort* myserial = new QSerialPort():
this->myserial->setPortName("COM1");//待打开的串口号
this->myserial->setBaudRate(9600,QSerialPort::AllDirections);//设置波特率;QSerialPort::AllDirections:双工
this->myserial->setDataBits(QSerialPort::Data8);//数据位
this->myserial->setParity(QSerialPort::NoParity);//校验位
this->myserial->setStopBits(QSerialPort::OneStop);//停止位
this->myserial->setFlowControl(QSerialPort::NoFlowControl);//流控制
this->myserial->open(QIODevice::ReadWrite);//打开串口
2、发送数据
char test[5] = {0x01,0x02,0x03,0x04,0x05};
myserial.write(test);//发送
3、接收数据
定义一个函数作为槽函数,当接收缓冲区有数据的时候回调此函数。
void ReadBuf()
{
QByteAr