【问题标题】:Error sending temperature data from Arduino to Matlab将温度数据从 Arduino 发送到 Matlab 时出错
【发布时间】: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


【解决方案1】:

fscanf 将在找到第一个终止符时返回。这通常是回车 (CR)。由于您使用的是Serial.print(bme.readTemperature());,因此 Arduino 永远不会发送 CR。试试Serial.println(bme.readTemperature());

【讨论】:

  • 你好。感谢你的回复。它显示错误消息,但来自温度(即 26.32 度)的数据从未发送到 Matlab。相反,它显示的是 NaN。没有发送值的可能原因是什么?
  • 我设法通过将 fopen(s) 和 fclose(s) 置于 while 循环之外来解决问题。它显示了值,但对于前几个值,它显示的是 NaN。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-06-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-24
  • 2015-06-09
  • 1970-01-01
相关资源
最近更新 更多