Effacer les filtres
Effacer les filtres

How can I empty the serial port buffer in MATLAB without using the Instrument Control Toolbox?

33 vues (au cours des 30 derniers jours)
I am using core MATLAB to communicate via the serial port (not the Instrument Control Toolbox).
I would like to clear the serial port (RS-232) buffer from MATLAB.

Réponse acceptée

MathWorks Support Team
MathWorks Support Team le 27 Juin 2009
One way to clear the contents of the serial port buffer is to do a simple
fscanf(serialobj)
to read everything in the buffer until the first terminator, which is 'LF' by default. If there is a terminator in the buffer before the last character of the buffer then the previous code will not clear the buffer completely.
Alternatively, you can read the available bytes in the buffer of the serialobj
and use the following code:
fread(serialobj, serialobj.BytesAvailable)
to read all the contents of the buffer. It should be noted that FREAD will return data in the binary format (as opposed to ASCII).
Disconnecting and then reconnecting to the serial object via FCLOSE and FOPEN will also flush the serial buffer. However this approach is computationally much less efficient than the solutions above.

Plus de réponses (0)

Catégories

En savoir plus sur Instrument Connection and Communication dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by