PDA

View Full Version : serial port command doesn't work



hovuquocan1997
17th July 2015, 17:53
Hello everyone, I'm having problems of getting the development boards do whatever I want through commands sent through serial port.

So before I wrote my Qt GUI program, I had tested my commands with a program named RealTerm Terminal that I found online, and everything worked fine, but then when I started building my Qt program and started to send the exact same commands through serial port, the board did receive the data, but it didn't do anything at all.

For example, on RealTerm, I sent this command to my board : 1 7 2 100 0 110 4, which in hex is 01 07 02 64 00 6E 04, and it set the board ID to 100 as expected, the board also sent back an acknowledgement line, so no problem there.

However, when I used these lines of code to send the same command to the board on my Qt program:


char panID[] = {0x01, 0x07, 0x02, 0x64, 0x00, 0x6E, 0x04};
serial->write(panID);


The board received the data (it flashed), but that was all, the panID wasn't set to 100, and the board didn't send any acknowledgement back.

Can anyone help me to point out the problem here? I'm perplexed by this strange behavior. Thank you.

yeye_olive
17th July 2015, 18:19
Hard to say just by reading those two lines of code. What is 'serial'? What is 'write'?

I am going to take a shot in the dark here: does, by any chance, the method WhateverClassSerialIsAnInstanceOf::write(const char *s) write to the serial port the bytes starting at s, up to and including the first NUL byte? If it is so, then your code only writes the 5 first bytes of panID.

hovuquocan1997
17th July 2015, 18:31
Thank you for your reply and sorry for not providing enough information.

I used QSerialport to write this program, and serial was an instance of that class, I declared it above those lines of codes
QSerialPort *serial = new QSerialPort(this);, and "write" is just a function of the class, writing data to the port.

What you said can be a possibility, but I'm not really sure, do you have any suggestion on how to fix it? I would greatly appreciate your help :)

yeye_olive
18th July 2015, 09:16
Thanks for the clarification. QSerialPort is derived from QIODevice, whose write(const char *) method indeed writes a NUL-terminated string. In order to specify the number of bytes to write, call the write(const char *, qint64) method instead:

serial->write(panID, sizeof(panID));
and check the return value to make sure everything was written.

hovuquocan1997
20th July 2015, 17:10
Thank you very much! It worked! I really appreciate your help, my time was running out and you saved my life.