RS232 with MATLAB: Doesn't send what I want

1 次查看(过去 30 天)
Hello everyone! I am using Matlab to communicate with a device on a RS232 connexion. My code is quite simple: I first open the correct COM port: obj=serial('COM3','BaudRate',9600); fopen(obj);
And send I send various ASCII commands. For example: fprintf(obj,'%s\n',[char(hex2dec('A1'))])
I have a sniffer on the RS232 port, so I can make sure I am sending the right command. in this case 'A1'.
However, sometimes when I try to send '8B' it sends '3F' instead (shown by the sniffer): fprintf(obj,'%s\n',[char(hex2dec('8B'))])
This is bizarre, because sometimes it works perfectly fine and for some ASCII characters, it doesn't work!
Do you know why this happens? And what can I do to avoid this? Are there other alternatives to communicate on RS232 serial port with MATLAB?
Thank you in advance for your response and your time!
  1 个评论
Julia
Julia 2014-10-30
ok so I understood something. When the characters I send are larger than 127 (so when the 8th bit is a 1: ex. 10000000 and larger), Matlab doesn't send the right number. This is why everything worked well for most of the characters, but as soon as I send a value larger than 127 (80 in hex) it didn't work properly.
Does someone know how I can avoid this? I guess Matlab considers the 8th bit as a sign or something...
Thank you!

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Language Fundamentals 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by