matlab - MATLAB - Arduino 串行通信
问题描述
我想将一个整数从 MATLAB 发送到 Arduino。我希望将整数发回并在 MATLAB 中显示。有时 MATLAB 上不显示任何内容,有时会显示错误的整数,例如 -457。如果有人可以阅读以下代码,我将不胜感激。如果有更简单的方法来解决这个问题,请告诉我。谢谢。
MATLAB代码:
s = serial('/dev/tty.KeySerial1')
set(s,'DataBits',8);
set(s,'StopBits',1);
set(s,'BaudRate',9600);
set(s,'Parity','none');
fopen(s);
a='b';
while (a~='a');
a=fread(s,1,'uchar');
end
if (a=='a')
disp('serial read');
end
fprintf(s,'%c','a');
mbox= msgbox('serial com set up complete'); uiwait(mbox);
fprintf(s, '%d\n', 589367);
fscanf(s,'%s')
Arduino草图:
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
// check serial communication
Serial.println('a');
char a='b';
while (a !='a')
{ a=Serial.read();
}
}
void loop() {
// put your main code here, to run repeatedly:
while(Serial.available()==0);
int val=Serial.parseInt();
Serial.print("integer received: ");
Serial.println(val);
}
解决方案
推荐阅读
- c# - C# - 使用泛型使用派生类型覆盖接口的返回类型
- vue.js - 如何在 VueJS 单页应用程序中添加 Matomo 跟踪代码?
- java - 如何创建一个二维数组,从中心以螺旋形填充?出现错误:线程中的异常...-1 和 9
- r - 更改 flexdashboard 中使用的 shiny::renderText() 内的行
- amazon-s3 - 在 SageMaker 中没有回调 Keras docker 图像的 TensorBoard
- c# - 创建 IQueryable
在 EF 中使用基类进行扩展 - intellij-idea - 如何在 IntelliJ 18.3 中使 Cucumber Java 测试失败?
- azure - 如何向应用程序洞察发送异步调用 (AZURE)
- javascript - 动态 javascript 表不会显示数组中的迭代
- java - 有谁知道如何向 org.w3c.dom.Element 添加一个值来实例化 MiningSchema 和 SupportVectorMachineModel?