Qt 串口通讯
2016-06-28 10:25
417 查看
整理一下关于Qt串口通讯
程序下载地址:http://download.csdn.net/detail/u012166958/9561553
串口通讯使用的类为
QSerialPort Class
公共类型:
enum BaudRate { Baud1200, Baud2400, Baud4800, Baud9600, ..., UnknownBaud } //波特率
enum DataBits { Data5, Data6, Data7, Data8, UnknownDataBits } //数据位
enum Direction { Input, Output, AllDirections } //输入(输出)方向
flags Directions
enum FlowControl { NoFlowControl, HardwareControl, Software
enum Parity { NoParity, EvenParity, OddParity, SpaceParity, MarkParity, UnknownParity }//奇偶校验
enum PinoutSignal { NoSignal, TransmittedDataSignal, ReceivedDataSignal, DataTerminalReadySignal, ..., SecondaryReceivedDataSignal }
flags PinoutSignalsenum SerialPortError { NoError, DeviceNotFoundError, PermissionError, OpenError, ..., UnknownError }
enum StopBits { OneStop, OneAndHalfStop, TwoStop, UnknownStopBits } //停止位
函数
void QSerialPort::setPort(const QSerialPortInfo & serialPortInfo)
void QSerialPort::setPortName(const QString & name) //设置端口名称
qint32 baudRate(Directions directions = AllDirections) const
bool setBaudRate(qint32 baudRate, Directions directions = AllDirections)//设置波特率
DataBits
dataBits() const
bool setDataBits(DataBits dataBits) //数据位设置
Parity parity() const
bool setParity(Parity parity) //设置奇偶校验
StopBits stopBits() const
bool setStopBits(StopBits stopBits)/设置停止位
FlowControl <span style="font-family: Arial, Helvetica, sans-serif;">flowControl() const</span>
bool setFlowControl(FlowControl flowControl) //设置硬件流
bool QSerialPort::open(OpenMode mode)<span style="white-space:pre"> </span>//打开串口
打开方式
QIODevice::NotOpen 0x0000 The device is not open.
QIODevice::ReadOnly 0x0001 The device is open for reading.
QIODevice::WriteOnly 0x0002 The device is open for writing. Note that this mode implies Truncate.
QIODevice::ReadWrite ReadOnly | WriteOnly The device is open for reading and writing.
QIODevice::Append 0x0004 The device is opened in append mode so that all data is written to the end of the file.
QIODevice::Truncate 0x0008 If possible, the device is truncated before it is opened. All earlier contents of the device are lost.
QIODevice::Text 0x0010 When reading, the end-of-line terminators are translated to '\n'. When writing, the end-of-line terminators are translated to the local encoding, for example '\r\n' for Win32.
QIODevice::Unbuffered 0x0020 Any buffer in the device is bypassed. virtual
void close()//关闭
QByteArray QIODevice::readAll()//读函数
qint64 QIODevice::write(const char * data)
qint64 QIODevice::write(const QByteArray & byteArray)
qint64 QIODevice::write(const char * data, qint64 maxSize)//写函数
1、与编写网络一样在 .pro 文件中添加
QT += serialport
程序下载地址:http://download.csdn.net/detail/u012166958/9561553
串口通讯使用的类为
QSerialPort Class
公共类型:
enum BaudRate { Baud1200, Baud2400, Baud4800, Baud9600, ..., UnknownBaud } //波特率
enum DataBits { Data5, Data6, Data7, Data8, UnknownDataBits } //数据位
enum Direction { Input, Output, AllDirections } //输入(输出)方向
flags Directions
enum FlowControl { NoFlowControl, HardwareControl, Software
enum Parity { NoParity, EvenParity, OddParity, SpaceParity, MarkParity, UnknownParity }//奇偶校验
enum PinoutSignal { NoSignal, TransmittedDataSignal, ReceivedDataSignal, DataTerminalReadySignal, ..., SecondaryReceivedDataSignal }
flags PinoutSignalsenum SerialPortError { NoError, DeviceNotFoundError, PermissionError, OpenError, ..., UnknownError }
enum StopBits { OneStop, OneAndHalfStop, TwoStop, UnknownStopBits } //停止位
函数
void QSerialPort::setPort(const QSerialPortInfo & serialPortInfo)
void QSerialPort::setPortName(const QString & name) //设置端口名称
qint32 baudRate(Directions directions = AllDirections) const
bool setBaudRate(qint32 baudRate, Directions directions = AllDirections)//设置波特率
DataBits
dataBits() const
bool setDataBits(DataBits dataBits) //数据位设置
Parity parity() const
bool setParity(Parity parity) //设置奇偶校验
StopBits stopBits() const
bool setStopBits(StopBits stopBits)/设置停止位
FlowControl <span style="font-family: Arial, Helvetica, sans-serif;">flowControl() const</span>
bool setFlowControl(FlowControl flowControl) //设置硬件流
bool QSerialPort::open(OpenMode mode)<span style="white-space:pre"> </span>//打开串口
打开方式
QIODevice::NotOpen 0x0000 The device is not open.
QIODevice::ReadOnly 0x0001 The device is open for reading.
QIODevice::WriteOnly 0x0002 The device is open for writing. Note that this mode implies Truncate.
QIODevice::ReadWrite ReadOnly | WriteOnly The device is open for reading and writing.
QIODevice::Append 0x0004 The device is opened in append mode so that all data is written to the end of the file.
QIODevice::Truncate 0x0008 If possible, the device is truncated before it is opened. All earlier contents of the device are lost.
QIODevice::Text 0x0010 When reading, the end-of-line terminators are translated to '\n'. When writing, the end-of-line terminators are translated to the local encoding, for example '\r\n' for Win32.
QIODevice::Unbuffered 0x0020 Any buffer in the device is bypassed. virtual
void close()//关闭
QByteArray QIODevice::readAll()//读函数
qint64 QIODevice::write(const char * data)
qint64 QIODevice::write(const QByteArray & byteArray)
qint64 QIODevice::write(const char * data, qint64 maxSize)//写函数
1、与编写网络一样在 .pro 文件中添加
QT += serialport
2、使用的库
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>
</pre><pre name="code" class="html">#ifndef SERIALPORT_H #define SERIALPORT_H #include <QMainWindow> #include <QtSerialPort/QSerialPort> #include <QtSerialPort/QSerialPortInfo> #include <QByteArray> #include <QTimer> namespace Ui { class SerialPort; } class SerialPort : public QMainWindow { Q_OBJECT public: explicit SerialPort(QWidget *parent = 0); ~SerialPort(); private slots: void on_OpenPushButton_clicked(); void on_ClosePushButton_clicked(); void on_SendPushButton_clicked(); void Updates(); private: Ui::SerialPort *ui; QSerialPort *mySerialPort; QByteArray requestData; QTimer *timer; void sendData(); }; #endif // SERIALPORT_H #include "serialport.h" #include "ui_serialport.h" #include <QDebug> SerialPort::SerialPort(QWidget *parent) : QMainWindow(parent), ui(new Ui::SerialPort) { ui->setupUi(this); ui->baudRatecomboBox->setCurrentIndex(5); ui->DataBitsComboBox->setCurrentIndex(3); ui->parityComboBox->setCurrentIndex(0); ui->stopBitsComboBox->setCurrentIndex(0); foreach (const QSerialPortInfo &info,QSerialPortInfo::availablePorts()) { qDebug()<<"Name :"<<info.portName(); qDebug()<<"Description :"<<info.description(); qDebug()<<"Manufacturer:"<<info.manufacturer(); QSerialPort serial; serial.setPort(info); if(serial.open(QIODevice::ReadWrite)) { ui->ProtsComboBox->addItem(info.portName()); serial.close(); } } } SerialPort::~SerialPort() { mySerialPort->close(); delete ui; } void SerialPort::on_OpenPushButton_clicked() { mySerialPort = new QSerialPort(); qDebug()<<ui->ProtsComboBox->currentText(); mySerialPort->setPortName(ui->ProtsComboBox->currentText()); mySerialPort->open(QIODevice::ReadWrite); mySerialPort->setBaudRate(ui->baudRatecomboBox->currentText().toInt()); //数据位 int dataBitNumber = ui->DataBitsComboBox->currentText().toInt(); switch(dataBitNumber) { case 5: mySerialPort->setDataBits(QSerialPort::Data5); break; case 6: mySerialPort->setDataBits(QSerialPort::Data6); break; case 7: mySerialPort->setDataBits(QSerialPort::Data7); break; case 8: mySerialPort->setDataBits(QSerialPort::Data8); break; default: break; } // QSerialPort::NoParity; QSerialPort::Baud1200; mySerialPort->setParity(QSerialPort::NoParity);//无奇偶校验 if(ui->stopBitsComboBox->currentText() == "1") { mySerialPort->setStopBits(QSerialPort::OneStop); } else if(ui->stopBitsComboBox->currentText() =="1.5") { mySerialPort->setStopBits(QSerialPort::OneAndHalfStop); } else if(ui->stopBitsComboBox->currentText() =="2") { mySerialPort->setStopBits(QSerialPort::TwoStop); } mySerialPort->setFlowControl(QSerialPort::NoFlowControl); timer = new QTimer(this); connect(timer,SIGNAL(timeout()),this,SLOT(Updates())); timer->start(1000); } void SerialPort::on_ClosePushButton_clicked() { mySerialPort->close(); } void SerialPort::Updates() { qDebug()<<"this is a test!"; requestData = mySerialPort->readAll(); if(requestData !=NULL) { ui->datasTextEdit->append(requestData); } requestData.clear(); } void SerialPort::on_SendPushButton_clicked() { mySerialPort->write(ui->sendLineEdit->text().toLatin1()); }
相关文章推荐
- QT编程--监听USB设备拔出和插入。
- Qt程序发布到其他电脑上无法运行解决办法(二)
- Qt中QScrollArea类的简单使用心得
- Qt如何去掉按钮等控件的虚线框(焦点框)
- QT笔记之模态对话框及非模态对话框
- qt cef嵌入web(二)
- Qt5MVC模式(一)
- qt sleep
- qt qss
- qt 拖拽 修改大小(二)
- qt 标签 QTextBrowser QLabel
- qt 拖拽 修改大小
- qt 布局
- qt集成dsoframer.ocx打开office办公软件
- qt 窗口鼠标穿透
- Qt窗口定制
- qt4升级到qt5的一些问题
- qt 窗口动画
- qt系统托盘显示、无主窗体
- qt程序启动播放动画