配置
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets serialport
1 2 3
| #include <QSerialPort> #include <QSerialPortInfo> #include <QDebug>
|
初始化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| int QWidgetSerialTx::SerialInit(void) { qDebug()<<"SerialInit"; foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) { qDebug()<<"Name:"<<info.portName(); qDebug()<<"Description:"<<info.description(); qDebug()<<"Manufacturer:"<<info.manufacturer(); qDebug()<<"Serial Number:"<<info.serialNumber(); } com = new QSerialPort(); com->setPortName(this->comName); if(!com->open(QIODevice::ReadWrite)) { qDebug()<<"Serial open failed!"; return -1; } else qDebug()<<"Serial open success:"<<com->portName(); com->setBaudRate(QSerialPort::Baud115200,QSerialPort::AllDirections); com->setDataBits(QSerialPort::Data8); com->setFlowControl(QSerialPort::NoFlowControl); com->setParity(QSerialPort::NoParity); com->setStopBits(QSerialPort::OneStop);
return 0; }
|
发送接收
1 2 3 4 5
| qint64 write(const char *data, qint64 len); qint64 write(const char *data);
QByteArray read(qint64 maxlen); QByteArray readAll();
|