Simple Matlab & Arduino Serial Communication

83 ビュー (過去 30 日間)
Tyler
Tyler 2013 年 7 月 1 日
コメント済み: Ana-Maria Moga 2021 年 7 月 24 日
I am attempting a running the following program and am getting strange results. After opening the serial port, the Arduino sends a "Ready" command to matlab. Then it sends a uint16 (2 bytes) value of 500 to the arduino. The arduino simply echoes what was received back to matlab in HEX.
When I run this I expect readData to equal 0x01F4, but I get 0xC0F4. Any suggestions?
Arduino CODE:
byte incomingByte1;
void setup(){
pinMode(2,OUTPUT);
Serial.begin(115200);
Serial.println("Ready");
}
void loop() {
digitalWrite(2,LOW); //turn off LED
delay(500);
if (Serial.available() > 0) {
digitalWrite(2,HIGH); //flash LED everytime data is available
delay(500);
incomingByte1 = Serial.read(); //read incoming data
Serial.println(incomingByte1,HEX); //print data
}
}
Matlab CODE:
clear
clc
s=serial('COM7','BaudRate',115200);
fopen(s);
readData=fscanf(s) %reads "Ready"
writedata=uint16(500); %0x01F4
fwrite(s,writedata,'uint16') %write data
for i=1:2 %read 2 lines of data
readData=fscanf(s)
end
fclose(s);
delete(s);
OUTPUT:
readData =
Ready
readData =
F4
readData =
C0
  3 件のコメント
avram alter
avram alter 2019 年 2 月 14 日
It's been nearly 6 years, but from the bottom of my heart, thank you. this thing actually worked to solve my problems.
Ana-Maria Moga
Ana-Maria Moga 2021 年 7 月 24 日
Thank you for this!

サインインしてコメントする。

回答 (1 件)

islam dib
islam dib 2020 年 12 月 8 日
Hello,
How can I read data sending from arduino via serial port, in Matlab?
  2 件のコメント
Jan Dustin Tengdyantono
Jan Dustin Tengdyantono 2021 年 2 月 18 日
fscanf(s);
islam dib
islam dib 2021 年 3 月 4 日
I mean not just read,open and read and print them !!?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMATLAB Support Package for Arduino Hardware についてさらに検索

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by