필터 지우기
필터 지우기

equivalent of BytesAvailable in serialport?

조회 수: 24 (최근 30일)
Arthur Zhang
Arthur Zhang 2024년 5월 2일
댓글: Arthur Zhang 2024년 5월 17일
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!

답변 (1개)

Poorna
Poorna 2024년 5월 17일
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
Walter Roberson
Walter Roberson 2024년 5월 17일
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 2024년 5월 17일
Thanks Walter, I'll give it a try.

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by