PDA

View Full Version : qextserialport taking value 0 as null ?



dheeraj
8th August 2008, 13:38
Hi friends i am using qextserialport for RS232 communication between a targetboard and a PC. Now i have a serious issue which i am not able to solve. I am receiving data from the target board every thing is fine but when i receive the value " 0x00 " I get null, i am not able to receive it
as it is. When i receive it the byte coming after 0 is taken as a new frame, example - i receive a single frame which is --> 0x04,0x08,0x81,0x00,0x89,0x78 . I get it as 0x04,0x08,0x81 as one frame and 0x89,0x78 as another frame. I wanted this whole thing as a single frame.
When i receive 0x00 i get it as Null. But i wanted the 0x00 to be received as any other hex value.
This is the code i am using to receive.


char buff[2];
int numBytes;
numBytes = port->bytesAvailable();
int i= port->read(buff, numBytes);
if (i!= -1)
buff[i] = '\0';
else
buff[0] = '\0';
QByteArray msg = buff;
textBrowser->append(msg.toHex());


Help me solve this problem. !!

Thank You

calhal
9th August 2008, 08:05
What's the difference between NULL and 0x00?

You have

char buff[2];
and then you read some unknown number of characters. Are you sure there are only two to read?

Are you sure, that all the characters (whole frame) are read before you use QTextBrowser::append() ? If not, you'll see some characters in new line every time you run this code.

dheeraj
11th August 2008, 06:57
Hi



What's the difference between NULL and 0x00?


For me in my program null means nothing no value, But 0x00 is taken as null i din't want that happen because i have some service request with 0x00.



char buff[2];


Sorry that was a mistake , I changed it to 1024 but forgot to change it while i posted it here.

Hey so is there any way i can take 0x00 as a normal hex number. Actually i receive this frame from the target board. I send a request frame in response to it the target board sends back a frame which may have 0x00, and if 0x00 is received the frame is broken and the hex values after 0x00 are taken has a second frame. How can i avoid this.


Thank You.

calhal
11th August 2008, 07:43
Instead of this

QByteArray msg = buff;
try

QByteArray msg = QByteArray::fromRawData(buff,numBytes);
or

QByteArray msg = port->read(numBytes);

Sorry, i think i din't read your post carefully last time ;)

dheeraj
11th August 2008, 10:55
Hey thanks for the help it worked. !!!

I changed it to



QByteArray msg = QByteArray::fromRawData(buff,numBytes);


and now i am able to even receive zeros . Thank You again friend.:)



Cheers
Dheeraj