Hi! I am writing program for communication with GPRS module through serial port and I had encountered problem with byte array. I wrote a small test program to investigate it:
What really surprises me is that i still receive 3 bytes for integer number and that i don't receive 0D 0A in the end, so it looks like problem with Serial library. The interesting thing is that the problem is somehow connected with 0 in the byte array, as long as there are no zeros in the array everything works perfect.
Does anybody have ideas how to fix this and send zeros through serial port using byte array?
What really surprises me is that i still receive 3 bytes for integer number and that i don't receive 0D 0A in the end, so it looks like problem with Serial library.
The sum of the numbers you wrote is 224, which you then print, as a string - '2', '2', '4'. '2' = 50 and '4' = 52 in the ASCII table.
I would have expected these to show up as '2', '2', and '4', though.
The carriage return and line feed characters have no visual appearance, so you don't see them.
Where/how are you examining the serial port data?
Arrays can by indexed using either pointer notation, as you are, or array notation.
Using your sketch, here's what I got using a program on my Linux workstation to capture and display bytes (in hexadecimal) from /dev/ttyUSB0
Opened /dev/ttyUSB0 for reading
Number of bytes read = 11
5d 00 02 1e 38 2b 32 32 34 0d 0a
These are the six bytes of your array followed by hex values of ASCII '2', '2', '4' and then '\r', '\n'
That's what I expected and that's what I got.
PaulS asked:
Where/how are you examining the serial port data?
And I have the same question: How did you capture the bytes that you show? If you have a program that uses a function that reads C-Style "strings" (fgets(), for example) it won't work if there are any bytes that are equal to zero. (Which seems to be what you observed.)
Thank you for your replies, i was using GTKTerm in ubuntu 10.04. I tried today same sketch in windows and there was no error. I found this topic today as well: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1236622673 so there is bug in GTKTerm. Now i am using hterm and everything works fine.