RS232 with MATLAB: Doesn't send what I want

1 view (last 30 days)
Julia
Julia on 30 Oct 2014
Commented: Julia on 30 Oct 2014
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 Comment
Julia
Julia on 30 Oct 2014
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!

Sign in to comment.

Answers (0)

Categories

Find more on Language Fundamentals in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!