How to send a set of bytes in fwrite or fprintf command?

조회 수: 2 (최근 30일)
Fattah Hanafi
Fattah Hanafi 2012년 10월 30일
Hello
Recently I bought MD49 driver and EMG49 motor. Driver document said:
The commands are sent with a sync byte of 0 at the start and then the command followed by any data bytes. The MD49 will then respond if the command is applicable.
For example to read the battery voltage, send: 0x00 - sync byte 0x26 - READ VOLTS command and the MD49 would respond with 0x18 - returned byte (24 decimal) 24v
But I don't now how to send a set of bytes like that with fwrite or fprintf command.
Thanks

답변 (2개)

Honglei Chen
Honglei Chen 2012년 10월 30일
Here is an example using sprintf, but fprintf is similar
>> sprintf('0x%02x',24)
ans =
0x18

Walter Roberson
Walter Roberson 2012년 10월 30일
Use hex2dec() or sscanf() to convert the hex representation to numeric representation. fwrite() that.
fwrite(s, uint8(hex2dec('26'))
or
fwrite(s, hex2dec('26'), 'uint8')
or
fwrite(s, sscanf('%x', '26'), 'uint8')
or
fwrite(s, sscanf('%i', '0x26'), 'uint8')

카테고리

Help CenterFile Exchange에서 Specialized Power Systems에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by