Trouble sending serial data from MATLAB to arduino

Hello, if any of you are well versed in MATLAB and arduino, I am trying to send something from a document.
Here is my MATLAB code

clear s;

s = serialport("COM4", 115200);
text = extractFileText("transition.txt");
write(s,text,"string");

the variable text looks like this

"L,R,D',B,U,D,F',R,B',L',B2,R,F2,D2,F,L',U2,L,U2,L,B2,L',B2,L2,U2,L2,R2,U2,B2,U2,L,L"

If I take this code and put it into the serial monitor of my arduino, everything works as it should, so I am sure my arduino software is correct.

Problem is your Matlab code is different than you using Serial Monitor to send characters.

so my matlab code sends a string, my code must not read a string then, here is arduino code that someone helped me write.

const size_t maxCommandNumber = 55;
const size_t maxCommandSize = 3 * maxCommandNumber; // if commands fit on 2 characters and a comma, this is enough for maxCommandNumber commands
char receivedChars[maxCommandSize + 1]; // +1 for trailing null char required by cStrings
const char endMarker = '\n';

void func_L(const char* s)  {Turn90(dirPinL,stepPinL,TurnSpeed);}
void func_Lp(const char* s)  {Turn90CCW(dirPinL,stepPinL,TurnSpeed);}
void func_L2(const char* s)  {Turn180(dirPinL,stepPinL,TurnSpeed);}
void func_R(const char* s)  {Turn90(dirPinR,stepPinR,TurnSpeed);}
void func_Rp(const char* s)  {Turn90CCW(dirPinR,stepPinR,TurnSpeed);}
void func_R2(const char* s)  {Turn180(dirPinR,stepPinR,TurnSpeed);}
void func_D(const char* s) {Turn90(dirPinD,stepPinD,TurnSpeed);}
void func_Dp(const char* s) {Turn90CCW(dirPinD,stepPinD,TurnSpeed);}
void func_D2(const char* s) {Turn180(dirPinD,stepPinD,TurnSpeed);}
void func_B(const char* s) {Turn90(dirPinB,stepPinB,TurnSpeed);}
void func_Bp(const char* s) {Turn90CCW(dirPinB,stepPinB,TurnSpeed);}
void func_B2(const char* s) {Turn180(dirPinB,stepPinB,TurnSpeed);}
void func_U(const char* s) {Turn90(dirPinU,stepPinU,TurnSpeed);}
void func_Up(const char* s) {Turn90CCW(dirPinU,stepPinU,TurnSpeed);}
void func_U2(const char* s) {Turn180(dirPinU,stepPinU,TurnSpeed);}
void func_F(const char* s) {Turn90(dirPinF,stepPinF,TurnSpeed);}
void func_Fp(const char* s) {Turn90CCW(dirPinF,stepPinF,TurnSpeed);}
void func_F2(const char* s) {Turn180(dirPinF,stepPinF,TurnSpeed);}
void func_unknown(const char* s) {Serial.print(F("Unknown commmand: ")); Serial.println(s);}

struct {
  const char* label;
  void (*func)(const char*);
} keys[] = {
  {"L", func_L},
  {"L'", func_Lp},
  {"L2", func_L2},
  {"R", func_R},
  {"R'", func_Rp},
  {"R2", func_R2},
  {"D", func_D},
  {"D'", func_Dp},
  {"D2", func_D2},
  {"B", func_B},
  {"B'", func_Bp},
  {"B2", func_B2},
  {"U", func_U},
  {"U'", func_Up},
  {"U2", func_U2},
  {"F", func_F},
  {"F'", func_Fp},
  {"F2", func_F2},
};
const size_t nbKeys = sizeof keys / sizeof keys[0];

bool findAndExecute(const char* s)
{
  bool success = false;
  for (size_t i = 0; i < nbKeys; i++)
    if (!strcmp(s, keys[i].label)) {
      keys[i].func(s);
      success = true;
      break;
    }
  if (!success) func_unknown(s);
  return success;
}

bool commandAvailable()
{
  static size_t commandIndex = 0;
  static bool awaitEndMarkerError = false;

  int incomingByte = Serial.read();
  bool commandComplete = false;
  if (incomingByte != -1) { // read returns -1 if there is nothing to read (faster than using available)
    if (awaitEndMarkerError) {
      if (incomingByte == endMarker) {
        awaitEndMarkerError = false;
        commandIndex = 0;
        receivedChars[0] = '\0';  // discard what was there
        commandComplete = true; // if we want to still notify the parser we got a command (here will be empty)
      }
    } else {
      char incomingCharacter = incomingByte; // grab LSB as a char
      if (incomingByte != endMarker) {
        if (commandIndex < maxCommandSize) {
          if (!isspace(incomingCharacter)) { // discard all spaces. could also add a test to only keep A-Z and ' and 2, whatever is legit in the command language
            receivedChars[commandIndex++] = incomingCharacter;
            receivedChars[commandIndex] = '\0'; // always maintain a correct cString
          }
        } else {
          Serial.println(F("ERROR: Command line too big. Discarded.\n\t-> increase maxCommandNumber or send shorter command lines."));
          awaitEndMarkerError = true;
        }
      } else {
        commandIndex = 0;
        commandComplete = true;
      }
    }
  }
  return commandComplete;
}

bool handleCommand()
{
  bool success = true;
  const char* separators = ",";
  Serial.print(F("\nparsing : ")); Serial.println(receivedChars);
  char* ptr = strtok(receivedChars, separators);
  if (*ptr == '\0') Serial.println(F("Error: empty command"));
  else
    while (ptr) {
      success &= findAndExecute(ptr);
      ptr = strtok(NULL, separators);
    }
  return success;
}

void setup() {
  Serial.begin(115200);
}

void loop() {
  if (commandAvailable())
    if (! handleCommand())
      Serial.println(F("Some commands were not recognized"));

Perhaps try the MATLAB writeline function with the endmarker ('\n') you are looking for.

https://www.mathworks.com/help/matlab/ref/serialport.writeline.html

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