send array from matlab using serial communication

1 回表示 (過去 30 日間)
Yefta Devian
Yefta Devian 2015 年 5 月 18 日
コメント済み: Saishka Naidoo 2021 年 1 月 27 日
hello, i need your help.
i want to send array from matlab to arduino using serial communication
here is my code
s1=serial('com1','BaudRate',9600)
fopen(s1)
A=[1,0,1,0,1,0]
fwrite(s1,A)
but it seems didnt work, because my arduino shows nothing. and can u help me how to print data which i sent?
regards,
yefta (<mailto:yeftadp@gmail.com yeftadp@gmail.com>)
  1 件のコメント
tamim ahmed
tamim ahmed 2016 年 12 月 4 日
what is the arduino code to receive this data in arduino. TIA

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

採用された回答

Walter Roberson
Walter Roberson 2015 年 5 月 18 日
Notice that fwrite() sends binary data. And note that unless you tell MATLAB otherwise, constants such as 0 and 1 are double precision numbers, which occupy 8 bytes.
Possibly you want
fprintf(s1, '%d', A)
that would send the character string '1' '0' '1' and so on.
You might also want to send a newline to end the line:
fprintf(s1, '\n');
  4 件のコメント
Walter Roberson
Walter Roberson 2017 年 4 月 19 日
In the case where you know ahead of time which values will be integer, then something like,
fprintf(s1, '%f\n%d\n%f\n%d\n', A)
Otherwise,
fprintf(s1, '%g\n', A)
Naseeb Gill
Naseeb Gill 2017 年 4 月 20 日
編集済み: Walter Roberson 2017 年 4 月 20 日
Thanks @walter Roberson for your reply. I used fprintf(s1, '%g\n', A) as you suggested but didn't get any output. I can't paste my whole code here so I open a new question for the same which link I'm giving here. It will be helful for me if you suggest solution to my problem. https://in.mathworks.com/matlabcentral/answers/336393-send-numerical-values-from-matlab-to-arduino
Thank you

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

その他の回答 (1 件)

Yefta Devian
Yefta Devian 2015 年 5 月 19 日
hei Walter Roberson, you rock man
its work!! thankyou so much brother
regards,
yefta
  1 件のコメント
Saishka Naidoo
Saishka Naidoo 2021 年 1 月 27 日
Please can you attach the matlab and arduino code for what you have done.

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

カテゴリ

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