Ok so the progress so far,
I can send readable string commands without any problem.
when i try to add parts from grbl code transmission it gets corrupted
GRBL_msg4 should be FS:0,7500 for a spindle speed of 7500 (with feedrate 0 - not reqd) but the previous and next string from the array keeps breaking through corrupting the value.
when i send the value out via serial port at 9600 Baud i get a constant stream of FS:0,7500 but when the same is transmitted at 2400 Baud it gets the spurious messages.
So if i can find a way of guaranteeing the parse data to always output FS:0,7500 then the project is almost cracked.
it would appear that the slower baud rate is triggered when the parse isn't complete ?
like there is a timing issue or order of precedence issue.
#include <SoftwareSerial.h>
#define rx 3
#define tx 2
SoftwareSerial XSERIAL = SoftwareSerial(rx, tx);
const byte LED_PIN = 12;
// Parsing GRBL message variables
const byte numChars =71;
char receivedChars[numChars];
char tempChars[numChars]; // temporary array for use when parsing
//Feed and Speed variables
char FSMsgGRBL[12];
int FS_FeedMsg = 0;
int FS_SpeedMsg=0;
char FS_speed1=0;
char FS_speed2=0;
//Overide variables
bool OvMsg = false; //Ov message being transmitted
int CmdCoolant = 0;
int CmdSpindleCW = 0;
int Mach_fcn =0;
// Variables to hold Message blocks
char GRBL_msg1 [numChars] = {0};
char GRBL_msg2 [numChars] = {0};
char GRBL_msg3 [numChars] = {0};
char GRBL_msg4 [numChars] = {0};
char GRBL_msg5 [numChars] = {0};
char GRBL_msg6 [numChars] = {0};
boolean newData = false;
void setup() {
Serial.begin(115200);
XSERIAL.begin(2400);
//rs485.begin (28800);
//pinMode (ENABLE_PIN, OUTPUT); // RS485 IC driver output enable
pinMode (LED_PIN, OUTPUT); // built-in LED - lights on transmission error
digitalWrite(LED_PIN, HIGH);
Serial.println("Ready");
} // end of setup
//============
void loop() {
// byte level = (data);
recvWithStartEndMarkers();
if (newData == true) {
strcpy(tempChars, receivedChars);
// this temporary copy is necessary to protect the original data
// because strtok() used in parseData() replaces the commas with \0
parseData();
outputParsedData();
newData = false;
}
} // End of loop
//============
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}// End of recvWithStartEndMarkers
//============
void parseData() { // split the data into its parts
char * strtokIndx; // this is used by strtok() as an index
//Parse into Blocks based on "|" Delimiter
// machine state
strtokIndx = strtok(tempChars,"|"); // Find first part of string (Machine State Idle, Run, Alarm)
strcpy(GRBL_msg1, strtokIndx); // write characters from string to machine state variable
// machine position
strtokIndx = strtok(NULL,"|"); // look for next data block in string
strcpy(GRBL_msg2, strtokIndx); // write characters from string to machine position variable
// BF god knows - alarms ?
strtokIndx = strtok(NULL,"|"); // look for next data block in string
strcpy(GRBL_msg3, strtokIndx); // write characters from string to BF variable
// feed and spindle speed
strtokIndx = strtok(NULL,"|"); // look for next data block in string
strcpy(GRBL_msg4, strtokIndx); // write characters from string to Feed&Speed variable
//Ov Overides 1% to 200%
strtokIndx = strtok(NULL,"|"); // look for next data block in string
strcpy(GRBL_msg5, strtokIndx); // write characters from string to Overide variable
//machine command status
strtokIndx = strtok(NULL,"|"); // look for next data block in string
strcpy(GRBL_msg6, strtokIndx); // write characters from string to machine command status variable
//anything else
} //End of parseData
//============
void outputParsedData()
{
ParseFS (); // Parse feed and speed values from FS Block
ParseOv_A (); //Reads first character in buffer looking for "O" then knows commands follow "A:" (A:SF Buffer)
//XSERIAL.println = ((receivedChars));
//String stringOne = ("<");
//String stringTwo = stringOne + String(FS_SpeedMsg);
// XSERIAL.print(stringTwo);
//String stringThree = stringOne + stringTwo;
//String stringFour = (",");
//String stringFive = stringFour + Mach_fcn;
//String stringSix = stringFive + (">");
// XSERIAL.println(stringSix);
XSERIAL.print(GRBL_msg4);
XSERIAL.print("-");
XSERIAL.println(String(Mach_fcn));
}//end of outputParsedData
//Feeds and speeds FS Message GRBL /////////////////////////////////////////////////////////
void ParseFS ()
{
//GRBL_msg4 array containing Feed & Speed message characters
char * partOfString; // this is used by strtok_r() as an index
//FS_SpeedMsg = 0;
// FS_speed1 = 0;
//FS_speed2 = 0;
partOfString = strtok (GRBL_msg3,":"); // get the first part - the string
strcpy(FSMsgGRBL, partOfString); // copy it to inputCsvString
partOfString = strtok (NULL, ","); // this continues where the previous call left off
FS_FeedMsg = atoi(partOfString); // convert this part to an integer
partOfString = strtok (NULL, ","); // this continues where the previous call left off
FS_SpeedMsg = atoi(partOfString); // convert this part to a float// actually an int
//FS_speed1 = (FS_SpeedMsg>>8);
//FS_speed2 = (FS_SpeedMsg & 0xFF);
}//End of ParseFS
// Parse Overide Ov Message GRBL ////////////////////////////////////////////////////////////
void ParseOv_A ()
{
//GRBL_msg5 array containing Overide message characters
if (GRBL_msg4[0] == 'O')// Ov statement when true check machine command instructions
{
if (GRBL_msg5[0] == 'A')
{
//Serial.print("Got A ");
//Serial.println(GRBL_msg5);
if (GRBL_msg5[2] == 'S')
{
CmdSpindleCW =1;
}
if(GRBL_msg5[2] == 'C')
{
CmdSpindleCW = 2;
}
if (GRBL_msg5[2] == 'F'|(GRBL_msg5[3] == 'F'))
{
CmdCoolant = 4;
}
else
{
(CmdCoolant = 0);
}
}//End - seeking "A" in GRBL_msg6
if(strlen(GRBL_msg5) == 0) // makes sure spindle direction and coolant conditions are off woth no signal present
{
CmdSpindleCW = 0;
CmdCoolant = 0;
}
}
Mach_fcn = CmdSpindleCW + CmdCoolant;
} // end of ParseOv_A