receive data with specific format via serial communication
2 views (last 30 days)
Show older comments
khaled saad
on 29 Apr 2020
Commented: khaled saad
on 14 May 2020
i'm controlling a motor working on serial communication. it send the feedback as ( #(motor ID)(command)(value)<cr>) and i need to onle extract the (value)
i used this (feedback = fscanf(s , '*1QD%u<cr>') and feedback = fscanf(s , '*1QD%u') ) to read the value of (motor with ID 1) and for the command of (QD) and this warning always appear :
Warning: Matching failure in format. The format specified for conversion, '*1QD%u', is incorrect.
is there's any other format should i use ?
0 Comments
Accepted Answer
Walter Roberson
on 29 Apr 2020
In situations where you are reading data to end of line, it is good programming practice to fgets() or fgetl(), and then to manipulate the result, such as by sscanf() or textscan() or regexp() or regexprep() .
Serial ports should be assumed to drop characters or corrupt characters from time to time, and should typically be assumed to be connected to objects outside of your strict control that might enter unexpect states (such as being rebooted or encountering a power glitch).
You should never assume that a serial port is in the proper state: you should receive data and test whether it is acceptable for the current state, and process it if it is.
... And it is easier to debug if you do this.
7 Comments
Walter Roberson
on 7 May 2020
You cannot. The Serial Send Block only accepts bytes. You should use a block to convert to ascii.
More Answers (0)
See Also
Categories
Find more on String in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!