Sending integer values through serial

Hi Guys,

I am trying to figure out the best way to send a integer value from one Due board to another via serial (tx/rx). I receive a value from my sensor (compass) as integer and want to send it straight to board no 2.

But I am not getting the same value from board one to board no2. I have searched online and have come across/tried soooo many different approach to this which is confusing the hell out of me.

All I need is, if I send 45 I should receive 45.

Bit lost, now or i'm missing something

Please help!!

Board one Sender:

int compassvalue;

void setup() {
Serial1.begin(115200);

compassvale = 359;

}

void loop() {


Serial1.write(compassvalue);  // Sent as binary???
delay(200);


}

Board 2 receiver:

void setup() {
Serial1.begin(115200);

}

void loop() {
   if (Serial1.available())
  {

      Serial.print(Serial1.read());  // (debug) I want to send the data from serial read a an integer to SD card/
delay(200);


}

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. The system in the 3rd example will be most reliable.

It is easier to make the sending code match the receiving code rather than vice versa.

It is easier to debug programs if the data is sent as human readable characters rather than as binary data. I suggest you only use binary data if the required throughput can't be achieved any other way.

...R

Also bear in mind that 359 is larger than 255 and serial only sends one byte at a time, not sure what it would do with that value but I assume it only sends the lower 8 bits.

@Robin2

Thanks Robin, I’ve looked at the code sample. I went for example 6, as it deals wit binary data.

The sender device, I wrote

Serial1.write(0x3C);
Serial1.write(359);
Serial1.write(0x3E);

and I received

“This just in (HEX values)… 67”

No sign of my 359!

@Graynomad, Good point, what does happen to the rest? anyone know?

It will just be discarded.

I think you would have to do something like this

Serial1.write(359 >> 8);     // send high byte
Serial1.write(359 && 0xFF);  // send low byte

Then combine the two bytes back into an int at the receiving end.

NOTE: I hope my syntax is correct, I haven't written any C for a few years now.

BTW, 359DEC == 167HEX, notice anything?

Damm, I need to get my "bytes" in order!!

Yes,

Thanks for pointing it out, guess all that fresh air in your motor home works!!

I'm going to have to jigg the receive code, as now I am only getting

"This just in (HEX values)... 1" but now not the 67

Show us your receive code. Looks like you're only printing the MSB.

I borrowed it, from Robin2 to see how Arduino deals with sending/receiving data

// Example 6 - Receiving binary data

const byte numBytes = 32;
byte receivedBytes[numBytes];
byte numReceived = 0;

boolean newData = false;

void setup() {
    Serial.begin(9600);
    Serial.println("<Arduino is ready>");
}

void loop() {
    recvBytesWithStartEndMarkers();
    showNewData();
}

void recvBytesWithStartEndMarkers() {
    static boolean recvInProgress = false;
    static byte ndx = 0;
    byte startMarker = 0x3C;
    byte endMarker = 0x3E;
    byte rb;
   

    while (Serial.available() > 0 && newData == false) {
        rb = Serial.read();

        if (recvInProgress == true) {
            if (rb != endMarker) {
                receivedBytes[ndx] = rb;
                ndx++;
                if (ndx >= numBytes) {
                    ndx = numBytes - 1;
                }
            }
            else {
                receivedBytes[ndx] = '\0'; // terminate the string
                recvInProgress = false;
                numReceived = ndx;  // save the number for use when printing
                ndx = 0;
                newData = true;
            }
        }

        else if (rb == startMarker) {
            recvInProgress = true;
        }
    }
}

void showNewData() {
    if (newData == true) {
        Serial.print("This just in (HEX values)... ");
        for (byte n = 0; n < numReceived; n++) {
            Serial.print(receivedBytes[n], HEX);
            Serial.print(' ');
        }
        Serial.println();
        newData = false;
    }
}

If i do

    Serial1.write(359 && 0xFF);  // send low byte
    Serial1.write(359);

Then I receive 167

“This just in (HEX values)… 1 67”

So i’m guessing, I have to work out then send low byte first?

itgeek2015:
So i'm guessing, I have to work out then send low byte first?

You have to send integers bigger than 255 as two separate bytes. But if you are writing the code at both ends it does not matter which you send first (as long as you are consistent :slight_smile: }

However, is it really necessary to send them as binary values? Why not just use Serial.print(359); ?

...R
PS. Have you looked at this Python Binary Data demo which uses structs to "reconstitute" the received binary data.

Oh ok, makes sense.

No, i am open to suggestions as I'm struggling to find a method that works for my needs. So many different suggestions online!!

How would you process it?

Thanks

itgeek2015:
How would you process it?

As in the link I gave you in Reply #1. Look at the parse example.

...R

I guess, its something I'll have to figure.

Thanks to you all.