Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 4 of 4

Thread: union + struct not working (but works on arduino mega)

  1. #1
    Junior Member
    Join Date
    Aug 2016
    Posts
    2

    union + struct not working (but works on arduino mega)

    I am having problems running some code that I succesully tested on the arduino mega. The code receives 5 bytes ('&','@', uint8, uint16) and parses them using a union with a struct inside. This works well on the arduino mega.

    Any ideas on how I can solve this bug?

    I'm working from Windows 10, and I sending the data from Matlab

    Code:
    #define A_START       '&'
    #define B_START       '@'
    #define HEAD_LENGTH     2
    #define MSG_LENGTH      3
    #define BAUDRATE      115200
    
    int cnt = 0;        // serial data counter
    // Struct for Data Conversion
    typedef struct {
      union {
        struct {
          uint8_t channel;
          uint16_t val;
        };
        char data[3];
      };
    } convertor;
    convertor packet;
    
    enum State { st_h1, st_h2, st_data};     // not used
    enum State fromState = st_data;     // not used
    
    void setup() 
    {
      // put your setup code here, to run once:
      Serial.begin(BAUDRATE);
      Serial1.begin(BAUDRATE);
      Serial1.println("Check!");
    }
    
    void loop()
    {
      char msg;
      while (Serial.available())
      {
        msg = Serial.read(); cnt++;
        Serial1.println(msg);
    
        // check if A_START       
        if ((msg == A_START) && (cnt == 1))
        {
          cnt++;
        }
        // check if B_START       
        else if ((msg == B_START) && (cnt == 2))
        {
          cnt++;
        }
        // read data
        else if (cnt == 3)
        {
          Serial.readBytes(packet.data, 3);
          Serial1.print("Channel: "); Serial1.println(packet.channel);
          uint16_t val = packet.val;
          Serial1.print("TEST: "); Serial1.println(val);
          // Serial1.print("Value: "); Serial1.println(packet.val);
          // Serial1.print("Value: "); Serial1.println(packet.data[1]);
          // Serial1.print("Value: "); Serial1.println(packet.data[2]);
          cnt = 0;
        }
        // force search for heard
        else
        {
            cnt = 0;
        }
      }
    }

  2. #2
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    3,247
    Try looking into: #pragma pack

    Different architectures will pack data differently.

    Warning on different machines doing a pack may cause machine faulting. That is some machines do not allow you to access things like words when they are not word aligned...

  3. #3
    Junior Member
    Join Date
    Aug 2016
    Posts
    2
    Thanks KurtE,
    It's my first time learning about '#pragma pack'.
    After googling "teensy pragma pack" I got this thread that already solved this problem
    https://forum.pjrc.com/archive/index.php/t-24940.html

  4. #4
    Senior Member Constantin's Avatar
    Join Date
    Nov 2012
    Location
    In the yard with a 17' Dia. Ferris Wheel
    Posts
    1,408
    Another option is to use the easy transfer library from bill porter. Very easy to use, very reliable, CRC and all that.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •