SOLVED: Problem with consecutive serial commands from MATLAB

Hi all, I am making a DC motor control system for my project. It implements a command protocol that I have established. If i use the Arduino IDE serial terminal, i can write as many commands as i like and the motor will react, even if they are connected to each other. for example: 1F0.6e 1C11,96,0.5e

or: 1Me1D2,3,5e for connected commands (with 'e' being a sort of terminator).

The thing is, if i try to do the same via MATLAB, the system only accepts the first command and does not read it again. MATLAB code example:

s = serial('COM28','BaudRate',57600);
fopen(s);
pause(2);

fprintf(s,'1xF0.8e1xA255e'); % This line makes the arduino follow both of the commands in a row.

fprintf(s,'1xF0.8e\n1xA255e');%Both this line or the following makes the arduino follow only the first command (up until 'e').

fprintf(s,'1xF0.8e');
fprintf(s,'1xA255e');

I suspect this has something to do with different terminators (LF and CR) sent to the arduino by the IDE terminal and the fprintf command. But then again, it may not. I've tried looking through the web for it and couldn't find the exact issue. I would be delighted to know which terminators are send by each (I know that it can be changed in MATLAB via serial object), in addition to solving the problem.

Thanks in advance, Tim

How about giving us a peek at the Arduino code?

...R

Because that won't be a little peek. And it doesnt really matter because it does work, only not from MATLAB. The whole code is over a 1000 rows so i will not put it all here. Plus i dont come from a programming background but mechanical engineering, so i use global variables, make non-efficient coding, and leave little to none comments. Some variables are created intentionally for debugging purposes even though they arent needed. i dont really mind as long as it works. The big question is whats the difference between sending consecutive serial commands through the terminal in contrast to fprintf. Nevertheless, if it helps, then:

byte DCmotor::commProtocol(){
    float value1=0, value2=0, value3=0, value4=0, value5=0;
    int protocol, errorNum, i, address;
    bool flag=0;
    source=0;
    while (flag==0) {//wait for input    
        if (Serial.available()>0) {
            delay(1);
            address=Serial.peek();          
            if (address==motorIndex+48) Serial.read();
            else return(command);
            protocol=Serial.read();
            if (protocol=='x'){
                source=1;
                protocol=Serial.read();
            }
            if (protocol=='s' || protocol=='S'){
                motorStop();                          
                return(9);          
            }
            else if (protocol=='N') return(3);
            else if (protocol=='P'){
                value1=Serial.parseFloat();
                if (Serial.read()!='e') return(12);
                errorNum=trapezodial(pos,value1,velFiltered,maxSpeed,0,maxAcc,maxDec);
                if (errorNum) return(errorNum+100);
                lastPos = pos;
                backwards = 0;
                forwards = 0;
                endCondition=0;
                m=0;
                startTime=micros();             
                return(0);                       
            }
            else if (protocol=='V'){
                protocol=Serial.read();             
                if (protocol=='T'){
                    value1=Serial.parseFloat();                 
                    if (Serial.read()!=',') return(11);                 
                    value2=Serial.parseFloat();                 
                    if (Serial.read()!=',') return(11);
                    value3=Serial.parseFloat();
                    if (Serial.read()!=',') return(11);
                    value4=Serial.parseFloat();
                    if (Serial.read()!=',') return(11);
                    value5=Serial.parseFloat();
                    if (Serial.read()!='e') return(12);                 
                    errorNum=trapezodial(pos,value1,velFiltered,value2,value3,value4,value5);
                    if (errorNum){
                        Serial.print("Error ");
                        Serial.println(errorNum);
                        errorNum=ramp(pos,value1,velFiltered,value3,max(value4,value5));
                        if (errorNum) return (errorNum);
                    }
                }               
                else if (protocol=='R'){
                    value1=Serial.parseFloat();                 
                    if (Serial.read()!=',') return(11);
                    value2=Serial.parseFloat();
                    if (Serial.read()!=',') return(11);
                    value3=Serial.parseFloat();
                    if (Serial.read()!='e') return(12);
                    ramp(pos,value1,velFiltered,value2,value3);
                    if (errorNum) return (errorNum);
                }               
                else return(14);
                lastPos = pos;
                backwards = 0;
                forwards = 0;
                endCondition=0;
                velToggle=1;
                m=0;
                startTime=micros();             
                return(0);          
            }
            else if (protocol=='H'){                            
                value1=Serial.parseFloat();
                if (Serial.read()!='e') return(12);
                home(value1);
                return(1);              
            }
            else if (protocol=='F'){                
                value1=Serial.parseFloat();
                if (Serial.read()!='e') return(12);             
                endCondition=0;
                findLimits(value1);
                return(1);
            }
            else if (protocol=='A'){                
                value1=Serial.parseFloat();
                if (Serial.read()!='e') return(12);             
                endCondition=0;
                tuneRelay(value1);
                return(1);
            }
            else if (protocol=='C'){
                value1=Serial.parseFloat();             
                if (Serial.read()!=',') return(11);
                value2=Serial.parseFloat();             
                if (Serial.read()!=',') return(11);
                value3=Serial.parseFloat();             
                if (Serial.read()!='e') return(12);             
                setControl(value1,value2,value3);
                return(2);
            }
            else if (protocol=='D'){
                value1=Serial.parseFloat();                 
                if (Serial.read()!=',') return(11);
                value2=Serial.parseFloat();
                if (Serial.read()!=',') return(11);
                value3=Serial.parseFloat();
                if (Serial.read()!='e') return(12);
                whatToPrint=(int)value1*value2*value3;
                EEPROM.updateInt(motorIndex*(sizeof(float)*4+sizeof(int)*11)+sizeof(float)*4+sizeof(int)*9, whatToPrint);
                if (!source) Serial.println("Data feedback values set");
                return(2);              
            }
            else if (protocol=='L'){
                value1=Serial.parseFloat();                 
                if (Serial.read()!=',') return(11);
                value2=Serial.parseFloat();
                if (Serial.read()!=',') return(11);
                value3=Serial.parseFloat();
                if (Serial.read()!=',') return(11);
                value4=Serial.parseFloat();
                if (Serial.read()!='e') return(12);                         
                setLimits(value1,value2,value3,value4);
                return(2);
            }           
            else if (protocol=='R'){
                value1=Serial.parseFloat();                 
                if (Serial.read()!=',') return(11);
                value2=Serial.parseFloat();
                if (Serial.read()!='e') return(12);                         
                setRates(value1,value2);
                return(2);
            }
            else if (protocol=='G'){
                value1=Serial.parseFloat();                 
                if (Serial.read()!=',') return(11);
                value2=Serial.parseFloat();
                if (Serial.read()!=',') return(11);
                value3=Serial.parseFloat();
                if (Serial.read()!='e') return(12);
                setGeneral(value1, value2, value3);
                return(2);              
            }
            else if (protocol=='E'){
                value1=Serial.parseFloat();             
                if (Serial.read()!=',') return(11);
                value2=Serial.parseFloat();             
                if (Serial.read()!=',') return(11);
                value3=Serial.parseFloat();
                if (Serial.read()!=',') return(11);
                value4=Serial.parseFloat(); 
                if (Serial.read()!='e') return(12);             
                setPins(value1,value2,value3,value4);
                return(2);
            }
            else if (protocol=='M'){
                if (Serial.read()!='e') return(12);
                memoryRead(motorIndex);
                return(2);
            }
            else{               
                return(13);
            }
        }   
    } 
}

Basicly, the arduino summons the function when Serial.abailable>0, peeks to check whether the command is issued for this motor (in case of multiple motors), and then issues a function depending on the rest of the input. "return" is made for defining errors or continuing commands in caller loop. i would greatly appreciate any pearls of knowledge regarding the subject. Tim.

If it were me, I'd have MATLAB send a \n and \r as part of the data. YMMV.

idantim:
Because that won’t be a little peek. And it doesnt really matter because it does work, only not from MATLAB.

The fact that you are asking questions here seems to contradict the “it does work”.

If you have a large project just write a short sketch that illustrates and isolates the Matlab problem and post that here.

…R

Thanks all, Problem solved. I would delete the thread but have no access. nice week, Tim.