equivalent of BytesAvailable in serialport?
33 ビュー (過去 30 日間)
古いコメントを表示
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!
0 件のコメント
回答 (1 件)
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!
5 件のコメント
Sam
2025 年 2 月 8 日 18:44
Hi @Walter Roberson, does this mean that the buffer stores all data you write to the serial object until you read it, regardless of how much data this is? Is there a maximum number of bytes the buffer will store? Thanks!
Walter Roberson
2025 年 2 月 8 日 18:59
Sorry, I do not know that.
I see that in https://www.mathworks.com/matlabcentral/answers/1953714-streaming-data-using-serialport#comment_2758899 I posted
The newer serialport() has no documented buffer size control. In practice, though, it has hidden user-settable properties InputBufferSize and OutputBufferSize . With the properties not being documented, I cannot promise that it will actually use the buffer size that is set.
Historically, Mathworks has occasionally created object properties that they intend to implement in future, but hard-coding behaviour for a while, or keeping the properties hidden until they are finished testing or finished getting bugs out to their satisfaction. So the fact that the properties exist does not mean that they are used or that they function properly at this point.
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!