0
votes

I am trying to send some data from Matlab to arduino. It is a matrix of [1 by 2]. My plan is to convert this two numbers in to a string and send to the arduino. However in the serial monitor, I can not read any value coming from matlab.

This is my matlab code,

val_a = matt(n,:);
                 A = [val_a];

                asd = A(1:1);
                asb = A(:,2);
                strA = num2str(asd);
                strB = num2str(asb);
                comma = ',';
                endVal = '#';
                theString = strcat(strA,comma,endVal);

                obj1 = instrfind('Type', 'serial', 'Port', 'COM19', 'Tag', '');

                if isempty(obj1)
                    obj1 = serial('COM19');
                else
                    fclose(obj1);
                    obj1 = obj1(1);
                end

                fopen(obj1);
                fprintf(obj1,theString)

                fclose(obj1);
                delete(obj1);
                A = [];

And this is the serial event of arduino

bool gotalfa = false;
bool event = false;
void serialEvent() {
while (Serial.available()) 
{
  char inChar = (char)Serial.read(); 
  event = true;
  if (inChar == , && !gotalfa) 
  {
  alfa = inputString;
  inputString = "";
  gotalfa = true;
  event = false;
  }
  if (inChar == '#' && gotalfa) 
  {
    theta = inputString;
    gotalfa = false;
    inputString = "";

    Serial.print("alfa ");
    Serial.print(alfa);
    Serial.print("theta ");
    Serial.println(theta);

    //some program.... 
    event = false;
  }
  if(event)
  {
    inputString += inChar;
  }}}

Do I have to change anything in my matlab/arduino code. Any helpful tip is greatly appreciated.

Thank you in advance

1
Did you got a solution to this effort? Pelase post an update - Jose Enrique Calderon

1 Answers

0
votes

Just a tip, hopefully it points you to the answer.

I would use a terminal program like putty or teraterm to isolate the root cause of your error.

i.e. Run putty/teraterm to send certain string to your serial port of your arduino and see if it does returns the expected strings.

http://www.putty.org/ https://ttssh2.osdn.jp/index.html.en

Also, consider using the ReadStringUntil in Arduino. https://www.arduino.cc/en/Serial/ReadStringUntil

Hope it helps.