@manel-sam said in Serial data received:
So the append I should do it where§?
Create a member variable in your imu class.
// _packetData member variable of imu
_packetData.append(serialPort->readAll());
if(_packetData.size()>=PACKET_LENGTH)
{
// packet complete
qInfo() << QStringLiteral("data size is : ") << _packetData.size() << " octets";
qDebug() << "Serial received " << _packetData.toHex();
// go ahead with this packet
QDataStream stream(_packetData);
// Read The BigIndian value
stream.setByteOrder(QDataStream::BigEndian);
qint16 headerSignature;
stream >> headerSignature;
}
You may also check the data received don't exceed PACKET_LENGTH, cause it means you have already received data from the next packet. (it may be irrelevant in your case, I don't kown)
Hi, @J-Hilk!
Looks like i found desired method in QModbusDataUnit class docs. Don't know how i missed it being mentioned in List of All Members for QModbusRtuSerialMaster. I guess digging into Qt docs about Modbus was a proper way to solve this :)
QModbusDataUnit::QModbusDataUnit(QModbusDataUnit::RegisterType type, int address, const QVector<quint16> &data)
In my case this will be:
QModbusDataUnit input(QModbusDataUnit::InputRegisters, 3001, data);
Thanks for joining my journey!
@elypheldia said in How I listen serial port with writing client code?:
Sorry if used the wrong term. I am new on qt. I wanted to indicate that I read the data I wrote to the tnt0 serial port from terminal with using cat /dev/tnt0. But How can I do this with client code instead of reading from terminal. I just wanna open the tnt1 serial port from code., and I wanna read from there.
You are confusing to me.
Do you want to use /dev/tnt0 or /dev/tnt1?
With the code example you have provided, you are writing hello on serial device /dev/tnt0. Is this working?
You have defined a slot MySerialPort::readData() , which is called when data are ready to be read from serial port.
I have proposed you some changes to display debug message when slot is called. Is this slot called?
I don't know:
what kind of device is connected to this serial port?
if the serial port configuration is correct according to attached device?
the attached device communication protocol: It is a binary or text protocol? Are carriage return or line feed required?
@J-Hilk Thank you all guys for your patient and sorry for my basic mistakes :). I fixed my issue. As you said, I expect the read without writing a data to serial port.
@suslucoder are you sure com4 is the correct name ? something seems of, I would suggest using https://doc.qt.io/qt-5/qserialportinfo.html
to query for the correct port and port name
@J-Hilk said in Serial terminal disconnecting unexpectedly:
of course not, and there is no guarantee that 20ms will be enough each and every time!
If new data arrives readyread will signal again. Your job as a programmer is to store the data, identify if all is arrived, and only then do your processing.
Thank you so much for your quick reply. I store the data in an array and wait for the entire data to come. Now there is no problem with serial communication.
Thanks, @kuzulis for giving me a reference to look at the device side.
The problem is solved by adding line coding settings to the CDC buffer on the STM. It is related to the hardware. STM users should bind USBD_CDC_LineCodingTypeDef data structure to CDC_Control_FS function.
In ready-made examples, this binding does not exist. Users who do not add this binding can face the same problem in long term tests. Maybe it can help people who face the same problem.
Regards
Solved. The missing libQt5SerialPort.so.5 error on the target device was because that library effectively was not present. On the host side I added that library in the package list of the image and rebuild the image itself.
AWESEOME! Thank you very much. 2) did the trick. I found a file /var/lock/LCK..ttyS3. After removing it the code posted above works fine. I will now try 1) or implement a patch myself.
Thanks a lot. You made my day.
Phillip
You're welcome !
Since you have it working now, please update the thread title prepending [solved] so other forums users may know a solution has been found :)
@Leonardo said:
nections, the parameters must be of types that are known to Qt's meta-object system, because Qt needs to copy the arguments to store them in an event behind the scenes.
...
Call qRegisterMetaType() to register the data type before you establish the connectio
Wow, that really helps!
Thank you so much!
I will try it right now