【发布时间】:2020-02-17 22:10:55
【问题描述】:
我正在尝试使用串行通信将温度传感器数据从 arduino 发送到 matlab。温度数据显示在 Arduino 串行监视器上,但我无法在 Matlab 中获取数据。发生以下错误。我已经尝试了所有可能的方法来通过在 fscanf 之前放置一个 pause(3) 来获取数据,但它仍然不起作用。
警告:读取失败:在到达终止符之前发生超时。 “串行”无法读取任何数据。有关可能原因的更多信息,请参阅串行读取警告。 钠氮
我的matlab代码如下
if ~isempty(instrfind)
fclose(instrfind);
delete (instrfind);
end
waitTime = 10;
s = serial('COM4','BAUD',9600);
waitTime = duration(0,waitTime,0);
t = datetime('now') - startTime;
while t < waitTime
fopen(s);
pause(3);
idn = fscanf(s);
fclose(s);
serialData = str2double(idn);
corrData = serialData;
disp(corrData);
end
clear a;
clear s;
【问题讨论】:
-
循环中的 fopen 和 close 可能是错误的。通常你打开连接并让它保持打开状态,直到所有内容都被读取。 arduino 发送什么样的数据?文本数据?
-
我不确定 arduino 发送什么样的数据,因为我是 arduino 的绝对初学者,但我正在向 matlab 发送温度传感器数据。数据使用 Serial.print(bme.readTemperature()); 显示在 arduino 中。
标签: matlab arduino serial-port