Skip to main content
deleted 2 characters in body
Source Link
bot3663369
  • 271
  • 1
  • 3

You cannot send larger values because byte only covers the range from 0-255.

To send larger values, you can break your int variable into 2 byte variables. Here's an example:

// On Arduino
int myVar = -123;
byte myVar_HighByte = myVar>>8; // get the high byte
byte myVar_LowByte = myVar; // get the low byte
// x86 compatible machines are little-endian so we send the low byte first
Serial.write(myVar_LowByte); 
Serial.write(myVar_HighByte);

% On MATLAB
s = serial('COM10')
fopen(s)
myVar = fread(s,1,'int16')

Disclaimer: Syntax might not be entirely correct, since I have no MATLAB or Arduino near me when I typed this. But you should get the idea. ;-)

Edit: On second thought, it might be easier to use a pointer.

// On Arduino
float myFloat = 3.14159265359;
byte* ptr = (byte*) (&myFloat);
Serial.write(*ptr++);
Serial.write(*ptr++);
Serial.write(*ptr++);
Serial.write(*ptr++*ptr);

% On MATLAB
s = serial('COM10')
fopen(s)
myVar = fread(s,1,'float')

Having said that, you will still have to take care of the endianness, if you use a different microcontroller.

You cannot send larger values because byte only covers the range from 0-255.

To send larger values, you can break your int variable into 2 byte variables. Here's an example:

// On Arduino
int myVar = -123;
byte myVar_HighByte = myVar>>8; // get the high byte
byte myVar_LowByte = myVar; // get the low byte
// x86 compatible machines are little-endian so we send the low byte first
Serial.write(myVar_LowByte); 
Serial.write(myVar_HighByte);

% On MATLAB
s = serial('COM10')
fopen(s)
myVar = fread(s,1,'int16')

Disclaimer: Syntax might not be entirely correct, since I have no MATLAB or Arduino near me when I typed this. But you should get the idea. ;-)

Edit: On second thought, it might be easier to use a pointer.

// On Arduino
float myFloat = 3.14159265359;
byte* ptr = (byte*) (&myFloat);
Serial.write(*ptr++);
Serial.write(*ptr++);
Serial.write(*ptr++);
Serial.write(*ptr++);

% On MATLAB
s = serial('COM10')
fopen(s)
myVar = fread(s,1,'float')

Having said that, you will still have to take care of the endianness, if you use a different microcontroller.

You cannot send larger values because byte only covers the range from 0-255.

To send larger values, you can break your int variable into 2 byte variables. Here's an example:

// On Arduino
int myVar = -123;
byte myVar_HighByte = myVar>>8; // get the high byte
byte myVar_LowByte = myVar; // get the low byte
// x86 compatible machines are little-endian so we send the low byte first
Serial.write(myVar_LowByte); 
Serial.write(myVar_HighByte);

% On MATLAB
s = serial('COM10')
fopen(s)
myVar = fread(s,1,'int16')

Disclaimer: Syntax might not be entirely correct, since I have no MATLAB or Arduino near me when I typed this. But you should get the idea. ;-)

Edit: On second thought, it might be easier to use a pointer.

// On Arduino
float myFloat = 3.14159265359;
byte* ptr = (byte*) (&myFloat);
Serial.write(*ptr++);
Serial.write(*ptr++);
Serial.write(*ptr++);
Serial.write(*ptr);

% On MATLAB
s = serial('COM10')
fopen(s)
myVar = fread(s,1,'float')

Having said that, you will still have to take care of the endianness, if you use a different microcontroller.

added 518 characters in body
Source Link
bot3663369
  • 271
  • 1
  • 3

You cannot send larger values because byte only covers the range from 0-255.

To send larger values, you can break your int variable into 2 byte variables. Here's an example:

// On Arduino
int myVar = -123;
byte myVar_HighByte = myVar>>8; // get the high byte
byte myVar_LowByte = myVar; // get the low byte
// x86 compatible machines are little-endian so we send the low byte first
Serial.write(myVar_LowByte); 
Serial.write(myVar_HighByte);

% On MATLAB
s = serial('COM10')
fopen(s)
myVar = fread(s,1,'int16')

Disclaimer: Syntax might not be entirely correct, since I have no MATLAB or Arduino near me when I typed this. But you should get the idea. ;-)

Edit: On second thought, it might be easier to use a pointer.

// On Arduino
float myFloat = 3.14159265359;
byte* ptr = (byte*) (&myFloat);
Serial.write(*ptr++);
Serial.write(*ptr++);
Serial.write(*ptr++);
Serial.write(*ptr++);

% On MATLAB
s = serial('COM10')
fopen(s)
myVar = fread(s,1,'float')

Having said that, you will still have to take care of the endianness, if you use a different microcontroller.

You cannot send larger values because byte only covers the range from 0-255.

To send larger values, you can break your int variable into 2 byte variables. Here's an example:

// On Arduino
int myVar = -123;
byte myVar_HighByte = myVar>>8;
byte myVar_LowByte = myVar;
// x86 compatible machines are little-endian so we send the low byte first
Serial.write(myVar_LowByte); 
Serial.write(myVar_HighByte);

% On MATLAB
s = serial('COM10')
fopen(s)
myVar = fread(s,1,'int16')

Disclaimer: Syntax might not be entirely correct, since I have no MATLAB or Arduino near me when I typed this. But you should get the idea. ;-)

You cannot send larger values because byte only covers the range from 0-255.

To send larger values, you can break your int variable into 2 byte variables. Here's an example:

// On Arduino
int myVar = -123;
byte myVar_HighByte = myVar>>8; // get the high byte
byte myVar_LowByte = myVar; // get the low byte
// x86 compatible machines are little-endian so we send the low byte first
Serial.write(myVar_LowByte); 
Serial.write(myVar_HighByte);

% On MATLAB
s = serial('COM10')
fopen(s)
myVar = fread(s,1,'int16')

Disclaimer: Syntax might not be entirely correct, since I have no MATLAB or Arduino near me when I typed this. But you should get the idea. ;-)

Edit: On second thought, it might be easier to use a pointer.

// On Arduino
float myFloat = 3.14159265359;
byte* ptr = (byte*) (&myFloat);
Serial.write(*ptr++);
Serial.write(*ptr++);
Serial.write(*ptr++);
Serial.write(*ptr++);

% On MATLAB
s = serial('COM10')
fopen(s)
myVar = fread(s,1,'float')

Having said that, you will still have to take care of the endianness, if you use a different microcontroller.

added 93 characters in body
Source Link
bot3663369
  • 271
  • 1
  • 3

You cannot send larger values because unsigned byte only covers the range from 0-255.

To send larger values, you can break your int variable into 2 unsigned byte variables. Here's an example:

// On Arduino
int myVar = -123;
unsigned byte myVar_HighByte = myVar>>8;
unsigned byte myVar_LowByte = myVar;
// x86 compatible machines are little-endian so we send the low byte first
Serial.write(myVar_HighBytemyVar_LowByte); 
Serial.write(myVar_LowBytemyVar_HighByte);

//% On MATLAB
myVars = bitshiftserial(myVar_HighByte,8'COM10') +
fopen(s)
myVar myVar_LowByte= ;fread(s,1,'int16')

Disclaimer: Syntax might not be entirely correct, since I have no MATLAB or Arduino near me when I typed this. But you should get the idea. ;-)

You cannot send larger values because unsigned byte only covers the range from 0-255.

To send larger values, you can break your int variable into 2 unsigned byte variables. Here's an example:

// On Arduino
int myVar = -123;
unsigned byte myVar_HighByte = myVar>>8;
unsigned byte myVar_LowByte = myVar;
Serial.write(myVar_HighByte);
Serial.write(myVar_LowByte);

// On MATLAB
myVar = bitshift(myVar_HighByte,8) + myVar_LowByte ;

Disclaimer: Syntax might not be entirely correct, since I have no MATLAB or Arduino near me when I typed this. But you should get the idea. ;-)

You cannot send larger values because byte only covers the range from 0-255.

To send larger values, you can break your int variable into 2 byte variables. Here's an example:

// On Arduino
int myVar = -123;
byte myVar_HighByte = myVar>>8;
byte myVar_LowByte = myVar;
// x86 compatible machines are little-endian so we send the low byte first
Serial.write(myVar_LowByte); 
Serial.write(myVar_HighByte);

% On MATLAB
s = serial('COM10')
fopen(s)
myVar = fread(s,1,'int16')

Disclaimer: Syntax might not be entirely correct, since I have no MATLAB or Arduino near me when I typed this. But you should get the idea. ;-)

Source Link
bot3663369
  • 271
  • 1
  • 3
Loading