Qt:无法读取设备,没有提供错误

  • 本文关键字:错误 读取 Qt c++ qt uart
  • 更新时间 :
  • 英文 :


我下面有一些Qt的代码,如果可能的话,它通过UART连接两个设备。如果不可能,它将显示一个错误字符串。

void Hardware::initialisePort(QString portName)     //Initialized at balance and controlboard
{
/*! brief Method to initialise the com port
*
*/
// setup the port in the appropriate way for the platform that the code is compiled for
this->serialPort = new QextSerialPort(portName, QextSerialPort::EventDriven);   //serialPort now points to this new QextSerialPort, and the string will come from this port.
// setup port settings
serialPort->setBaudRate(BAUD9600);
serialPort->setFlowControl(FLOW_OFF);
serialPort->setParity(PAR_NONE);
serialPort->setDataBits(DATA_8);
serialPort->setStopBits(STOP_1);
// attempt to open the port for both reading and writing
if (serialPort->open(QIODevice::ReadWrite) == true)
{
// connect the port ready to read signal to the handling slot
connect(serialPort, SIGNAL(readyRead()), this, SLOT(serialDataReady()));    //readyRead is just signal, not function, serialDataReady is function that breaks up Arduino string
qDebug() << "Listening for data on" << serialPort->portName();
}
else // could not open port communications
{
qDebug() << "Device failed to open:" << serialPort->errorString() << ":" << portName;
// print the issue to the log with the port error string
//logging() << "Could not open serial port connection: " << serialPort->errorString();
}
}

我初始化了两个端口,如下所示:

Balance::Balance() :
Hardware()
{
// initialise members
// null values for the unallocated masses
lastStableMass = -1;
lastUnstableMass = -1;
stable = false;
timeoutStatus = 0;
hasTimedOut = false;
calibrationInProgress = false;
tareOffset = 0;
this->initialisePort("/dev/tty01"); //Port name is /dev/tty01
timer.setInterval(2000);
connect(&timer, SIGNAL(timeout()), this, SLOT(resend()));
timer.start();
}
ControlBoard::ControlBoard(QObject *obj) :
Hardware(obj)
{
/*! brief Constructor
*
*/
this->initialisePort("/dev/ttyO4"); //Port name is /dev/tty04, and that is somewhere on the control board, don't worry about it
//Initialise port is in hardware.cpp
this->airTempMessage = "";
this->valveChangeMessage = "";
acknowledgementTimer = 0;
calibrationTimer = 0;
sendCalibration = -1;
this->sampleTempTimer = new QTimer;
connect(sampleTempTimer, SIGNAL(timeout()), this, SLOT(requestSampleTempUpdate()));
hasTimedOut = false;
valveOpen = false;
airTemperature = 0;
temporaryCalFactor = -1;
compressorTimer = 0;
requestValveUpdate();
}

这是我收到的代码:

Device failed to open: "No Error has occurred" : "/dev/tty01" 
Listening for data on "/dev/ttyO4" 

我不确定为什么。我尝试过做以下事情:

else // could not open port communications
{
connect(serialPort, SIGNAL(readyRead()), this, SLOT(serialDataReady()));
//Even though the port is listed as cannot be opened, I attempted to see whether the error signal was a false flag by initializing it anyways.
qDebug() << "Device failed to open:" << serialPort->errorString() << ":" << portName;
// print the issue to the log with the port error string
//logging() << "Could not open serial port connection: " << serialPort->errorString();
}

这并没有导致端口实际打开。如果需要更多信息来解决这个问题,请告诉我;我将相应地更新这个问题。

有一个打字错误。港口被写成";tty01";,当它应该是";ttyO1";,O代表橙色,而不是零。多亏了chehrlic的捕捉,0s和Os在Linux中有点太相似了。

相关内容

最新更新