fwrite do not write numbers 1-32

조회 수: 11 (최근 30일)
prajjwal prem
prajjwal prem 2011년 11월 29일
i need to write a byte to a serial port say '24'. i am using the following code(this is for standard output) fwrite(1,[24],'uint8') but it writes a blank space but if i write any number greater than or equal to 33 it writes some character on the screen. any clues whats going on or what is my mistake fprintf is still worse it gives blank space from 1-150 or near about.

답변 (2개)

Fangjun Jiang
Fangjun Jiang 2011년 11월 29일
You are right, file ID 1 is the standard output. You are writing binary numbers but the standard output only shows the corresponding ASCII characters.
Your command will be the same as char(24) or char(33) for any other numbers.

Walter Roberson
Walter Roberson 2011년 11월 29일
What behavior are you wanting? fwrite() transfers the data in binary. Standard output interprets binary as indicating characters, and displays the characters corresponding to each byte (or pair of bytes perhaps.) Character 24 is the CAN control function, which is a non-printing character in most fonts.
You could change your font to Wingdings to see those characters (except 0), but you would then of course then have problems seeing your regular output.
If you want to see the characters '2' and then '4' when you write out 24, then you should be using fprintf() instead of fwrite()
  댓글 수: 2
prajjwal prem
prajjwal prem 2011년 11월 30일
actually i have to set a parameter in a micro-controller from 0-255
when i set it to say 50 it works fine i.e. micro-controller returns a response, but if i set it to 5 it does not respond
i want to know if matlab writes 00000101 for 5 or it writes something else at the connected port when using fwrite
Walter Roberson
Walter Roberson 2011년 11월 30일
MATLAB would indeed write binary 00000101 in that situation (since you are specifying uint8 output.)
Are you certain that the microcontroller is expecting the parameter to be in binary? Some want the commands in binary, some want them in text.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by