Matlab-arduino serial connection

Hi,
I have some issues with matlab fwrite to send char to arduino. basically, If I run the codes lines, arduino couldn't correctly receive the char from serial port and couldn't echo to it. Accordingly what matlab reads from serial port that arduino send back, looks like the char is not correctly sent to serial port. for example, the character sent is 't', arduino feedback is 'ð'. However, If manually run the codes line by line, the communication works. could you help debug what cause the issues? attached is the test code I used. Thank you in advance!

Arduino side:

bool state=0;
unsigned long startTime;
void setup() {
    // initialize serial communication at 9600 bits per second:
Serial.begin(115200);
pinMode(A0, INPUT);
digitalWrite(A0, LOW) ;  
}


void loop() {
if (Serial.available()) {  
char var=Serial.read();
Serial.print("charater received: ");
Serial.println(var);
if (var=='t') {
Serial.println("Arduino is connected");
}
else if (var=='s') {
state=1;
}
else if (var=='e') {
state=0;
}
}
if (state==1) {
float sensorValue = analogRead(A0);
Serial.println(sensorValue);
}
}

Matlab side:
establish serial comm:

serialInfo = instrhwinfo('serial');
            instrs=instrfind;
            if ~isempty(instrs)
            fclose(instrs);
            delete(instrs);
            end
            ports=serialInfo.AvailableSerialPorts;
            a=serial(ports{1},'Baudrate',115200,'InputBufferSize',50000,'OutputBufferSize',500);
    

Send command character to serial and read the feedback from arduino: if run the lines at once, even I put a pause(3) after fwrite(a,'t'), it often doesn't work. The arduino feedback suggest a wrong character was sent to it. however, if I run the code line by line once at a time, the serial communication will work.

 if strcmp(a.Status,'closed')
                    try fopen(a)
                       disp('Connecting to Arduino')
                        if strcmp(a.Status,'open')
                         fwrite(a,'t')
                        pause(1)
                          while a.bytesavailable==0
                         end 
                        while a.bytesavailable>0
                             strings=fgets(a);
                             disp([strings(1:end-2) ' via ' ports{1}]);
                        end
                           
                        end
                    catch
                        disp(['fail to connect with Arduino via port: ' ports{1}])                        
                    end

Are the 0v commons tied together and flow control/parity bits all set correctly?

I don't know about the flow control/parity bits and the 0v commons. could you explain a bit more? thank you

For the communication, are you using a USB connection to the board? You don't have a separate serial connection do you? If so, that should make sure both devices have the same 0v connected together.

for this test, I only have a USB connection between the PC and the board.

If you run one of the example projects that reports serial data, does that work correctly in the Arduino serial monitor?

do you mean the example projects from arduino? or matlab. When test this in arudino serial monitor, everything work fine. the issue appears to lie between matlab and arduino. in matlab, if run the code one line at a time, e.g. run fopen(a); then select 'fwrite(a,'t');' and execute it; then select the rest codes, it can receive the right echo info from arduino. but it often fail if run all at once. The arduino echo a different character received

So the hardware is okay then it seems. Dig into your Matlab settings. It may be changing or using non hardware default values so it's not being read/written properly.

https://www.google.com/search?q=Arduino+default+serial+settings&rlz=1C1UEAD_enUS990US990&oq=Arduino+default+serial+settings&aqs=chrome..69i57j69i64.4384j0j7&sourceid=chrome&ie=UTF-8&bshm=bshwcqp/1

the trouble is solved by providing a delay between fopen and fwrite. thank you for discussion

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.