[英]Matlab arduino serial communication
I am trying to send some data from Matlab to arduino. 我正在尝试将一些数据从Matlab发送到arduino。 It is a matrix of [1 by 2]. 它是[1 x 2]的矩阵。 My plan is to convert this two numbers in to a string and send to the arduino. 我的计划是将这两个数字转换成字符串并发送给arduino。 However in the serial monitor, I can not read any value coming from matlab. 但是,在串行监视器中,我无法读取任何来自matlab的值。
This is my matlab code, 这是我的matlab代码,
val_a = matt(n,:);
A = [val_a];
asd = A(1:1);
asb = A(:,2);
strA = num2str(asd);
strB = num2str(asb);
comma = ',';
endVal = '#';
theString = strcat(strA,comma,endVal);
obj1 = instrfind('Type', 'serial', 'Port', 'COM19', 'Tag', '');
if isempty(obj1)
obj1 = serial('COM19');
else
fclose(obj1);
obj1 = obj1(1);
end
fopen(obj1);
fprintf(obj1,theString)
fclose(obj1);
delete(obj1);
A = [];
And this is the serial event of arduino 这是arduino的系列事件
bool gotalfa = false;
bool event = false;
void serialEvent() {
while (Serial.available())
{
char inChar = (char)Serial.read();
event = true;
if (inChar == , && !gotalfa)
{
alfa = inputString;
inputString = "";
gotalfa = true;
event = false;
}
if (inChar == '#' && gotalfa)
{
theta = inputString;
gotalfa = false;
inputString = "";
Serial.print("alfa ");
Serial.print(alfa);
Serial.print("theta ");
Serial.println(theta);
//some program....
event = false;
}
if(event)
{
inputString += inChar;
}}}
Do I have to change anything in my matlab/arduino code. 我是否需要更改matlab / arduino代码中的任何内容。 Any helpful tip is greatly appreciated. 任何有用的提示,我们将不胜感激。
Thank you in advance 先感谢您
Just a tip, hopefully it points you to the answer. 只是一个提示,希望它为您指出了答案。
I would use a terminal program like putty or teraterm to isolate the root cause of your error. 我将使用腻子或teraterm之类的终端程序来找出错误的根本原因。
ie Run putty/teraterm to send certain string to your serial port of your arduino and see if it does returns the expected strings. 即运行putty / teraterm将某些字符串发送到您的arduino的串行端口,并查看它是否确实返回了预期的字符串。
http://www.putty.org/ https://ttssh2.osdn.jp/index.html.en http://www.putty.org/ https://ttssh2.osdn.jp/index.html.en
Also, consider using the ReadStringUntil in Arduino. 另外,考虑在Arduino中使用ReadStringUntil。 https://www.arduino.cc/en/Serial/ReadStringUntil https://www.arduino.cc/en/Serial/ReadStringUntil
Hope it helps. 希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.