如果勾选了显示时间戳则在每次数据发送后将数据填充到接收框进行显示;代码中对发送的不同格式数据进行了不同颜色的标记;发送后对发送计数框进行更新。 QSerialPort继承自QIODevice,串口发送数据就是使用QIODevice类的write方法: qint64 QIODevice::write(const QByteArray&byteArray) This is an overloaded function...
foreach(const QSerialPortInfo &info,QSerialPortInfo::availablePorts()) { serial->setPort(info); // 在对象中设置串口 if(serial->open(QIODevice::ReadWrite)) // 以读写方式打开串口 { ui->PortBox->addItem(info.portName()); // 添加计算机中的端口 serial->close(); // 关闭 } else { q...
QSerialPort继承自QIODevice,串口发送数据就是使用QIODevice类的write方法: qint64 QIODevice::write(const QByteArray&byteArray) This is an overloaded function. Writes the content of byteArray to the device. Returns the number of bytes that were actually written, or -1 if an error occurred. 2.4...
serial->close(); 就可以关闭串口了。我使用了ui界面设计来编写上位机的,界面如下: 代码如下: //mianwindow.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QDebug> #include <QtSerialPort/QSerialPort> #include <QtSerialPort/QSerialPortInfo> namespace Ui { class Main...
serial->write(FileText.mid(FrameCount * FrameLen, LastFrameLen).toUtf8()); ui->progressBar->setValue(100); } /*发送完毕*/ FileSendTimer->stop(); FrameCount = 0; ProgressBarValue = 0; FrameNumber = 0; LastFrameLen = 0; QMessageBox::information(this, "提示", "发送完成"); ...
if(m_serial->open(QIODevice::ReadWrite)){ qDebug()<<"串口打开成功"; } 第5步(必须):收发数据,假设COM1已经被我们成功打开了,接下来我们就开始读写啦。 1>首先是写操作,写操作比较简单,只管发送就行了。 1 2 3 QString str = "Hi'; ...
(QSerialPort::NoParity);if(!serial->open(QIODevice::ReadWrite)){QMessageBox::information(this,"错误提示","不能打开串口",QMessageBox::Ok);}else{ui->cbCom->setEnabled(false);ui->pbConnect->setText("关闭");}}else{serial->close;ui->cbCom->setEnabled(true);ui->pbConnect->setText(...
On Windows 10 I had to make sure to call serialPort.waitForBytes(100); immediately after the serialPort.write(data); to ensure the bytes actually made it on the wire. Otherwise, the write() would return the correct number of bytes sent, but nothing was sent on the wire. I also tried...
voidMainWindow::writeData(constQByteArray&data){serial->write(data);} 该插槽将在给定的控制台小部件中键入的字符发送到串行端口。 当串行端口接收到新数据时,将发出信号readyRead(),并将该信号连接到MainWindow :: readData()插槽: void MainWindow::readData(){QByteArray data=serial->readAll();console-...
void Widget::on_closeButton_clicked() { serialPort->close(); } void Widget::on_onButton_clicked() { serialPort->write("ON\n"); qDebug("ON\n"); } void Widget::on_offButton_clicked() { serialPort->write("OFF\n"); qDebug("OFF\n"); } 以上就是上位机逻辑代码的编写。 4、添...