Go Down

Topic: HELP~Help~Serial Write and Read of 2byte number (Read 828 times) previous topic - next topic

seanfan84

Jun 30, 2011, 06:53 am Last Edit: Jun 30, 2011, 07:03 am by seanfan84 Reason: 1
Hi, I am using matlab to communicate with Arduino.
The idea is that a computer get some input and translate it into the steps of motors, then send the number of steps to arduino.

I feel really headache when I was trying to communicate.
I don't know how to send numbers bigger than 127 to arduino. Also, Serial.write doesn't have argument, I don't know how to sent large number out from Arduino.

The following CODE works well when sending numbers smaller than 127 from Matlab. LED turns on and off as planed, and Matlab can get correct response from Arduino as well.

Code: [Select]
void setup() {
 Serial.begin(9600);
 pinMode(13,OUTPUT);
}
void loop() {
 if (Serial.available()) {
   int inByte = Serial.read();
   Serial.write(inByte);
 if (inByte==1){digitalWrite(13,HIGH);delay(500);}
 if (inByte==0){digitalWrite(13,LOW);delay(500);}


MATLAB CODE
Code: [Select]

arport=serial('com6','baudrate',9600,'databits',8);
fwrite(arport,1,'int8');
fread(arport,1,'int8');
pause(10);
fwrite(arport,0,'int8');
fread(arport,1,'int8');
fclose(arport);
delete(arport);



However, when I try to send a number bigger than 127, it doesn't work.
Here is the modified CODE.

Code: [Select]

void setup() {
 Serial.begin(9600);
 pinMode(13,OUTPUT);
}
void loop() {
 if (Serial.available()) {
   int inByte = Serial.read();
   Serial.write(inByte);
 if (inByte==32){digitalWrite(13,HIGH);delay(500);}
 if (inByte==512){digitalWrite(13,LOW);delay(500);}


It seems Arduino can't read two bytes at a time.
So I used inByte=inByte<<5 to try the problem.
input 1 LED turns on, input 16 LED turns off. perfect!
But when I try to write shifted inByte ( 512) back to MATLAB, it doesn't work.
I also tried
Code: [Select]
Serial.write(512)

when I read from matlab with
Code: [Select]

fread(arport,1,'int8')  //gives 0
fread(arport,2,'int8')  //gives 0 and a Warning: The specified amount of data was not returned within the Timeout period.
fread(arport,1,'int16') //gives only the same warning.


512 = (00010000 00000000)2
fread(arport,2,'int8')  should give 0 and 16, isn't it?

How to read and write 16 bits ToT  help~

Korman

A byte is 8 bits and cover values from 0 to 255. If you made it unsigned instead of signed, you could also use values between 128 and 255.

For 512, you're out of luck, you won't be able to make it work with a single byte. You need to send 2 bytes and on the Arduino you need to read 2 bytes  before you can reassemble them into a 16-but integer and process it. You should take care to assemble the value in the right order (high byte / low byte first) so that the Arduino gets the value you sent and not something else.

You also should  think about how you keep the sender and the Arduino in sync. Because if the Arduino thinks the second byte is the first, all will look fine to both parties but the received data will all be wrong.

Korman

Go Up