Matlab is reading extra characters from arduino serial output -


i having arduino output consistent set of numbers com port. matlab reading com port , 1 in 10 times reads successfully. other times adds character or 2 on first line. it's letter sort of accent. below code matlab , arduino. adding characters?

arduino code

int output1, output2, output3, area; void setup() {  serial.begin (9600) } void loop()  { output1 = 2.0; output2 = 2.1; output3 = 3.7;  delay(5); serial.print(2.0); serial.print(','); serial.print(2.1); serial.print(','); serial.println(3.7); } 

matlab code

clear  close serialport='com3'; maxdeviation=3; timeinterval=0.2; loop=120;  s = serial(serialport);  distance1(1)=0; time(1)=0; count = 2; k=1; fopen (s); while ~isequal(count,loop)  %%serial data accessing    distance1 = fgetl(s);  distance2= textscan(distance1,'%f %f %f','delimiter', ',');  distance3(count + 1) = cell2mat(distance2');   area=(0.5*2.094*((distance3(count,1))^2 + (distance3(count,1))^2 + (distance3(count,1))^2));  count = count + 1; end  %% clean serial port fclose(s); delete(s); clear s; 

well, have discard first line, since don't know when turning serial on.

i mean, printing

2.0,2.1,3.7\r\n 

but can open port when have transmitted 3, receive

.7\r\n 

so put fgetl after fopen , should read correctly.

just note, it's practice put starting character in stream, can align @ start. now, since sending new line, can synchronize on it


Comments

Popular posts from this blog

java - pagination of xlsx file to XSSFworkbook using apache POI -

Unlimited choices in BASH case statement -

apache - How do I stop my index.php being run twice for every user -