How to clear the serial buffer of Raspberry Pi

조회 수: 14 (최근 30일)
靖邦
靖邦 2023년 12월 26일
댓글: Walter Roberson 2023년 12월 27일
Hi
I use MATLAB to control the serial communication of Raspberry Pi, but its communication frequency is too fast, and the serial buffer is easily full. I want to know how to clear the serial buffer?
Here is my code:
clear all
r = raspi;
pc = serialdev(r,'/dev/serial0',9600,8,'none',1);
pc.Timeout = 1;
out = 0;
data1 = xlsread('data.xlsx',1);
tou = interp1(0:3600:23*3600,data1,0:1:24*3600-1,'spline')';
tou = [tou;tou];
load('electricity_price.mat');
money = money/0.58*0.86;
price = [0:24*3600-1;kron(money,ones(1,3600))]';
price = [price;price];
while true
try
data = read(pc,2,'int32');
if length(data) > 0
Troom = double(data(1))/1000;
t = double(data(2));
tout = tou(t+1:900:t+6*900);
P = price(t+1:900:t+6*900,2);
out = look(Troom, tout,P);
end
write(pc, out, 'int8');
pause(0.1)
catch
disp('NOT COMPUTE')
end
end
There is no problem with the connection, but now that the buffer is full, I cannot continue reading or writing

답변 (1개)

Angelo Yeo
Angelo Yeo 2023년 12월 26일
The function flush (since R2019b) clears serial port device buffers. Would this function work for you?
  댓글 수: 3
Angelo Yeo
Angelo Yeo 2023년 12월 27일
Thank you for your reply. Can I ask you how you realized Raspberry Pi does not support flush? Did you get any error message when you tried it?
Walter Roberson
Walter Roberson 2023년 12월 27일
flush() is not a documented method of serialdev

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

카테고리

Help CenterFile Exchange에서 MATLAB Support Package for Raspberry Pi Hardware에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by