I have written code as below.
I have made it work with 2 things
1) send request from PC and get response request<SMCB1,1>
2)Send request from PC of specified length<SMCB1,1,100> and get the specified response
Pending
3) Send request from PC <SMCB1,1,delaytime,ON> or <<SMCB1,1,delaytime,OFF> This function has to override request. Is timer is on get data continuously for specified delaytime and if timer is off get back to normal
#include <avr/wdt.h>
String Old_string;
String New_String;
String Received_String;
const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars]; // temporary array for use when parsing
String txtMsg = "";
// variables to hold the parsed data
char messageFromPC[numChars] = {
0};
int integerFromPC = 0;
float floatFromPC = 0.0;
static int Length_String=0;
static int Start_Stop=0;
boolean newData = false;
void showParsedData()
{
New_String=New_String+"SMCB1"+","+"1"+","+"1"+","+"1"+","+"1"+","+"1"+","+"1"+","+"1"+","+"1"+","+"1"+","+"1"+","+"1"+","+"1"+","+"1"+","+"1"+","+"1"+","+"1"+","+"1"+","+"1"+","+"1"+","+"1"+","+"4000";
String firstString = Received_String.substring(0,14);
String SecondString=New_String.substring(0,7);
if ((firstString==SecondString)&&Length_String==0)
{
Serial.println(New_String);
}
else
{
//Serial.println("wrong command send");
}
if(Length_String>8 && (firstString==SecondString))
{
Serial.println(New_String.substring(0,Length_String));
}
Received_String="";
New_String="";
}
void parseData()
{
char * strtokIndx; // this is used by strtok() as an index
strtokIndx = strtok(tempChars,","); // get the first part - the string
strcpy(messageFromPC, strtokIndx); // copy it to messageFromPC
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
integerFromPC = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
Length_String = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
Start_Stop = atoi(strtokIndx); // convert this part to an integer
Received_String=Received_String+messageFromPC+","+integerFromPC;
}
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;
}
}
}
void setup()
{
wdt_enable(WDTO_8S);
Serial.begin(57600);
Serial.println("Format1: <SMCB1,1>");
Serial.println("Format1: <SMCB1,1,Length>");
Serial.println("Format1: <SMCB1,1,Timer,On_OFF>");
}
void loop() {
wdt_reset();
recvWithStartEndMarkers() ;
if (newData == true)
{
strcpy(tempChars, receivedChars);
parseData();
showParsedData();
newData = false;
}
}
It looks like you're new here. If you want to get involved, click one of these buttons!