Why does MATLAB print a special character '?' when I read from my hardware device?
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
MathWorks Support Team
le 20 Sep 2019
Modifié(e) : MathWorks Support Team
le 30 Déc 2020
I am connecting to a hardware device via a serial interface, writing and reading data to and from that device. Reading '80' from the device indicates successful communication.
Why does MATLAB print a special character with the data read from my device?
s = serial('COM4');
set(s,'Baudrate',19200,'Terminator','CR/LF');
fopen(s);
fprintf(s,'cmd401');
fscanf(s)
ans =
80
Réponse acceptée
MathWorks Support Team
le 30 Déc 2020
Modifié(e) : MathWorks Support Team
le 30 Déc 2020
Using the 'unicode2native' function in MATLAB reveals that this special character has a decimal value 6, which corresponds to the Unicode 'Acknowledgement' control character.
The character that is being returned before '80' is the Acknowledgement Character(ACK), which is a commonly used signal in communication protocols that indicates successful receipt of the message.
While the '80' may be a user-configured success signal, the 'ACK' character is likely being generated by the device to indicate a successful action or the receipt of a command. The device documentation may be able to confirm this.
Since the 'ACK' is generated by the device, it may not be possible to suppress it but there are a number of ways to ignore it.
To receive the '80' without the 'ACK' character, try the following workarounds:
1) Use 'fscanf' to read just a single character in order to clear the 'ACK', and then read the remaining characters, separately. For example:
fscanf(s, '%c', 1); % read in the first character in the device's output buffer
fscanf(s) % read the remaining characters. This should return '80'.
2) Use indexing to trim the leading character from the character array you read from the device. For example:
cmd_success = fscanf(s);
cmd_success = cmd_success(2:end)
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Analog Input and Output dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!