- Install the MATLAB Support Package for Arduino Hardware: Access this package via MATLAB's "Add-On Explorer" and install it.
- Command Transmission: Read commands from your file and use the “sendCommand”function to send them to the Arduino.
Opening text document and sending the info over serial port
13 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I have a text document, called transition.txt, that has this inside it.
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
I need to send it to my arduino. Not sure what the code should do on the Matlab side. Also, how would I make sure the information got to the arduino in the way I wanted it to?
Here is my arduino code.
const size_t maxCommandNumber = 50;
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) {Serial.print(F("in Function ")); Serial.println(s);}
void func_Lp(const char* s) {Serial.print(F("in Function ")); Serial.println(s);}
void func_L2(const char* s) {Serial.print(F("in Function ")); Serial.println(s);}
void func_R(const char* s) {Serial.print(F("in Function ")); Serial.println(s);}
void func_Rp(const char* s) {Serial.print(F("in Function ")); Serial.println(s);}
void func_R2(const char* s) {Serial.print(F("in Function ")); Serial.println(s);}
void func_D(const char* s) {Serial.print(F("in Function ")); Serial.println(s);}
void func_Dp(const char* s) {Serial.print(F("in Function ")); Serial.println(s);}
void func_D2(const char* s) {Serial.print(F("in Function ")); Serial.println(s);}
void func_B(const char* s) {Serial.print(F("in Function ")); Serial.println(s);}
void func_Bp(const char* s) {Serial.print(F("in Function ")); Serial.println(s);}
void func_B2(const char* s) {Serial.print(F("in Function ")); Serial.println(s);}
void func_U(const char* s) {Serial.print(F("in Function ")); Serial.println(s);}
void func_Up(const char* s) {Serial.print(F("in Function ")); Serial.println(s);}
void func_U2(const char* s) {Serial.print(F("in Function ")); Serial.println(s);}
void func_F(const char* s) {Serial.print(F("in Function ")); Serial.println(s);}
void func_Fp(const char* s) {Serial.print(F("in Function ")); Serial.println(s);}
void func_F2(const char* s) {Serial.print(F("in Function ")); Serial.println(s);}
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"));
}
0 Commenti
Risposte (1)
Jaskirat
il 24 Gen 2025 alle 8:35
Modificato: Jaskirat
il 24 Gen 2025 alle 10:25
Hello,
I understand that you are trying to send a series of commands to an Arduino.
To transmit a series of commands to an Arduino, please follow these steps:
Refer the below documentation page to know more information on “sendCommands”:
Hope this helps!
0 Commenti
Vedere anche
Categorie
Scopri di più su Arduino Hardware in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!