I am facing a problem in serial communication of matlab and arduino.
조회 수: 2 (최근 30일)
이전 댓글 표시
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
답변 (3개)
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.
댓글 수: 0
Haroon Aziz
2016년 4월 24일
편집: Walter Roberson
2016년 4월 24일
댓글 수: 1
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]);
참고 항목
카테고리
Help Center 및 File Exchange에서 Environment and Settings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!