Unsolved Not able to pass 0x00 over Qserial port
-
Hi,
Here is my command which i have to trsnsmitt over Qserial port.
MSG_EXT_T omsg;
omsg.start_byte =0x55;
omsg.param = 0x00;
omsg.priority = 0x00;
omsg.uniq_id = 0x00;
omsg.msg_id = 0x00;
omsg.ack = 0x00;
omsg.ext_msg_type = 0x00;
omsg.payload[0]=0x00;
omsg.payload[1]=0x00;
omsg.payload[2]=0x00;
omsg.payload[3]=0x00;
omsg.crc = 0x00;
omsg.stop_byte=0xAA;i am trying to convert it into Qbytearray and using serial->write() to send above structure. But i am not able to send except first character. Please let me know the possible way.
below is my Qserial port config.
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
serial = new QSerialPort(this);
connect(serial, SIGNAL(readyRead()), this, SLOT(serialReceived()), Qt::QueuedConnection);
//connect(serial, &QSerialPort::bytesWritten, this, &MainWindow::writeData);
serial->setPortName("/dev/ttyUSB1");
serial->setBaudRate(QSerialPort::Baud9600);
serial->setDataBits(QSerialPort::Data8);
serial->setParity(QSerialPort::NoParity);
serial->setStopBits(QSerialPort::OneStop);
serial->setFlowControl(QSerialPort::NoFlowControl);
serial->open(QIODevice::ReadWrite);
} -
@Ramarao said in Not able to pass 0x00 over Qserial port:
i am trying to convert it into Qbytearray and using serial->write() to send above structure. But i am not able to send except first character. Please let me know the possible way.
How do you convert
MSG_EXT
toQByteArray
?
By the way, what isMSG_EXT
?
How do send data on serial port? I can only seeQSerialPort
configuration, no data are sent from this code extract. -
@Ramarao
Your code shows no attempt to actually write any bytes.A byte with value
0
is perfectly sendable over serial port, there is nothing special about it. At a guess, the most likely reason you might think it is not sent is whatever you use to view what is being sent/received, e.g.qDebug()
output is not a good way to understand what is actually in a variable or whatever because of the way it presents bytes/strings to the user in a human-readable form. -
@KroMignon Thanks for your response. MSG_EXT_T is structure which i have framed for sending command to other board.
typedef struct MSG_EXT {
uint8_t start_byte;
uint8_t priority;
uint16_t uniq_id;
union {
uint32_t msg_id; /* pre-defined id used by ui board /
/ Break-up of msg_id into therapy internal ids /
struct {
uint8_t src_id; / LSB of msg_id */
uint8_t dest_id;
uint8_t t_cmd;
uint8_t param;
};
};
union {
uint8_t ack;
int8_t notify;
};
uint8_t ext_msg_type;
union
{
bool val_bool;
int8_t val_s8;
int16_t val_s16;
int32_t val_s32;
uint8_t val_u8;
uint16_t val_u16;
uint32_t val_u32;
float val_f32;
DEFINE_MSG_PAYLOAD_WITH_1_U32_ARG(config_data, addr);
DEFINE_MSG_PAYLOAD_WITH_1_U32_ARG(config, val);
DEFINE_MSG_PAYLOAD_WITH_2_U16_ARGS(valve_move, id, unused);
DEFINE_MSG_PAYLOAD_WITH_1_U32_ARG(soft_assert, id);
DEFINE_MSG_PAYLOAD_WITH_1_U32_ARG(rx_msg, addr);
DEFINE_MSG_PAYLOAD_WITH_2_U16_ARGS(rx_file, size, chunk_size);
uint8_t payload[4];
};
uint8_t crc;
uint8_t stop_byte;
} MSG_EXT_T, *MSG_EXT_PTR;my code for transmitting command is below.
char b[sizeof(omsg)];
memcpy(b,(char*)&omsg,sizeof(omsg));
QByteArray ba = QByteArray::fromRawData(b,sizeof(b));
serial->write((const char *)ba,sizeof(ba)-1);
qDebug() << ba;qDebug() output: "U\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xAA"
-
@Ramarao
...And you use rawqDebug()
to visualise the content....What is the value of
sizeof(MSG_EXT)
? What is the declaration ofomsg
?And what is your evidence that any byte of
0
is not actually sent??And
serial->write((const char *)ba,sizeof(ba)-1);
: why do you omit sending the last byte? -
@Ramarao said in Not able to pass 0x00 over Qserial port:
my code for transmitting command is below.
char b[sizeof(omsg)];
memcpy(b,(char*)&omsg,sizeof(omsg));
QByteArray ba = QByteArray::fromRawData(b,sizeof(b));
serial->write((const char *)ba,sizeof(ba)-1);
qDebug() << ba;
qDebug() output: "U\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xAA"Just a side note, try to use button
</>
to insert the code, so it will be more readable.To convert your raw structure to QByteArray, I would do:
QByteArray ba = QByteArray::fromRawData((char*)&omsg, sizeof(omsg));
Then to send:
serial->write(ba.data(), ba.size());
EDIT
A shorter way could be:serial->write((char*)&omsg, sizeof(omsg));
And why using sizeof(ba)? You don't want to have the size of the QByteArray instance but the size of the data with is ba.size().
-
Thanks for the suggestion. i am getting below result after trying this.
code_text"" "" "" "" "" "" "" "" "" "" "" "" "" "" "?"
-
@Ramarao said in Not able to pass 0x00 over Qserial port:
i am getting below result after trying this.
After trying what exactly?
-
@KroMignon after trying below code, i got "U" as first character and rest are like above.
code_textQByteArray ba = QByteArray::fromRawData((char *)&omsg,sizeof(omsg));
serial->write(ba.data(),ba.size()); -
@Ramarao said in Not able to pass 0x00 over Qserial port:
after trying below code, i got "U" as first character and rest are like above.
Which seems to be normal to me 0x55 is
U
is ASCII table.
But where to you see this?
You are only sending data with this code extract. -
@KroMignon i am sending this command to AM335x and observing the data in microcom or other QT receiving application running on board..
-
@Ramarao said in Not able to pass 0x00 over Qserial port:
i am sending this command to AM335x and observing the data in microcom or other QT receiving application running on board..
You should be aware that '\0' (NULL character) are not printable, so you cannot see it with microcom.
You have to use an hexadecimal terminal, something likessterm
(https://github.com/vsergeev/ssterm)ssterm -o hex /dev/ttyXXX
-
@KroMignon iam running QT receiving app also at board side to observe data.
code_text QByteArray data = serial->readAll(); QString str = QString(data); qDebug() << str;
My doubt is serial->write() will not send 0's by treating it as null character if we send in any format.
-
@Ramarao
I know you have ignored every post I have made, but I did tell you:QString str = QString(data); qDebug() << str;
Using
qDebug()
like this is not the way to be able to see the data. Particularly0x00
s....And additionally did you read QString::QString(const QByteArray &ba):
Constructs a string initialized with the byte array ba. The given byte array is converted to Unicode using fromUtf8(). Stops copying at the first 0 character, otherwise copies the entire byte array.
[My bold.]
-
@Ramarao said in Not able to pass 0x00 over Qserial port:
iam running QT receiving app also at board side to observe data.
code_text
QByteArray data = serial->readAll();
QString str = QString(data);
qDebug() << str;And this wrong, you want to see hexadecimal data:
QByteArray data = serial->readAll(); qDebug() << "Raw data:" << data; qDebug() << "Hex data:"<< data.toHex(':');
My doubt is serial->write() will not send 0's by treating it as null character if we send in any format.
QSerialPort::write(const char * buffer, int size)
do not check null byte, what matter is size. You can send as many null bytes you want, there is no interpretation about buffer content. -
@JonB sorry that as i am new guy, forum is not allowing me immediately(making me to wait 10 mins). I am new to QT and i am trying to send 16 bytes command to AM335x board from QT application(running in PC) with button press for time being.
My goal is to send and receive data over serial communication between AM335x and STM32 where AM335x acting as GUI interface with QT application. -
@Ramarao
That's OK, welcome :)If you read what @KroMignon & I have said between us you ought be good to go!
Truly there is no problem sending
0
bytes across serial. Your problems will be (a) if you ever convert aQByteArray
to aQString
, because it's liable to terminate at the0
, as I wrote earlier, and (b) if you're not careful about interpreting what you see fromqDebug()
, because it shows "odd" bytes in ways you might misinterpret. Using @KroMignon'sQByteArray::toHex()
at least is one way to visualise the bytes without having them "terminated early" or being shown "oddly". -
This issue is resolved.
-
This my open for opening IMU
Imu::Imu() :
moving(false)
{serialPort = new QSerialPort("COM7",this); if (!serialPort->open( QIODevice::ReadOnly)) { log_warning("imu","failed to open device file \"%s\", IMU measures will be unavailable",qPrintable(serialPort->portName())); return; } if (!serialPort->setBaudRate(115200)) log_error("imu","failed to set baudrate, error no %d",serialPort->error()); serialPort->setDataBits(QSerialPort::Data8); serialPort->setParity(QSerialPort::NoParity); serialPort->setStopBits(QSerialPort::OneStop); // One Stop bit serialPort->setFlowControl(QSerialPort::NoFlowControl); //serialPort->open(QIODevice::ReadOnly); pollingTimer = new QTimer(this); QObject::connect(pollingTimer, SIGNAL(timeout()), this, SLOT(pollSerialPort())); pollingTimer->start(10); }
Imu::~Imu()
{
serialPort->close();
}void Imu::pollSerialPort()
{
static const unsigned char START_BYTES[2] = {0x55,0xAA};
static const QByteArray START_WORD((char*)START_BYTES,2);static QTime startTime = QTime::currentTime(); static QByteArray data; data.append(serialPort->readAll()); qDebug() <<"Raw data"<<data.append(serialPort->readAll()); QByteArray hex = data.append(serialPort->readAll()).toHex('0'); // returns "123456abcdef" qDebug() <<"Hex data"<<hex;
I would like to get the data in hexadecimal, however I could not get anything
Neither in binary data, nor in hexadecimal
-
@manel-sam
Hi. It might have been better to open your own topic for this than putting it in this existing thread. You could still do that.You are calling
serialPort->readAll()
3 times, including in aqDebug()
statement. Each time you call it all the data is read, it will not be there for next read.Wouldn't using
readyRead()
signal be better than your timed polling?