I have some code for Qt below that connects two devices via UART if possible. If not possible, it will display an error string.
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();
}
}
I have two ports initialized, as below:
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();
}
This is something I have received with the code:
Device failed to open: "No Error has occurred" : "/dev/tty01"
Listening for data on "/dev/ttyO4"
I am unsure why. I have attempted to do the following:
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();
}
This did not result in the port actually opening. Please let me know if more information is required to solve this issue; I will update this question accordingly.
CodePudding user response:
There was a typo. The port was written as "tty01", when it should have been "ttyO1", O for Orange instead of a zero. Thanks to chehrlic for catching it, 0s and Os are a bit too similar in Linux.