I am facing a problem in serial communication of matlab and arduino.
2 views (last 30 days)
Show older comments
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);*/
}
}
0 Comments
Answers (3)
Walter Roberson
on 23 Apr 2016
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.
0 Comments
Haroon Aziz
on 24 Apr 2016
Edited: Walter Roberson
on 24 Apr 2016
1 Comment
Walter Roberson
on 24 Apr 2016
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
on 3 Jun 2019
I am facing the same problem plz let me know if you solved it ??
0 Comments
See Also
Categories
Find more on Environment and Settings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!