Email: helpdesk@telkomuniversity.ac.id

This Portal for internal use only!

  • My Download
  • Checkout
Application Package Repository Telkom University
All Categories

All Categories

  • IBM
  • Visual Paradigm
  • Adobe
  • Google
  • Matlab
  • Microsoft
    • Microsoft Apps
    • Analytics
    • AI + Machine Learning
    • Compute
    • Database
    • Developer Tools
    • Internet Of Things
    • Learning Services
    • Middleware System
    • Networking
    • Operating System
    • Productivity Tools
    • Security
    • VLS
      • Office
      • Windows
  • Opensource
  • Wordpress
    • Plugin WP
    • Themes WP
  • Others

Search

0 Wishlist

Cart

Categories
  • Microsoft
    • Microsoft Apps
    • Office
    • Operating System
    • VLS
    • Developer Tools
    • Productivity Tools
    • Database
    • AI + Machine Learning
    • Middleware System
    • Learning Services
    • Analytics
    • Networking
    • Compute
    • Security
    • Internet Of Things
  • Adobe
  • Matlab
  • Google
  • Visual Paradigm
  • WordPress
    • Plugin WP
    • Themes WP
  • Opensource
  • Others
More Categories Less Categories
  • Get Pack
    • Product Category
    • Simple Product
    • Grouped Product
    • Variable Product
    • External Product
  • My Account
    • Download
    • Cart
    • Checkout
    • Login
  • About Us
    • Contact
    • Forum
    • Frequently Questions
    • Privacy Policy
  • Forum
    • News
      • Category
      • News Tag

iconTicket Service Desk

  • My Download
  • Checkout
Application Package Repository Telkom University
All Categories

All Categories

  • IBM
  • Visual Paradigm
  • Adobe
  • Google
  • Matlab
  • Microsoft
    • Microsoft Apps
    • Analytics
    • AI + Machine Learning
    • Compute
    • Database
    • Developer Tools
    • Internet Of Things
    • Learning Services
    • Middleware System
    • Networking
    • Operating System
    • Productivity Tools
    • Security
    • VLS
      • Office
      • Windows
  • Opensource
  • Wordpress
    • Plugin WP
    • Themes WP
  • Others

Search

0 Wishlist

Cart

Menu
  • Home
    • Download Application Package Repository Telkom University
    • Application Package Repository Telkom University
    • Download Official License Telkom University
    • Download Installer Application Pack
    • Product Category
    • Simple Product
    • Grouped Product
    • Variable Product
    • External Product
  • All Pack
    • Microsoft
      • Operating System
      • Productivity Tools
      • Developer Tools
      • Database
      • AI + Machine Learning
      • Middleware System
      • Networking
      • Compute
      • Security
      • Analytics
      • Internet Of Things
      • Learning Services
    • Microsoft Apps
      • VLS
    • Adobe
    • Matlab
    • WordPress
      • Themes WP
      • Plugin WP
    • Google
    • Opensource
    • Others
  • My account
    • Download
    • Get Pack
    • Cart
    • Checkout
  • News
    • Category
    • News Tag
  • Forum
  • About Us
    • Privacy Policy
    • Frequently Questions
    • Contact
Home/Matlab/Opening text document and sending the info over serial port

Opening text document and sending the info over serial port

PuTI / 2025-01-24
Opening text document and sending the info over serial port
Matlab News

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] = ”; // 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] = ”; // always maintain a correct cString
}
} else {
Serial.println(F("ERROR: Command line too big. Discarded.nt-> 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 == ”) 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"));
}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] = ”; // 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] = ”; // always maintain a correct cString
}
} else {
Serial.println(F("ERROR: Command line too big. Discarded.nt-> 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 == ”) 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"));
} 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] = ”; // 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] = ”; // always maintain a correct cString
}
} else {
Serial.println(F("ERROR: Command line too big. Discarded.nt-> 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 == ”) 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"));
} arduino, serial, send MATLAB Answers — New Questions

​

Tags: matlab

Share this!

Related posts

test one two three four
2025-05-20

test one two three four

Is it possible to make this tiny loop faster?
2025-05-19

Is it possible to make this tiny loop faster?

Solar Wind Battery Hybrid Integration
2025-05-19

Solar Wind Battery Hybrid Integration

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Search

Categories

  • Matlab
  • Microsoft
  • News
  • Other
Application Package Repository Telkom University

Tags

matlab microsoft opensources
Application Package Download License

Application Package Download License

Adobe
Google for Education
IBM
Matlab
Microsoft
Wordpress
Visual Paradigm
Opensource

Sign Up For Newsletters

Be the First to Know. Sign up for newsletter today

Application Package Repository Telkom University

Portal Application Package Repository Telkom University, for internal use only, empower civitas academica in study and research.

Information

  • Telkom University
  • About Us
  • Contact
  • Forum Discussion
  • FAQ
  • Helpdesk Ticket

Contact Us

  • Ask: Any question please read FAQ
  • Mail: helpdesk@telkomuniversity.ac.id
  • Call: +62 823-1994-9941
  • WA: +62 823-1994-9943
  • Site: Gedung Panambulai. Jl. Telekomunikasi

Copyright © Telkom University. All Rights Reserved. ch

  • FAQ
  • Privacy Policy
  • Term

This Application Package for internal Telkom University only (students and employee). Chiers... Dismiss