フィルターのクリア

I am facing a problem in serial communication of matlab and arduino.

4 ビュー (過去 30 日間)
Haroon  Aziz
Haroon Aziz 2016 年 4 月 23 日
回答済み: husam alrajab 2019 年 6 月 3 日
I am trying to do serial communication of matlab and arduino. Serial communication is not so much difficult between arduino and matlab but my problem is different.
I want to make 3 edit boxes in GUI and want to send the numeric values entered in boxes to arduino. But now i am trying to send only one value from one edit box to arduino. Arduino is not performing the job.
Here is my matlab code.
function edit1_Callback(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
x =str2num(get(handles.edit1,'String'));
disp('Value entered: ')
disp(x)
delete(instrfind({'Port'},{'COM6'}));
s=serial('COM6');
s.BaudRate=9600;
fopen(s);
disp('After OPEN')
disp(s)
fprintf(s,x);
fclose(s);
disp('After CLOSE')
disp(s)
After entering number in edit box i get this in command windows.
Value entered:
10000
After OPEN
Serial Port Object : Serial-COM6
Communication Settings
Port: COM6
BaudRate: 9600
Terminator: 'LF'
Communication State
Status: open
RecordStatus: off
Read/Write State
TransferStatus: idle
BytesAvailable: 0
ValuesReceived: 0
ValuesSent: 0
After CLOSE
Serial Port Object : Serial-COM6
Communication Settings
Port: COM6
BaudRate: 9600
Terminator: 'LF'
Communication State
Status: closed
RecordStatus: off
Read/Write State
TransferStatus: idle
BytesAvailable: 0
ValuesReceived: 0
ValuesSent: 2
And here is my arduino code.
void setup() {
// put your setup code here, to run once:
pinMode(13, OUTPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
if(Serial.available()>0) // check if Serial data is available to read
{
int x=Serial.read();
//int x = Serial.parseInt();
//int y = Serial.parseInt();
//int z = Serial.parseInt();
digitalWrite(13, HIGH);
delay(x);
digitalWrite(13, LOW);
delay(x);
/*digitalWrite(13, HIGH);
delay(y);
digitalWrite(13, LOW);
delay(y);
digitalWrite(13, HIGH);
delay(z);
digitalWrite(13, LOW);
delay(z);*/
}
}

回答 (3 件)

Walter Roberson
Walter Roberson 2016 年 4 月 23 日
You have
fprintf(s,x)
The default format for fprintf to serial port is '%s\n', so you have done the equivalent of
fprintf(s, '%s\n', x)
Conversion of numeric arguments for %s works like uint8(x) (I think; I am not completely sure). Your 10000 would (I think) be converted to 255 and the single byte with value 255 sent. Followed by the terminator.
Your arduino code uses Serial.read() which expects only a single byte.

Haroon  Aziz
Haroon Aziz 2016 年 4 月 24 日
編集済み: Walter Roberson 2016 年 4 月 24 日
now i am using this arduino code but not getting any results.
void setup()
{
pinMode(13, OUTPUT);
Serial.begin(9600);
}
void loop()
{
while (Serial.available()) // check if Serial data is available to read
{
int x = Serial.parseInt();
int y = Serial.parseInt();
int z = Serial.parseInt();
digitalWrite(13,HIGH);
delay(x);
digitalWrite(13,LOW);
delay(x);
digitalWrite(13,HIGH);
delay(y);
digitalWrite(13,LOW);
delay(y);
digitalWrite(13,HIGH);
delay(z);
digitalWrite(13,LOW);
delay(z);
}
}
also i have tried changing in matlab command.
  1 件のコメント
Walter Roberson
Walter Roberson 2016 年 4 月 24 日
Your MATLAB code is sending one value but your arduino code expects three.
Test the MATLAB side with some constant data such as
fprintf(s, '%d %d %d\n', [42, 153, 287]);

サインインしてコメントする。


husam alrajab
husam alrajab 2019 年 6 月 3 日
I am facing the same problem plz let me know if you solved it ??

カテゴリ

Help Center および File ExchangeSparse Matrices についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by