我的arduino代码不读取从matlab输入的任何字符

时间:2014-03-02 05:36:52

标签: matlab serialization serial-port arduino

我正在进行从matlab到arduino的串行通信。我的代码写在下面。我只是需要建议或一些建议,因为我不确定我的arduino是否真正读取我在matlab中输入的确切字符串。但是当我运行matlab本身时,fscan正在读取并显示正确的字符集。请问我的代码是对的吗?我真的很困惑。

==== MATLAB代码======

delete(instrfindall)

text=1;

s = serial('COM7', 'BaudRate', 38400);

fopen(s);

pause(0.1)

text=input('enter:  ','s');

fprintf(s, '%s\n', text);

pause(0.1);

fscanf(s)

pause(0.1)

delete(instrfindall)

==== ARDUINO CODE =====

String inData; // Where to store string
char received; // Where to store the character read // Index into array; where to store the character


void setup(){
Serial.begin(38400);
}

void loop()
{   
while(Serial.available() > 0) // Do not execute if one less than the size of the array
   {
       char received = Serial.read(); // Read a character
       inData += received; 
       if(received == '\n');
     {
    Serial.print(inData);  
    inData="";
 }
   }

    }

1 个答案:

答案 0 :(得分:0)

你永远不会写'\ n',因为input()matlab函数将其删除。所以只需在text的末尾添加它,一切都会奏效。另外要注意,好像inData比100个字符更大的数字,你可能会用完ram而且结果很奇怪。