Problem with byte array and serial port

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:

byte tBuf[10];
long tInt;

void setup()
{
  Serial.begin(115200);
  
  *(tBuf + 5) = 0b00101011 ;//dec 43 ; //2B
  *(tBuf + 4) = 0b00111000 ;//dec 56 ; //38
  *(tBuf + 3) = 0b00011110 ;//dec 30 ; //1E
  *(tBuf + 2) = 0b00000010 ;//dec 2  ; //02
  *(tBuf + 1) = 0b00000000 ;//dec 0  ; //00
  *(tBuf + 0) = 0b01011101 ;//dec 93 ; //5D
  //Sum is 224
  

  Serial.write(*(tBuf + 0));
  Serial.write(*(tBuf + 1));
  Serial.write(*(tBuf + 2));
  Serial.write(*(tBuf + 3));
  Serial.write(*(tBuf + 4));
  Serial.write(*(tBuf + 5));

  tInt = *(tBuf + 5) + *(tBuf + 4) + *(tBuf + 3) + *(tBuf + 2) + *(tBuf + 1) + *(tBuf + 0);
  
  Serial.println(tInt);
}

void loop() {
  //
}

I expected to get

5D 00 02 1E 38 2B 50 50 52 0D 0A

but i am getting only two first bytes 5D and 00, everything else looks like rubbish and changing every time i run it. In 5 consecutive runs i got:

5D 00 D0 00 00 00 00 00 16 00 00
5D 00 D1 00 00 00 00 00 16 00 00
5D 00 D0 00 00 00 00 00 21 00 00
5D 00 D2 00 00 00 00 00 21 00 00
5D 00 B1 00 00 00 00 00 21 00 00

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.

  tBuf[5] = 0b00101011 ;//dec 43 ; //2B
  tBuf[4] = 0b00111000 ;//dec 56 ; //38
  tBuf[3] = 0b00011110 ;//dec 30 ; //1E
  tBuf[2] = 0b00000010 ;//dec 2  ; //02
  tBuf[1] = 0b00000000 ;//dec 0  ; //00
  tBuf[0] = 0b01011101 ;//dec 93 ; //5D

What happens if you change

  Serial.write(*(tBuf + 0));

to

  Serial.print(tBuf[0], BYTE);

?
I don't have my Arduino with me today, or I'd test it.

@chig00

I expected to get
...

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.)

Regards,

Dave

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. :slight_smile: