Go Down

Topic: Sending numbers bigger than 255 from Matlab to Arduino (Read 1 time) previous topic - next topic

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.   

madworm

#1
Aug 09, 2011, 10:29 am Last Edit: Aug 09, 2011, 10:33 am by madworm Reason: 1
How about using a union?

Code: [Select]
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: [Select]
uint16_t merged = ((uint16_t)(high_byte) << 8) | (uint16_t)(low_byte);
• 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!

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: [Select]
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);
    }

  }
}


MarcoDelta

Ehi xentity1x really good job!

Can I ask you how you've done to separate and send the two byte in matlab?
Because my target is send angle and traslation values that are contain in a matrix. Pratically I isolate each value that could be between o-360 for angle and 0-3600 for tralsation. So  you're program could be a really good solution.

Thanks!

Go Up