以下代码在Windows中正确地适用于我,但Linux不起作用.我使用相同的PC,两个操作系统都是原生安装的.我不使用虚拟机.我需要在Linux上工作.我已尝试过不同的Linux发行版,但无法在任何地方使用.
// In the main class:
QSerialPortInfo info = XXXX; // Generally in Linux: /dev/ttyUSB0, in win: COM1
QSerialPort serial;
QObject::connect(&serial, SIGNAL(readyRead()), this, SLOT(onReadyRead()), Qt::DirectConnection);
QObject::connect(&serial, SIGNAL(bytesWritten(qint64)), this, SLOT(onBytesWritten(qint64)), Qt::DirectConnection);
QObject::connect(&serial, SIGNAL(error(QSerialPort::SerialPortError)), this, SLOT(onError(QSerialPort::SerialPortError)), Qt::DirectConnection);
// Slot
void MyClass::onReadyRead()
{
qDebug()<<"Signal onReadyRead";
buffer_mutex.lock();
buffer += serial.readAll();
qDebug()<<"Read: "<<qstr_to_hexstr(buffer);
bufferNotEmpty.wakeAll();
buffer_mutex.unlock();
}
void MyClass::onError(QSerialPort::SerialPortError error) {
qCritical()<<"Serial Port Error: "<<(int)error;
}
void MyClass::onBytesWritten(qint64 size){
qDebug()<<"onBytesWritten: "<<size;
}
// In another place I did:
serial.setPort(info);
if(!serial.open(QIODevice::ReadWrite))
return false;
qDebug()<<"Init Setting!...";
if(!serial.setBaudRate(QSerialPort::Baud9600))
qCritical()<<"Error …Run Code Online (Sandbox Code Playgroud)