Clear Filters
Clear Filters

equivalent of BytesAvailable in serialport?

24 views (last 30 days)
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!

Answers (1)

Poorna
Poorna on 17 May 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 Comments
Walter Roberson
Walter Roberson on 17 May 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))

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!