Pages: [1]   Go Down
Author Topic: Sending numbers bigger than 255 from Matlab to Arduino  (Read 1005 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm trying to figure out how to send integers that are larger than one byte from Matlab to Arduino through the serial port. For starters, I'm just trying to see if I can send two byte integers and then try to generalize from there. My idea so far is to take a number in Matlab say 2344, and turn it into a 16 digit binary number 0000100100101000. I then break that up into two 8 digit binary numbers 00001001 and 00101000. I then convert those numbers back to decimal numbers 9 and 40, which I send to the Arduino with fwrite(s1,9,'uint8') and fwrite(s1,40,'uint8').

Now I'm trying to reconstruct these numbers back to the original 2344. My idea is to store them in two sequential bytes in the Arduino's memory. So I have 00001001 in one byte and 00101000 in the next byte. Then I tell it to reinterpret those 2 separate bytes as a single 2 byte integer. I'm trying to figure out how to use pointers to reference those memory addresses, but I'm not sure exactly how to code it. Is it even possible to do it this way? I'm kind of new to c++. If anyone has any better ideas I'm open to them.     
Logged

"The old Europe"
Offline Offline
Edison Member
*
Karma: 1
Posts: 2005
Bootloaders suck!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

How about using a union?

Code:
typedef union two_byte_merge {
        struct {
                uint8_t high_byte;
                uint8_t low_byte;
        } parts;
        int16_t s_merged;
        uint16_t u_merged;
} two_byte_merge_t;

two_byte_merge_t combined;

combined.parts.high_byte = 128;
combined.parts.low_byte = 128;

printf("HB: %u\n",combined.parts.high_byte);
printf("LB: %u\n",combined.parts.low_byte);
printf("signed merged: %d\n",combined.s_merged);
printf("unsigned merged: %u\n",combined.u_merged);

Or just:

Code:
uint16_t merged = ((uint16_t)(high_byte) << 8) | (uint16_t)(low_byte);
« Last Edit: August 09, 2011, 03:33:35 am by madworm » Logged

• Upload doesn't work? Do a loop-back test.
• There's absolutely NO excuse for not having an ISP!
• Your AVR needs a brain surgery? Use the online FUSE calculator.
My projects: RGB LED matrix, RGB LED ring, various ATtiny gadgets...
• Microsoft is not the answer. It is the question, and the answer is NO!

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks worked great. Here's the code I ended up writing. I changed some of the data types. As a test I made the LED light up if the correct number was sent (I chose 506).
Code:
typedef union twoByteMerge {
  struct {
    byte secondByte;
    byte firstByte;
  }
  parts;       
  unsigned int merged;
}
twoByteMerge_t;

twoByteMerge_t combined;

void setup() {
  Serial.begin(9600);
  pinMode(13,OUTPUT);
  digitalWrite(13,LOW); 
}

void loop() {

  if (Serial.available() > 0) {

    combined.parts.firstByte = Serial.read();

    while(Serial.available()==0){
    }

    combined.parts.secondByte = Serial.read();

    if (combined.merged==506){
      digitalWrite(13,HIGH);
    }

  }
}

Logged

Pages: [1]   Go Up
Jump to: