Send a hex block of data Serial port from matlab

45 ビュー (過去 30 日間)
Michael Mathew
Michael Mathew 2013 年 10 月 7 日
コメント済み: Ferran Gonzalez 2022 年 11 月 8 日
Hi,
I have a problem, I have a serial device which should addressed using a block of data in specific format. The data I am trying to send is [7E 00 12 10 00 00 13 A2 00 40 8B 63 85 08 59 00 00 00 00 00 1A 0C], all in hexadecimal. How to send it in a single go? The command is correct since I am able to send the same via a serial terminal program and the device responds.
I can do the same using a arduino by the following command: Serial.write(array,sizeof(array));
How to do the same kind in matlab?
Kindly please help.
Thanks in advance, Mike

採用された回答

Walter Roberson
Walter Roberson 2013 年 10 月 7 日
fwrite(fid, array)
  3 件のコメント
Walter Roberson
Walter Roberson 2013 年 10 月 8 日
You do NOT want to transmit hex. You want to transmit 8 bit binary. The hex you see is from the monitoring program displaying the binary that way.
fwrite(s, h)
Have you set the serial port Terminator property to no terminator, and the BytesAvailableFcnMode to 'byte' ? And possibly the InputBufferSize as well?
Michael Mathew
Michael Mathew 2013 年 10 月 8 日
Thanks, it worked, plus I found some suggestions in the below link.
Now the device responds.

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

その他の回答 (2 件)

Jan
Jan 2013 年 10 月 7 日
"All in hexadecimal" is not clear. You cannot store values in hexadecimal format like e.g.:
a = [7E 00 12 10 00 00 13 A2 00 40 8B 63 85 08 59 00 00 00 00 00 1A 0C]; % ERROR!
You would need e.g. a string (char vector):
a = ['7E', '00', '12', ...
But to send this, you would convert it to UINT8 values at first, most likely. So please explain this detail.
  2 件のコメント
Michael Mathew
Michael Mathew 2013 年 10 月 8 日
編集済み: Michael Mathew 2013 年 10 月 8 日
Hi Simon, Yup I did the same, following is the code I am trying to execute:
clc; clear all;
serial('COM14','BaudRate',9600);
set(s,'OutputBufferSize',88);
fopen(s);
toNode1={'7E' '00' '12' '10' '01' '00' '13' 'A2' '00' '40' '8B' '63' '85' '08' '59' '00' '00' '00' '00' '00' '1A' '1F'};
h=hex2dec(toNode1);
h1=(dec2hex(h)); %just making sure its hex itself; because none was working
binblockwrite(s,h1); %tried fwrite(s,h) but didn't work
b=fscanf(s);
fclose(s);
but it doesn't work. I want something like in the attachment. Please see the image attached.
Jan
Jan 2013 年 10 月 8 日
"It does not work" is not useful to explain the occurring problems. Please tell us, what happens.

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


Michael Mathew
Michael Mathew 2013 年 10 月 8 日
Hey all,
Thanks for the suggestions:
I got my answer in the following link:
  3 件のコメント
Kritika Vashishtha
Kritika Vashishtha 2020 年 5 月 8 日
Hey Michael, Can you post the answer. The link that you shared no more exists.
Ferran Gonzalez
Ferran Gonzalez 2022 年 11 月 8 日

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

Community Treasure Hunt

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

Start Hunting!

Translated by