QSerialPort *serial;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
serial = new QSerialPort(this);
serial->setPortName("/dev/ttyUSB0");
serial->setBaudRate(QSerialPort::Baud9600);
serial->setStopBits(QSerialPort::OneStop);
serial->setParity(QSerialPort::NoParity);
serial->setDataBits(QSerialPort::Data8);
serial->setFlowControl(QSerialPort::NoFlowControl);
serial->open(QIODevice::ReadWrite);
connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(serialReceived()));
connect(ui->pushButton_2, SIGNAL(clicked()), this, SLOT(serialSent()));
}
MainWindow::~MainWindow()
{
delete ui;
serial->close();
}
void MainWindow::serialReceived()
{
ui->label->setText(serial->readAll());
}
void MainWindow::serialSent()
{
QByteArray bytes;
serial->write(bytes);
}
从这段代码中,我在 Ubuntu 上运行了 qt。它显示 QIOdevice::write 设备未打开。但是我已经在这个端口上插入了我的 USB 到串口,我该如何解决这个问题。