All right. So I changed the println command in include HEX and the spaces disappear. I also set index back to zero right after I clear the array. The Hex shows all the crap in there. Thats a good trick! Now, I think I can use the strstr() function (or something) to pluck out useful string. Or, is there an easier way to do that?
LIMIT SWITCH PORTION OF SERIAL MONITOR OUTPUT:
30000XG --> IF LOOP
Character Read:
FFFFFF86 ---> Character 0
A ---> Character 1
FFFFFF86 ---> Character 2
A ---> Character 3
FFFFFF86 ---> Character 4
A ---> Character 5
4C ---> Character 6
2C ---> Character 7
31 ---> Character 8
36 ---> Character 9
33 ---> Character 10
39 ---> Character 11
32 ---> Character 12
D ---> Character 13
A ---> Character 14
2A ---> Character 15
?
?
?
L,16392
- --> Read String
Strings not Equal
/*
CNC Bed Sketch
This program drives a Peter Norburg SD4DEU Stepper Driver board running SD4DNCRouter firmware connecting at 9600 Baud.
*/
//******************Constants***********
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); // RX, TX
char homePosition[5] = "0XYG";
//******************Variables***********
char readString[70];
char c;
int index = 0;
int commandNumber = 0;
int numCommands = 0;
//****************Function Prototypes******
long convertToSteps(int inches);
//***************Setup******************************
void setup() {
delay(1000);
//readString[0] = '\0'; //Add a null termination to readString
Serial.begin(9600); //Begin serial communication at 9600
mySerial.begin(9600); //Set software serial baud rate
randomSeed(analogRead(0));
// Initialize Stepperboard Values
mySerial.write("!"); //reset Stepper Board
delay(50);
mySerial.write("1500R"); //Set Run Rate
delay(50);
mySerial.write("800P"); //Set Ramp Rate
delay(1000);
}
//*************Main Program Loop************************
void loop() {
char* stepCommand[] = { //Load up CNC Patterns into string array
"30000XG",
"-30000XG"
};
int numCommands = (sizeof(stepCommand)/sizeof(char*)); //Calculate array size
while(mySerial.available()){
c = mySerial.read(); //get one byte from buffer
readString[index++] = c; //increment array (original array index is returned)
readString[index] = '\0'; //Add NULL to next index
//i=i+1;
//Serial.print(c);
//Serial.println(" --> c charachter byte");
if(readString[index-1]== '*'){
mySerial.write(stepCommand[commandNumber]);
Serial.print(stepCommand[commandNumber]);
Serial.println(" --> IF LOOP");
commandNumber++;
if(commandNumber == numCommands){ //if the max number of commands is reached...
commandNumber = 0; //reset command number back to zero
}
break;
}
}
mySerial.write("L");
if(strlen(readString) > 0){
Serial.println("Character Read: ");
for(int j=0; j < strlen(readString); j++){
Serial.print(readString[j], HEX);
Serial.print(" ---> Character ");
Serial.println(j);
}
Serial.println("======================");
Serial.print(readString);
Serial.println(" --> Read String");
char response[] = {'L', ',','1', '6', '3', '9', '2','\0'};
if(strcmp(readString, response) == 0){
Serial.println("Write Q Here");
Serial.println("");
}
else{
Serial.println("Strings not Equal");
Serial.println("");
}
readString[0] = '\0'; //clear the array
index = 0;
}
}
//*********************FUNCTIONS***************************
long convertToSteps(int inches){
long steps = 130435 * inches; //there are 130435 steps in 1-inch
return(steps);
}
//**************************************************************************