2

I am exchanging structs over serial connection, in C++, between my linux machine (x64) and a micro controller (Arduino Uno). The struct is basically as follows for now.

struct __attribute__ ((packed)) packet
{
    uint8_t data[12];
}

For numbers smaller than 32, sending and reading is fine on the micro controller but larger numbers are not the same. Like 32, becomes 96, 33 becomes 97 and so forth.

packet my_packet;

for (int i = 0; i < 12; ++i)
{
    my_packet.data[i] = i;
}

if(write(fd, &my_packet, sizeof(packet))>0)
{
    printf("received:\n");
}
else
{
    printf("Error writing\n");
}

On the reading side,

int ret = read(fd, &my_packet_rev, sizeof(my_packet_rev));
if( ret < 0)
{
    printf("Error reading\n");
    exit(0);
}
else if (ret == 0 )
{
    printf("No response\n");
}
else
{
    printf("received:\n");
    for (int i = 0; i < 12; ++i)
    {
        printf("%02X, ", my_packet_rev.data[i]);
    }
    printf("\n");
}

Input:

1E 1F 20 21 22 23 24 25 26 27 28 29

Output:

1E 1F 60 61 62 63 64 65 66 67 68 69

What might I be doing wrong?

Jim Andrews
  • 43
  • 1
  • 7
  • 8
    sounds like you need to take care of endianess – 463035818_is_not_an_ai Sep 11 '17 at 14:31
  • 4
    You can use the `htonl()` / `ntohl()` functions to do so. – user0042 Sep 11 '17 at 14:32
  • 9
    While doing some research about [*endianness*](https://en.wikipedia.org/wiki/Endianness) is always good, it should not be a problem here since you're just sending an array of single bytes. And if `my_packet` is an instance of the `packet` structure, then the sending is okay. That only leaves the receiving as a possible problem, or how you show the data you receive, but unfortunately you don't show how you do that. Can you please try to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve) and show us? – Some programmer dude Sep 11 '17 at 14:36
  • 1
    you have not shown the specifics of the communication channel, such as the initialization. Given the posted values for the send and received data, it looks like the communication protocol is not 8N1 but perhaps 5N1 – user3629249 Sep 11 '17 at 17:03
  • 3
    this code: `for (int i = 0; i < 12; ++i) { my_packet.data[i] = i;` will result in the array containing: `0x00 0x01 0x02 0x03 0x04 ... 0x0A 0x0B` Which is nothing like the data you posted. – user3629249 Sep 11 '17 at 17:10
  • Does Arduino have a "write" function? I thought it would be Serial.write. – Makketronix Sep 11 '17 at 17:21
  • @Makketronix, yes but I am not using the default serial port on the arduino – Jim Andrews Sep 11 '17 at 18:56
  • 2
    Make sure that both sides have the comm port set the same such as 8N1. There seems to be exactly 1 bit short between sent and received data. https://www.arduino.cc/en/Serial/Begin – Chimera Sep 11 '17 at 19:08
  • Did you get the problem sorted out? – Chimera Sep 11 '17 at 21:14
  • No, the settings seem to be same on both sides 8N1, @Chimera – Jim Andrews Sep 12 '17 at 08:24
  • setup is same as in the stackoverflow answer: https://stackoverflow.com/a/18134892/8537237 – Jim Andrews Sep 12 '17 at 08:42
  • 1
    @user3629249 asked you - why the input you show is: 1E 1F 20 21 22 23 24 25 26 27 28 29 instead of: 0x00 0x01 0x02 0x03 0x04 ... 0x0A 0x0B ? – SChepurin Sep 12 '17 at 12:33
  • Could you give more information about the "write" function on Arduino? Where did you get the library from? – Makketronix Sep 12 '17 at 21:09
  • @user3629249 apologies, I had copied the wrong part of the code, now I have fixed it – Jim Andrews Sep 13 '17 at 09:06
  • Fixed my issue, was an error in the custom serial library on the Arduino – Jim Andrews Sep 26 '17 at 11:51

0 Answers0