PDA

View Full Version : send hexa data serial port and read it



hathemi
28th May 2018, 17:01
i have a problem here. i still can't figure out how to be able to communicate between raspberry and microcontroller.
each time i send data to serial port i don't receive any answer. i notice that data is sent in ascii.
Here's my code :
if (serial->isOpen()) {
qDebug() << "port is open: " ;
QByteArray buff= {0xA1, 0xFF, 0xFF, 0x00,97};
serial->write(buff);
}
if (serial->bytesAvailable()) { // If there are bytes available
QByteArray f_data; // data container
f_data.clear();

while(serial->bytesAvailable()) { // Reading loop
f_data.append(serial->readAll());


serial->flush();
}
qDebug()<< "the result" << f_data; // Check the result
}

if(!serial->isOpen())
qDebug() << "carte disconnect"<<serial->errorString();

any help PLZ !!!

high_flyer
29th May 2018, 10:16
Since this has to do with hardware and another system the point of failure can be anywhere.
You could help by post what are all the things you have checked to be ok such as:
Did you make sure both devices use the same serial settings for example?
The code you posted is too minimal and without a context so its hard to see if there are any problems in it as well...

Lesiok
29th May 2018, 11:30
This code is pointless. You are sending data, you are not waiting for the end of sending and you expect an immediate response. The serial port is a very slow device.

high_flyer
30th May 2018, 10:33
Lesioks point is correct.
You can use the blocking calls waitForReadyRead() and waitForBytesWritten() to wait for the data to be written and or until it is received and ready to be read.