0

I'm new to Arduino and am learning how Serial communication works. I am trying to send a value from Matlab to Arduino, but it seems to be partly fruitful. If anyone can explain the difference between the acquiring input and predefined strings in Matlab Here is the program I tried, and it is successful. Arduino :

 %This program receives an array of chars and stores them in array and transmits them back 

 const int no_of_fields = 3; 
 int field_index = 0;
 int vec_to_hold[no_of_fields]; //Array to hold the numbers

void setup()
{
   Serial.begin(9600);
}
void loop()
{
   if(Serial.available())
    {
     char ch = Serial.read();
     if(ch >= '0'&& ch<= '9') //check if the received chars are digits
      {
         if(field_index<no_of_fields)
           {
              vec_to_hold[field_index] = (vec_to_hold[field_index] * 10) + (ch -'0'); //accumulate values to form and integer
           }
      }
     else if(ch == ',')
     {
       field_index++;
     }
     else if(ch == '\n')
     {
        for (int i = 0;i<min(no_of_fields,field_index+1);i++)
         {
         Serial.print(vec_to_hold[i]);
         vec_to_hold[i] = 0;
         }
        Serial.println();
        field_index = 0;
      }
   }
}

Here is the Matlab side of Code :

delete(instrfindall)
S = serial('COM3', 'BaudRate', 9600,'Terminator','LF'); %38400
fopen(S);
pause(0.1);
vec_sent = input('Enter : ','s');
fprintf(S,'%s\n',vec_sent);
pause(0.1);
t_value = fscanf(S,'%d');
disp(t_value);
delete(instrfindall)

The above code is working, but what if i don't want to enter the string. I want to be predefined. For example in the above matlab code

vec_sent = '200,300,400';

If i convert this line

 vec_sent = input('Enter : ','s');

to

 vec_sent = '200,300,400';

it does not work. The RX pin on the Arduino blinks indicating something is coming on the serial, but TX pin is not at all blinking, and nothing appears in MATLAB except an error stating

 Warning: Unsuccessful read: A timeout occurred before the Terminator was reached..

I would be grateful if anybody points out what I am doing wrong. Thanks in advance.

2 Answers 2

1

To confirm that the cause of the connection problem is not the software, you can try to connect using reliable software. Do do this, download and install the MATLAB support package for Arduino hardware: http://www.mathworks.com/hardware-support/arduino-matlab.html

0

The Arduino parsing code seems to function correctly. If I just use the IDE Serial Monitor window and enter "200,300,400", the Arduino replies "200300400".

The error message says that you either did not get the LF, or the LF did not arrive within 0.1 seconds.

One thing to try, as I do not have MATLAB, is to change how the Arduino replies. On the Arduino, Serial.println will send a CR and a LF at the end. Perhaps you should just do the LF explicitly instead:

for (int i = 0;i<min(no_of_fields,field_index+1);i++)
{
  Serial.print(vec_to_hold[i]);
  vec_to_hold[i] = 0;
}
Serial.print( (char)10 ); // just LF, not CR/LF with println
field_index = 0;

This will test whether the CR is causing a line termination problem in MATLAB.

Another possibility is that the pause(0.1) is too short. The documentation states that this is highly dependent on the OS timer resolution. Try pause(1). This will test whether the time is too short. At 9600, 0.1s is only 100 character times.

4
  • Yes i thought of it and tried it yesterday, but with pause(2). It works perfectly when i put a pause of 2 seconds in MATLAB, but i couldn't come up with the reason. Since you just said that it(pause) might be the reason, would you care to explain why i have to extend the pause time when i am connecting it to Arduino. I tried with pause(1) but not fruitful, the thought is killing me from yesterday why pause(2)?? Commented Nov 11, 2015 at 12:15
  • Because MATLAB runs on so many OS's, it will exhibit slight differences between platforms. In this case, the resolution of the timers used by MATLAB is only 1 second: pausing (0,1] could be no pause, pausing (1,2] is <= 1s, etc. Because there are so many things going on in the background in Windows, and it's not a Real Time OS, the MATLAB developers may have simply said, "Windows Update might be running... Close enough!" :P I have found MATLAB examples that use pause(1), so sub-second resolution isn't something you should count on. Answered? :)
    – slash-dev
    Commented Nov 11, 2015 at 14:48
  • Yes, i agree, it might be one of the reason. Do you think the (DTR - Data Terminal Ready) of the arduino might be the culprit here? I have obeserved some scenarios arduino taking some time to print to serial monitor. Commented Nov 11, 2015 at 22:27
  • DTR is probably not the culprit. On the PC side, you must go out of your way to enable DTR handshaking. On the Arduino side, there's nothing in the HardwareSerial class that does hardware handshaking. I don't know which Arduino you're using, so I don't know what's handling the USB interface. If it's using an FTDI chip, it can do DTR/DSR handshaking. More likely, it's just waiting a little bit to gather serial data to send to the PC in a packet. Below some threshold (64 bytes?), it will wait to see if more data is coming.
    – slash-dev
    Commented Nov 13, 2015 at 18:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.