Arduino/MATLAB Serial Comms Skipping

I'm conducting an echo from MATLAB to my Arduino Pro Mini, but was not receiving a response and getting a Timeout error from MATLAB. I was curious about what would happen if I continued firing instructions and started getting responses.

My MATLAB code:

%Barebones serial testing
clear all

if ~isempty(instrfind)
     fclose(instrfind);
     delete(instrfind);
end
tic
s = serial('com5', 'Baudrate', 9600, 'Timeout', 2);
fopen(s);
scommand = 0;
summation = [];
for i = 1:20
    packet = 0;
    scommand = scommand + 1;
    fwrite(s,scommand);
    % packet = fscanf(s);
    packet = uint8(fread(s,1,'uint8'));
    pause(1)
    if packet ~= 0
        summation = [summation; packet toc];
    end

end
fclose(s);

My Arduino code:

unsigned char temp;
void setup() {
  digitalWrite(13, HIGH);
  Serial.begin(9600);
  while(!Serial){
    delay(1); //if serial is not available, continue to query
  }
}

void loop() {
  if (Serial.available()>0){
    digitalWrite(13, LOW);
    temp = Serial.read();
    Serial.write(temp);
  }
}

This the MATLAB variable produces a table (variable "summation") that looks like:

2 4 3 6 ... 19 22 20 23

There is also a MATLAB warning in saying: Warning: Unsuccessful read: The specified amount of data was not returned within the Timeout period..

From the table and the error above, this indicates that MATLAB/Arduino/Serial Comms times out on the first fwrite command.

In addition, the LED I have turned on at the beginning of the program remains on. I had instructed it to turn off when it receives a serial command. I'm thinking this indicates the Arduino resetting for some inexplicable reason.

Things I've tried: 1. Trying a variety of "Timeout" settings in serial from 0.5 up to 5. 2. Trying a variety of "Baudrate" settings in serial from 4800 to 19200. 3. Verified correct COM port for MATLAB and the Arduino. 4. Verified both MATLAB and the Arduino have the same Baudrate. 5. Tried with a different Arduino Pro Mini and an Arduino Uno with similar results. 6. Tried "Serial.print" instead of "Serial.write". 7. Tried "fprintf" instead of "fwrite". 8. Tried "packet = fscanf(s);" instead of "packet = uint8(fread(s,1,'uint8')".

What could be going on causing the system to skip the first fwrite command?

Instead of jumping both feet into the lake and drowning, I suggest that you use a terminal program such as TeraTerm and make sure that what you type in it is received by the Arduino and that what the Arduino sends shows up in TeraTerm. Clearly you don't know what you are doing.

.

void loop() {
  if (Serial.available()>0){
    digitalWrite(13, LOW);
    temp = Serial.read();
    Serial.write(temp);
  }
}

There really is no excuse for making temp global, and making us scroll around to find the declaration. I can't imagine why you want to use unsigned char as the type.

It would behoove you to understand the difference between write() and print().

eind45:
From the table and the error above, this indicates that MATLAB/Arduino/Serial Comms times out on the first fwrite command.

I don’t know anything about Matlab but that makes me wonder if it is allowing time for the Arduino to reset after Matlab opens the serial port.

Your Matlab code should open the serial port, allow time for the Arduino to reset before sending anything, and then keep the serial port open until it is completely finished with the Arduino.

Have a look at how it is done in this simple Python - Arduino demo

…R

ieee488:
Instead of jumping both feet into the lake and drowning,

Wow.

ieee488:
I suggest that you use a terminal program such as TeraTerm and make sure that what you type in it is received by the Arduino and that what the Arduino sends shows up in TeraTerm.

I have used a serial com emulator that already does what you say. Works fine and I receive the correct echo response each time. The problem is the interface with MATLAB as mentioned in the OP.

ieee488:
Clearly you don’t know what you are doing.

Thank you for the lack of helpful input.

PaulS:
There really is no excuse for making temp global, and making us scroll around to find the declaration. I can’t imagine why you want to use unsigned char as the type.

It would behoove you to understand the difference between write() and print().

Clearly, I’m testing to get the system to work. It’s neither a formal nor final version of my program. As mentioned above, this sketch works fine with a serial com emulator. Using Serial.write() vs. Serial.print() is an attempt to see if the interface with MATLAB interprets things differently than suggested within Arduino docs.

Robin2:
I don’t know anything about Matlab but that makes me wonder if it is allowing time for the Arduino to reset after Matlab opens the serial port.

Your Matlab code should open the serial port, allow time for the Arduino to reset before sending anything, and then keep the serial port open until it is completely finished with the Arduino.

Have a look at how it is done in this simple Python - Arduino demo

…R

Thank you for the respectful and helpful input! MATLAB does have a configurable timeout setting for reading serial which I changed to various values to no avail. I just find it strange that it always misses the first sent instruction, but all subsequent instructions are read without issues. This does not happen with a barebones serial com communicator

I noticed in the link you provided that there are start and end markers (’<’ and ‘>’) in the Arduino code. Is it necessary to have some indicator when sending serial?

eind45:
I noticed in the link you provided that there are start and end markers (’<’ and ‘>’) in the Arduino code. Is it necessary to have some indicator when sending serial?

No. Not if you know what you are doing.

Used serial port numerous times writing LabVIEW and VB.NET programs.

.

eind45: MATLAB does have a configurable timeout setting for reading serial which I changed to various values to no avail. I just find it strange that it always misses the first sent instruction, but all subsequent instructions are read without issues. This does not happen with a barebones serial com communicator

I think you are confusing the concept of allowing an interval between subsequent messages and what I mentioned, which is allowing time for the Arduino to reset after the serial port is opened. Did you notice in my Python example how the PC waits for a message FROM the Arduino before it starts sending anything?

Start- and end-markers make things more reliable. An end-marker is pretty much essential unless you want to write complex code. The second example in Serial Input Basics uses the line-feed character as the end-marker.

...R

PS ... If you focus more on the content of replies rather than their style you will probably find most of them are actually useful.