bomby
Posts: 9
Joined: Sun Feb 07, 2016 7:34 pm

Sending and receiving integers from Arduino (endian)

Tue Mar 01, 2016 3:10 pm

I'm trying to send and receive integers from Arduino.

I have checked online that Raspberry Pi and Arduino are both little-endian.

However when I receive a integer from Arduino's Serial.write() via serial rs232, the byte always seems to come in big endian format, and i would need to manually shift the bytes to change to little endian.

I am using the rs232 library from http://www.teuniz.net/RS-232/

I have no idea why the byte array turn into big endian when transferring a single byte from Arduino to RPi.

If anyone can shed some light on this, that would be great!
Last edited by bomby on Tue Mar 01, 2016 4:18 pm, edited 1 time in total.

-rst-
Posts: 1316
Joined: Thu Nov 01, 2012 12:12 pm
Location: Dublin, Ireland

Re: Sending and receiving integers from Arduino (endian)

Tue Mar 01, 2016 4:03 pm

'Single byte' and 'endianness' does not compute, sorry :roll:
https://en.wikipedia.org/wiki/Endianness wrote: Endianness refers to the order of the bytes, comprising a digital word...
so a multi-byte 'word' would be represented differently but single bytes are not affected.

To me what you write sounds like the bits in the single byte come in wrong order? So something like you send 64 (0100000b) and receive 2 (00000010b)??

Or is it maybe that you have a multi-byte integer value and the Serial.write(val) writes out a byte you did not expect? Or maybe it is signed integers vs unsigned??

If not the above, could you do a very simple test - something like:

Code: Select all

Serial.write(42)
and

Code: Select all

unsigned char buf[5];
int n = RS232_PollComport(int comport_number, buf, 1);
printf("n=%d\n", n);
printf("buf[0]=%d\n", buf[0]);
and tell us what the output is?
http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'

bomby
Posts: 9
Joined: Sun Feb 07, 2016 7:34 pm

Re: Sending and receiving integers from Arduino (endian)

Tue Mar 01, 2016 4:29 pm

Hi!

Sorry for the confusion (updated by initial post!) Due to errors reading in integer of 4 bytes, i had to resort to using 1 byte and somehow it seems to read the values correctly (cant possibly go wrong with 1 byte right? :lol: )

Ok so the main problem is this:
1) I am trying to receive an integer of 4 byte from Arduino to RPi. I use the same method:

Code: Select all

int cport_nr = 24, num;
unsigned char buf[4];

if(RS232_PollComport(cport_nr, buf, sizeof(int)) > 0) {

memcpy(buf, (char*)&num, sizeof(int));

// do some bit shifting here in order to display correct num via printf("%d", num)

}
Let's say if I use this on Arduino IDE:

Code: Select all

int data = 170;
//uint32_t = 170;
Serial.write(data)
RPI will display "AA AA AA AA" when i try to print %x from buf[0] to buf[3] when it should just be "AA 0 0 0"

I do not have the arduino with me now so i can't tell u the result, let me update you and reply in this thread again in about another 10 hour's time. In the meantime, we still cant figure out why Arduino is sending AA AA AA AA. Could it be due to insufficient bit? Example my RPi is waiting for 4 bytes but somehow the Arduino int is only 1 byte? We tried uint32_t as well but it still sends AA AA AA AA

-rst-
Posts: 1316
Joined: Thu Nov 01, 2012 12:12 pm
Location: Dublin, Ireland

Re: Sending and receiving integers from Arduino (endian)

Tue Mar 01, 2016 5:31 pm

As far as I know, Arduino Serial.write(val) only sends one byte, so you are probably getting one byte to your array[0] and the rest is just garbage! Always good to initialise arrays to zero and check the read number of bytes:

Code: Select all

int cport_nr = 24, num;
unsigned char buf[sizeof(int)];

memset(buf, 0, sizeof(int));

int n = RS232_PollComport(cport_nr, buf, sizeof(int))
if (n == sizeof(int)) {

    num = *((int *)buf);
...
For a 16-bit / 2-byte int you should probably do something like:

Code: Select all

int data = 170;
Serial.write(&data, 2)
and obviously only read two bytes at the receiving end. And for 4-byte int:

Code: Select all

uint32_t = 170;
Serial.write(&data, 4)
http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'

PabloMack
Posts: 10
Joined: Tue Feb 09, 2016 11:30 pm

Re: Sending and receiving integers from Arduino (endian)

Sat Mar 05, 2016 10:59 pm

By convention, the TCP/IP protocols (for example) are Big-Endian regardless of the processor being used. This is necessary so that all processors, regardless of their architectures can all speak and understand the same protocol. It is easy to reverse the order of the bytes for use with the processor you are using. In fact there are standard functions to do this. For example, htons() will convert the local host's byte order of a 16-bit integer (the 's' in htons stands for "short") to the network (or communications) byte order. If your system is little-endian, it will reverse the order because "network order" is (at least in TCP/IP) Big Endian. If your system is Big Endian, then the function does nothing to the number but it is still retained in the source code so that it will remain portable and work as it is regardless which system it is compiled to run on.

Return to “C/C++”