equivalent of BytesAvailable in serialport?

34 visualizzazioni (ultimi 30 giorni)
Arthur Zhang
Arthur Zhang il 2 Mag 2024
Commentato: Arthur Zhang il 17 Mag 2024
Hi, I am trying to migrate from `serial` to serialport interface, and having a hard time accessing BytesAvailable in `serialport`. While using serial, I could access the input buffer and get all bytes using `serial.BytesAvailable` which by default is 512. So with `fread` I get a matrix of 512 *1. However, when using `serialport` interface, NumBytesAvailable does not give me the same info.
Could anyone help me please? Thanks!

Risposte (1)

Poorna
Poorna il 17 Mag 2024
Hi Arthur,
It is to be noted that "BytesAvailable" and "NumBytesAvailable" behave the same way. They return the number of bytes available to read in the input buffer. When you say "I could access the input buffer and get all bytes using 'serial.BytesAvailable' which by default is 512", It is not true that the default value of the "BytesAvailable" is 512, the default value of the "BytesAvailable" property is 0. It is set to zero as soon you open the serial port by using the "fopen"' function.
It is difficult to understand what went wrong while you were using the "NumBytesAvailable" property of the "serialport" object without the actual code. Also, the "read" function is the equivalent of the "fread" function when you switch from "serial" to "serialport". Please refer to the following documentation about transitioning from "serial" to "serialport":
To know more about "serial" and "serialport", refer to the below documentation:
Hope this helps!
  3 Commenti
Walter Roberson
Walter Roberson il 17 Mag 2024
serialport() objects manage the buffer internally, adjusting the size as needed.
If you only need 512 bytes out of the 8053 currently available, then
read(OBJECT, min(512,OBJECT.NumBytesAvailable))
Arthur Zhang
Arthur Zhang il 17 Mag 2024
Thanks Walter, I'll give it a try.

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by