I'm getting closer. I'm a bit confused on how to make sure readString is null terminated..I just chose the last place in the array and put a '\0' there. I made c a character array and null terminated the second spot. So, now the strcmp() function is working. Looking at the serial monitor output, it makes sense that readString is loading up. I've noticed that if I increase the size of the serialRead array, I get more of my code going through. If I don't make it big enough to accept the initialization/firmware strings from the stepper board, it never gets to the " * " and my motors won't start. If I make the readString array around [40] my code will go load for a while and stop. If I make it [100], the code runs for a long time, and doesn't seem to stop. Any clue as to what going on here?
The string compare function that compares my limit code "L,16392" to readString still doesn't work, even if I put an * on the end. So, there is still something fishy there, too.
/*
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[100];
int commandNumber = 0;
int numCommands = 0;
//****************Function Prototypes******
long convertToSteps(int inches);
//***************Setup******************************
void setup() {
delay(1000);
readString[99] = '\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()){
char c[2] = {mySerial.read(), '\0'} ; //get one byte from buffer
strcat(readString, c);
//Serial.print(c);
//Serial.println(" --> c charachter byte");
if(strcmp(c, "*") == 0){
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.print(readString);
Serial.println(" --> Read String");
char response[10] = "L,16392";
response[9] = '\0';
if(strncmp(readString, response, 7) == 0){
Serial.println("Write Q Here");
}
else{
Serial.println("Strings not Equal");
}
readString[0] = '\0'; //clear the array
}
}
//*********************FUNCTIONS***************************
long convertToSteps(int inches){
long steps = 130435 * inches; //there are 130435 steps in 1-inch
return(steps);
}
//**************************************************************************
SERIAL MONITOR OUTPUT BELOW:
M1,-12
SD4DNCRouter 4.3 Oct 3, 2011
o?
- --> Read String
Strings not Equal
-30000XG --> IF LOOP
? - --> Read String
Strings not Equal
30000XG --> IF LOOP
? - --> Read String
Strings not Equal
-30000XG --> IF LOOP
?
G3* --> Read String
Strings not Equal
30000XG --> IF LOOP
?
L,256 - --> Read String
Strings not Equal
-30000XG --> IF LOOP
? - --> Read String
Strings not Equal
30000XG --> IF LOOP
?
G2* --> Read String
Strings not Equal
-30000XG --> IF LOOP
?
L,0 - --> Read String
Strings not Equal
30000XG --> IF LOOP
(REPEATING)