Matlab + Arduino - problem with serial port

Hi. I have problem with my function which I use to send values of angles to my uC (uC send them to servos). This is my function in Matlab:

function comunicate(fi1d,fi2d,fi) 
s = serial('COM7','BaudRate',115200); 
pause(1); 
fopen(s); 
s.Terminator = 'CR'; 
pause(2); 
s.RecordDetail = 'verbose'; 
s.RecordName = 'MySerialFile.txt'; 
record(s,'on'); 
kat1=round(fi1d); 
kat2=round(fi2d); 
for i=1:1:length(fi) 
fprintf(s,'S %d %d',[kat1(i),kat2(i)]); 
pause(2); 
end 
fscanf(s); 
record(s,'off'); 
fclose(s); 
fclose(instrfind); 
clear 'MySerialFile.txt'; 
end

I don't know why it work fine while I'm typing manually

fprintf(s, 'S 20 30')

But it doesn't work automatically, servos doesn't take a position

fprintf(s,'S %d %d',[kat1(i),kat2(i)]);

On a uC I have program in C, which is reading data from serial port. It recognize arguments after 'S' which are separated by space symbol.

kat[i] = message.readInt();

Interesting, that 'log' file MySerialFile.txt looks little different:

for manually

1 Recording on 12-Jan-2012 at 10:13:02.058. Binary data in little endian format.
2 > 8 ascii values.
S 90 12

for automatic

1 Recording on 12-Jan-2012 at 09:50:33.750. Binary data in little endian format.
2 > 7 ascii values.
S 90 12

Have anyone idea how to fix it?

rekram,

As a first attempt, I would add a CR character into the fprintf statement. Sometimes Matlab can be a little wonky with text. That said, I haven't tried using MATLAB for Arduino interfacing, but I use MATLAB everyday at work.

Rekram, (and anyone else!)

Did you ever solve this problem? I'm having the exact same difficulties. I've tried carriage returns, but the code only works while entering by command line in MATLAB.

In MATLAB:

S1 = serial ('COM7', 'Baudrate', 9600, 'Terminator', 'CR');
fopen(S1);
fprintf(S1, '%c %c', 'L \r');
fclose (S1);

In Arduino:

int timer = 100;  //1000 is one second
int inByte = 0;    //incoming serial byte
void setup() 
{                
  // start serial port at 9600 bps:
  Serial.begin(9600);
   // initialize the digital pins as an outputs.
    pinMode(5, OUTPUT);        //CTRL-1A LED
    pinMode(6, OUTPUT);        //CTRL-2A LED
    pinMode(7, OUTPUT);        //CTRL-2B LED
    pinMode(8, OUTPUT);        //CTRL-2C LED
    pinMode(9, OUTPUT);        //CTRL-1B LED
  
void loop() {
  if (Serial.available() > 0) {
  inByte = Serial.read();    // get incoming byte

  switch (inByte) {
 // use a switch case to check for the "L" input 
  //use for switch routine
    case 'L':  
      digitalWrite(5, HIGH);   // set 1A on 
      digitalWrite(6, LOW);   // set 2A off 
      digitalWrite(7, LOW);   // set 2B off
      digitalWrite(8, LOW);   // set 2C off
      delay(timer);
      digitalWrite(6, HIGH);   // set 2A on
      digitalWrite(7, LOW);   // set 2B off
      digitalWrite(8, LOW);   // set 2C off
      delay(timer);
      digitalWrite(6, LOW);   // set 2A off
      digitalWrite(7, HIGH);   // set 2B on
      digitalWrite(8, LOW);   // set 2C off
      delay(timer);
      break;

    default:
      digitalWrite(5, LOW);  
      digitalWrite(8, HIGH);   
      digitalWrite(6, LOW);     
      digitalWrite(7, LOW);     
      digitalWrite(8, LOW);   
      delay(timer);

    }//end switch case statement
  }//end if statement checking for serial input
}//end void loop

Thanks!

fprintf(S1, '%c %c', 'L \r'); is this correct? ==> fprintf(S1, '%c %c\r', ['L', ' ']); ?

// No matlab expert , just comparing your fprintf with the one above...

furthermore your arduino code might process the data too fast to notice .. The two bytes will arrive in less than 3 msec so ...

2 cents,

Rob,

Thanks for your response. That was the um-teenth* iteration of that line! My original line was:

fprintf(S1, '%c', 'L');

I did get it to work, after checking with the MATLAB forum. I needed larger pauses before and after 'fprintf' to get the LEDs to work.

It works like this fine:

pause(3)
fprintf(S1, '%c', 'L');
pause(1)

Cheers,
M

Do you have a link to the matlab forum thread, would be informative to say the least,

TIA,
Rob

Sure, It's here:

In Adam's reply on August 15th.

-Mel