2016-05-28 41 views
1

SimulinkからArduino Unoに数値データを送信したい。SimulinkからArduino Unoへの数値データ

私はSimulinkで動作させる方法がわからないので、私はMatlabを試しています。

このコードは数値データをcharとして送信します。 Arduinoに一度に一人のキャラクター。その後、文字を連結して数値を構成し、それをArduinoに渡して治療する必要があります。その後、同じ方法でMatlabに送り返します。

数字データをArduinoに送信し、数値データとしてMatlab/simulinkに送り返す可能性があるかどうかを知りたいと思います。

これは、私はMATLABで使用しているコードです:

close all; clear all ; clc; 
delete (instrfind({'Port'},{'COM5'})) 

s = serial('COM5'); 

set(s,'DataBits',8); 
set(s,'StopBits',1); 
set(s,'BaudRate',4800); 
set(s,'Parity','none'); 

fopen(s) 

while (1) 
    if (s.BytesAvailable) 
     readData=fscanf(s) 
    else 
     fprintf(s,'arduino'); 
    end 
end 

fclose(s) 

そして、これは私はArduinoの中で使用しているコードです:

int sensorPin = A0; 
int sensorValue = 0; 
char incomingData; 

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

void loop() { 

    if (Serial.available() > 0) 
    { 
     incomingData = Serial.read(); //read incoming data 
     Serial.println(incomingData); 
     delay(100); 
    } 
    else { 

     sensorValue = analogRead(sensorPin); 
     Serial.println(sensorValue);  
     delay(100); 
    } 
} 

答えて

0

私はすでに上記の質問をします。そして今、私は答えを見つけたので、あなたと共有したいと思いました。

まず、シリアルポートとの通信をすべて終了し、通信の値を初期化する必要があります。あなたはこのようにします。

close all ; clc; 
delete (instrfind({'Port'},{'COM3'})) 
s = serial('COM3'); 
set(s,'DataBits',8); 
set(s,'StopBits',1); 
set(s,'BaudRate',9600); 
set(s,'Parity','none'); 

レマルク:あなたが使用しているポートあなたのArduinoに表示する必要がありますので、あなたが、「COM3」ポートを持っていないすべての時間。また、MatlabとArduinoでも "BaudRate"のexacte値を作らなければなりません。

第二に、あなたは、あなたがこのようにそれを読んで、フロート番号を送信します。

YourValue = 123.456; % This is just an exemple 
while (1) 
    fprintf(s,'%f',YourValue); % sendig the value to the Arduino 
    readData=fscanf(s,'%f')  % receiving the value from the Arduino and printing it 
end 
fclose(s) 

さて、Arduinoの部分のために、それは簡単です。コードは以下のように提示されています

int sensorPin = A0; // select the input pin for the potentiometer 
int sensorValue = 0; // variable to store the value coming from the sensor 
float incomingData; 

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

void loop() { 

    if (Serial.available()) 
    { 
    incomingData = Serial.parseFloat(); //read incoming data 
    delay(100); 
    } 
    else 
    { 
    Serial.println(incomingData); 
    delay(100); 
    } 
} 

レマルク:私はアルドゥイーノにMathWorks社のMATLABから多くの値を送信すると、その後、私はMathWorks社のMATLABにそれらを送り返します。私は最初の値が常にこのように印刷されている[]またはこの方法は0です。問題の内容はわかりません。

ありがとうございます!

関連する問題