receive data with specific format via serial communication

2 views (last 30 days)
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 ?

Accepted Answer

Walter Roberson
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
khaled saad
khaled saad on 14 May 2020
You were right about that the serial communication corrupt characters from time to time. and i sometimes got this message will running the code :
Warning: A timeout occurred before the Terminator was reached.
'serial' unable to read all requested data. For more information on possible reasons, see Serial Read Warnings.
for the control algorithm this isn't allowed so i need to protect the process from such a corruption.For the same format everytime ('*3QD%d'), Is there's any way to be confident that the feedback value is always correct (sscanf() won't fail) ?

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!