Sending 16bit and 32bit variables over UART

I am currently developing a project in which two microcontrollers communicate with eachother over UART.

So far, I had no problems with sending data (bytes) between the microcontrollers.

But now, I need to send 16bit(int) and 32bit(unsigned long) variables over UART.

How can I split up bigger integer types into two/four bytes to send them over UART and then join them together again?

I would need something like this:

int var1;
    unsigned long var2;

    byte arr1[2];
    byte arr2[4];


    //Somehow convert this

    MyUARTProtocol.transmit(arr1, 2);
    MyUARTProtocol.transmit(arr2, 4);

And on the other microcontroller:

byte arr1[2];
    byte arr2[4];


    //Somehow convert this

    int var1;
    unsigned long var2;

union

[quote author=Coding Badly link=msg=4005377 date=1546676226] union

[/quote]

Ah right, brilliant. Thank You.

Wow. That was easy. Any follow up questions?

[quote author=Coding Badly link=msg=4005388 date=1546677188] Wow. That was easy. Any follow up questions?

[/quote]

I think I understood what you meant with the union, I already used it for structs.

I will try it and if it works, I think I wont have any more questions.

Thank You

You are welcome. Good luck.

[quote author=Coding Badly link=msg=4005422 date=1546678870] You are welcome. Good luck.

[/quote]

Ok, now I successfully integrated the transmission concept in my communication protocol.

For others who might have the same problem in the future, this is the basic principle:

void setup() {
  Serial.begin(9600);
}

union unionNum {
  unsigned int num;
  byte arr[2];
};

void loop() {
  unsigned int primaryNum = 950;  //value to transform/send
  unsigned int secondaryNum;
  
  unionNum primaryUnion, secondaryUnion;

  primaryUnion.num = primaryNum;

  //Other Microcontroller:
  
  secondaryUnion.arr[0] = primaryUnion.arr[0];
  secondaryUnion.arr[1] = primaryUnion.arr[1];

  Serial.println(secondaryUnion.num);
  Serial.println("-------------------------");
  delay(500);
}

@OP

But, you have not posted your hardware setup and full codes for the Transmitter and Receiver. The codes that you have posted are also not presented in a literate way.

Have you noticed that an integer value can be sent and be reconstructed without using union?

(1) Hardware Setup

(2) Transmitter_UNO Codes:

#include<SoftwareSerial.h>
SoftwareSerial mySUART(6, 7); //SRX, STX
unsigned int x = 0x1234;

void setup()
{
  Serial.begin(9600);
  mySUART.begin(9600);
}

void loop()
{
  mySUART.write(highByte(x));
  mySUART.write(lowByte(x));
  delay(1000);
}

(3) Receiver NANO Codes:

#include<SoftwareSerial.h>
SoftwareSerial mySUART(6, 7); //SRX, STX
unsigned int x;

void setup()
{
  Serial.begin(9600);
  mySUART.begin(9600);
}

void loop()
{
  if (mySUART.available() == 2)
  {
    x = mySUART.read() << 8 | mySUART.read();
    Serial.println(x, HEX);
  }

}

(4) Receiver Serial Monitor

sm31.png

uartms-1.png

No, I did not know about this method.

I am pretty sure you would not like to see the whole code and the hardware setup, it is very complex.

This method looks useful, but does it also work for 32bit variables?

hallolo:
This method looks useful, but does it also work for 32bit variables?

Just use another union

union union32bits {
  uint32_t num;
  byte arr[sizeof(uint32_t)];
};

Guess what: this idea works for floats too! How cool is that?

union byte2float {
  float num;
  byte arr[sizeof(float)];
};

[quote author=Coding Badly link=msg=4006474 date=1546732325] Just use another union...

union union32bits {
  uint32_t num;
  byte arr[sizeof(uint32_t)];
};

[/quote]

Of course, this was clear to me. This question referred to the low/highByte method.

hallolo: I am pretty sure you would not like to see the whole code and the hardware setup, it is very complex.

Which Arduino Boards you are using as Transmitter and Receiver.

Are you using hardware UART Ports for data exchange between the Arduinos?