Serial Input Basics Example 5 Modification

Hi there!

I'm currently working on a robotics project that requires serial communication to control motor direction and speed. These commands will come one at a time. I have been referencing Serial Input Basics by Robin2, specifically example 5. My modified code (shown below) is supposed to receive the data then print out that same data with labels.

const byte maxInts = 32;
char receivedInformation[maxInts];
char tempInformation[maxInts];

int infoDirection;
int infoPower;

bool newData = false;

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

void loop() {
  
  while(newData == false){
    receiveInfo();
  }
  if (newData == true){
    strcpy(tempInformation,receivedInformation);
    parseData();
    showParsedData();
    newData = false;
  }
     
}

void receiveInfo(){
  static bool receivingInProgress = false;
  static byte index = 0;
  char startMarker = '<';
  char endMarker = '>';
  char rc;

  while (Serial.available() > 0 && newData == false){
    rc = Serial.read();

    //Find Start
    if (rc == startMarker){
      receivingInProgress = true;
    }
    //Read Information
    else if (rc != endMarker){
      receivedInformation[index] = rc;
      index++;
      if (index >= maxInts){
        index = maxInts - 1;
      }
    }
    //Find End
    else if (rc == endMarker){
      receivedInformation[index] = '\0';
      receivingInProgress = false;
      index = 0;
      newData = true;
    }
  }
}

void parseData() {
  
  char * strtokIndex;

  strtokIndex = strtok(tempInformation,",");
  infoDirection = atoi(strtokIndex);
  strtokIndex = strtok(NULL,",");
  infoPower = atoi(strtokIndex);
  
}

void showParsedData() {
  
  Serial.print("Direction ");
  Serial.println(infoDirection);
  Serial.print("Power ");
  Serial.println(infoPower);
  
}

This code receives commands in the format <direction,power>. It works for the first command entered into the terminal, but every other command defaults to <0,0>. I've attached a picture of the output from two inputs.

I think this is possibly happening because the code is not checking for new data as it should or the data is being overridden. Is it possible that this is the case, or is there something else I'm missing?

while (Serial.available() > && newData == false)

Oops :o

I think you made a typo

Power_Broker:

while (Serial.available() > && newData == false)

Oops :o

I think you made a typo

Whoops, thanks! It didn't fix the issue, but that was definitely an important fix.

rcphelps29:
Whoops, thanks! It didn't fix the issue, but that was definitely an important fix.

It fixed it for me...

I fixed your original typo myself, it worked. I used your corrected code, it works....

Direction 1
Power 2
Direction 4
Power 5
Direction 100
Power 100

I just retested it, and it works when I enter the commands in the serial monitor, which I'm very happy to see! We are actually trying to send the data from a non-arduino program through the terminal it generates. That leads me to believe that there is an issue with that code. I'm using the SerialPort Library to do this.

#include<iostream>
#include<string>
#include<stdlib.h>
#include"SerialPort.h"

using namespace std;

char output[MAX_DATA_LENGTH];
char incomingData[MAX_DATA_LENGTH];

// change the name of the port with the port name of your computer
// must remember that the backslashes are essential so do not remove them
char *port = "\\\\.\\COM6";

int main(){
    SerialPort arduino(port);
    if(arduino.isConnected()){
        cout<<"Connection made"<<endl<<endl;
    }
    else{
        cout<<"Error in port name"<<endl<<endl;
    }
    while(arduino.isConnected()){
        cout<<"Enter your command: "<<endl;
        string data;
        cin>>data;

        char *charArray = new char[data.size() + 1];
        copy(data.begin(), data.end(), charArray);
        charArray[data.size()] = '\n';

        arduino.writeSerialPort(charArray, MAX_DATA_LENGTH);
        arduino.readSerialPort(output, MAX_DATA_LENGTH);

        cout<<">> "<<output<<endl;

        delete [] charArray;
    }
    return 0;
}

1.) Consider using ArduSerial instead of SerialPort (ArduSerial is easier and more intuitive to use)
2.) To debug what the program is sending, upload a sketch to your board that does nothing but print out each individual char received by the program. That way you know exactly what your program is sending.