Title: Matlab- Arduino- serial com Post by: pouchito on July 04, 2014, 10:07:21 22:07 I want to send 3 numbers separated by commas from the GUI serially through the COM port to the arduino.
the arduino will later on use these values for example as the delay() time input for each of the 3 LEDs. i found this code: MATLAB: Quote function stepInput_Callback(hObject, eventdata, handles) xyz = get(hObject,'String'); disp('Value entered: ') disp(xyz) global COM; global s; s = serial(COM,'BaudRate',9600); fopen(s); disp('After OPEN') disp(s) fprintf(s,xyz); fclose(s); disp('After CLOSE') disp(s) Arduino Code: Quote int x; but it is not working at allint y; int z; void setup() { pinMode(2,OUTPUT); pinMode(6,OUTPUT); pinMode(10,OUTPUT); Serial.begin(9600); } void loop() { if (Serial.available()) // check if Serial data is available to read { x = Serial.parseInt(); y = Serial.parseInt(); z = Serial.parseInt(); if (Serial.read() == '\n') // check if ENTER has been pressed (newline) { digitalWrite(2,1); delay(x); digitalWrite(6,1); delay(y); digitalWrite(10,1); delay(z); digitalWrite(10,0); delay(z); digitalWrite(6,0); delay(y); digitalWrite(2,0); delay(x); } } } can u help me figure out the problem Title: Re: Matlab- Arduino- serial com Post by: hate on July 05, 2014, 12:00:57 00:00 Code: xyz = get(hObject,'String'); Code: [x y z] = get(hObject,'String'); Code: x = Serial.parseInt(); And next time please use code tags and COMMENT your code so that others may get an idea about what it's doing without the need to consult MATLAB documentation! Title: Re: Matlab- Arduino- serial com Post by: pouchito on July 05, 2014, 06:23:08 18:23 Thank you hate and sorry for the bad formatting, i edited my original post.
i modified it as u suggested:[x y z] = get(hObject,'String'); instead of xyz = get(hObject,'String'); but nothing changed. i m not able to read any values. do u think the problem is in the arduino part. what i want to do is to send 3 different long bytes variable to the arduino. can u help me please ? Title: Re: Matlab- Arduino- serial com Post by: TucoRamirez on July 05, 2014, 06:36:06 18:36 have you tested your serial data on any terminal soft in order to verify that are you sending what u expected?
Title: Re: Matlab- Arduino- serial com Post by: pouchito on July 05, 2014, 06:55:42 18:55 no i did not, i m sure that information are being transmitted since the arduino is flashing
i will try to test it on a serial terminal, hope i ll know how :S Title: Re: Matlab- Arduino- serial com Post by: hate on July 05, 2014, 08:00:03 20:00 i modified it as u suggested:[x y z] = get(hObject,'String'); instead of xyz = get(hObject,'String'); My suggestion was based on a general issue in your code. For this change to help with anything, get() function must be returning 3 values which I have no idea if it does. I even have no idea what get() function performs! Before I can be of more help can you please explain the MATLAB code further? For example what is the purpose of the definition of the function in the first line and where it is used in the code?Code: function stepInput_Callback(hObject, eventdata, handles) Title: Re: Matlab- Arduino- serial com Post by: pickit2 on July 05, 2014, 10:16:00 22:16 no i did not, i m sure that information are being transmitted since the arduino is flashing Did you use the built in serial coms in Arduino Ctrl Shift & M it will show more than just flashing a led.i will try to test it on a serial terminal, hope i ll know how :S Title: Re: Matlab- Arduino- serial com Post by: hate on July 06, 2014, 09:27:35 09:27 Did you use the built in serial coms in Arduino Ctrl Shift & M it will show more than just flashing a led. Unfortunately that can only work if he has 2 serial ports, one for MATLAB communication and one for IDE communication.Title: Re: Matlab- Arduino- serial com Post by: TucoRamirez on July 06, 2014, 11:25:06 11:25 except if you create a bridge on Virtual ports ...
Title: Re: Matlab- Arduino- serial com Post by: pouchito on July 06, 2014, 12:01:38 12:01 I tried the arduino part and the serial monitor
It s working properly But when i m sending from matlab it s not working So i guess my problem is with matlab, right ? Hate, concerning the function i used: function stepInput_Callback(hObject, eventdata, handles) it s simply what i got when i insert a text box in the gui interface of matlab, it s nthg imp. Title: Re: Matlab- Arduino- serial com Post by: pablo2048 on July 06, 2014, 12:59:18 12:59 I think that there can be problem in Your Arduino sketch - main loop can be executed too fast to catch all characters of an integer for .parseint() method - try check if there is valid '\n' character (if Serial.read() == '\n' is true...)
Title: Re: Matlab- Arduino- serial com Post by: pouchito on July 06, 2014, 01:17:34 13:17 I inserted a
Quote "}" after the:Quote z = Serial.parseInt(); instead of the end and i changed the : Quote if (Serial.read() =='\n') toQuote if (Serial.read() <='\n') now the leds are turning ON whenever i press the "enter" key regardless of the values where am i mistaken ? Title: Re: Matlab- Arduino- serial com Post by: pablo2048 on July 06, 2014, 01:46:49 13:46 Ok, simple solution - get characters from serial into an array (use stream class) and after receiving "\n" just use .parseInt ...
Title: Re: Matlab- Arduino- serial com Post by: pouchito on July 06, 2014, 02:30:27 14:30 not working :S
can you modify it maybe i m doing sthg wrong :S Title: Re: Matlab- Arduino- serial com Post by: pablo2048 on July 06, 2014, 02:45:13 14:45 Ok - one maybe weird idea - are You sure that Your Matlab code sends "\n" correctly (i'm not familiar with matlab)? Can You please check this? It seems that Arduino serial.parseint method wait internally for the next character...
Title: Re: Matlab- Arduino- serial com Post by: pouchito on July 06, 2014, 06:04:28 18:04 Do u think the problem is in the matlab part ?
Title: Re: Matlab- Arduino- serial com Post by: pablo2048 on July 06, 2014, 07:31:23 19:31 I'm not sure, but now i suspect line-ending output from matlab rather than Arduino part...
Title: Re: Matlab- Arduino- serial com Post by: hate on July 06, 2014, 09:22:21 21:22 except if you create a bridge on Virtual ports ... Can you please elaborate this? I always thought debugging an Arduino with the serial port already connected to some application was a real pain. I believe the method you mentioned is meant for these situations. ;)@pouchito: Can you please answer the following questions with appropriate code lines. 1- In which variables are your 3 delay values stored? -> insert code line(s) here! Can you see those values in the corresponding variables on MATLAB debugger which is usually at the top right of the main window named 'workspace'? 2- Which com port does the code initialize and where in the code? -> insert code line(s) here! 3- How are these values are sent to serial port and where in the code? -> insert code line(s) here! 4- Can you receive anything at the Arduino side, did you test it with the built-in serial monitor? 5- If the answer is yes to the previous question, did you try sending something like: 23 85 15 and did you get the expected behavior on the Arduino part? Title: Re: Matlab- Arduino- serial com Post by: pouchito on July 06, 2014, 10:41:03 22:41 Thank you hate for your time.
i found the code on the net and it is not me who wrote it. i will try to answer ur questions: Quote 1- In which variables are your 3 delay values stored? in xyz : -> insert code line(s) here! Can you see those values in the corresponding variables on MATLAB debugger which is usually at the top right of the main window named 'workspace'? Quote xyz = get(hObject,'String'); no, i cannot see it in the corresponding variables debugger but in the command window i m receiving like a summary and i found this:Quote Read/Write State TransferStatus: idle BytesAvailable: 227 ValuesReceived: 0 ValuesSent: 3 Quote 2- Which com port does the code initialize and where in the code? i m working on com5 Quote s = serial('COM5','BaudRate',9600); Quote 3- How are these values are sent to serial port and where in the code? fprintf(s,'%f',char(xyz)); i think here i have a problem Quote 4- Can you receive anything at the Arduino side, did you test it with the built-in serial monitor? i tried the arduino code with the serial monitor (no matlab) and it is working properly. i sent (1000,1000,1000) and i receive (1000,1000,1000) but when i use the matlab to send the info serially, i can see that the COM port opens since the arduino flashes but i receive no output. Posted on: July 06, 2014, 11:02:21 23:02 - Automerged i ve been working on this project nights and now one line (pause) simply make it work yeyyyyyy :) thank you all for ur help |