我使用socat创建虚拟串口:
$ socat -d -d pty,raw,echo = 0 pty,raw,echo = 0
然后只需通过我的程序打开它,然后按Ctrl + c停止程序,再次运行后,请告诉我这条消息:
"无法打开端口pts / 9,错误:设备或资源繁忙"
#include "serport.h"
#include <QCoreApplication>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QSerialPort serialPort;
serialPort.setPortName("/dev/pts/9");
serialPort.setBaudRate(QSerialPort::Baud9600);
if (!serialPort.open(QIODevice::ReadOnly)) {
qDebug() << QObject::tr("Failed to open port %1, error: %2").arg(serialPort.portName()).arg(serialPort.errorString()) << endl;
return 1;
}
serport port(&serialPort,&a);
a.connect(&a, SIGNAL(aboutToQuit()), &serialPort, SLOT(deleteLater()));
a.connect(&a, SIGNAL(aboutToQuit()), &port, SLOT(deleteLater()));
return a.exec();
}
serport.h
#ifndef SERPORT_H
#define SERPORT_H
#include <QDebug>
#include <QObject>
#include <QtSerialPort/QSerialPort>
class serport : public QObject
{
Q_OBJECT
public:
explicit serport(QSerialPort *serialPort, QObject *parent);
~serport();
QSerialPort *port;
signals:
public slots:
};
#endif // SERPORT_H
serport.cpp
#include "serport.h"
serport::serport(QSerialPort *serialPort, QObject *parent) : QObject(parent),port(serialPort){ }
serport::~serport(){
qDebug()<<"closing";
port->close();
}
如何关闭港口?为什么在我的程序中从未见过&#34;关闭&#34;消息?