Serial communication send and receive command
Dear all
I have simple serial receive and parse code as below.This code Will parse data given from PC.
My actual code. Where i am creating NewString value where i am saving my all data in CSV format .Here From PC i am requesting to send data only if request data match with <Device_ID,Device_name>else dont print any data
I have simple serial receive and parse code as below.This code Will parse data given from PC.
const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars]; // temporary array for use when parsing
// variables to hold the parsed data
char messageFromPC[numChars] = {0};
int integerFromPC = 0;
float floatFromPC = 0.0;
boolean newData = false;
//============
void setup() {
Serial.begin(57600);
Serial.println("This demo expects 3 pieces of data - text, an integer and a floating point value");
Serial.println("Enter data in this style <Device_ID, 12, 24.7> ");
Serial.println();
}
//============
void loop() {
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();
showParsedData();
newData = false;
}
}
//============
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 parseData() { // split the data into its parts
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, ",");
floatFromPC = atof(strtokIndx); // convert this part to a float
}
//============
void showParsedData() {
Serial.print("Message ");
Serial.println(messageFromPC);
Serial.print("Integer ");
Serial.println(integerFromPC);
Serial.print("Float ");
Serial.println(floatFromPC);
}
My actual code. Where i am creating NewString value where i am saving my all data in CSV format .Here From PC i am requesting to send data only if request data match with <Device_ID,Device_name>else dont print any data
int Serial_Status=0;
int Mod_current[24];
int Mod_current1[8];
int Mod_current2[8];
int Mod_current3[8];
static int Device_ID=1;
int SO_enable = 5;
int S1_enable = 4;
int S2_enable = 3;
const int numReadings = 10;
const int numReadings1 = 10;
int Temp_Total = 0;
int Temp_readings[numReadings];
int Temp_index = 0;
int Temp_Average = 0;
float reading[10];
float Total = 0.0;
static int i;
String Old_string;
String New_String;
int Mul_Factor = 10;
float ANALOG_SCALING = 0.004887585532746823;
int index = 0;
int total = 0;
int average = 0;
char receivedChar;
//String char_test;
boolean newData = false;
int array[8][3] = {
{
0, 0, 0
}
, {
0, 0, 1
}
, {
0, 1, 0
}
, {
0, 1, 1
}
, {
1, 0, 0
}
, {
1, 0, 1
}
, {
1, 1, 0
}
, {
1, 1, 1
}
};
float Current_Value1[8] = {
0.0
};
float Current_Value2[8] = {
0.0
};
float Current_Value3[8] = {
0.0
};
//HV perameter setting are defined here
int HV_SensorValue;
int HV_Reading;
float HV_voltage;
float Conv_HV_voltage;
// Truth table OF 8:1 Multiplexer cd74hct4051e
uint32_t last_loop = 0; // current value of loop timer:
const uint32_t period_loop = 1000; // loop timer preset value, every 1 second:
unsigned int DC_STATUS = 0;
unsigned int SPD_STATUS = 0;
int8_t state = 0;
uint16_t au16data[30];
float MINV_RANGE = 2.52;
//float MIDV_RANGE1=1.875;
float MAXV_RANGE = 4.5;
int SPD;
int DISCONNECTOR;
float analog_1_pv[8];
float analog_2_pv[8];
float analog_3_pv[8];
static int SPD_STATUS_PIN = 6;
static int DC_STATUS_PIN = 7;
int readings[numReadings];
String Device_Name="ab";
void setup()
{
Serial.begin(57600);
}
void loop()
{
Take_Reading();
Serial_Command();
Serial_call();
delay(1000);
}
void Serial_Command()
{
/* int val = Serial.read() - '0';
if (val == 1)
{ // test for command 1 then turn on LED
Serial.println("RELAY on");
Serial_Status=1;
}
else if (val == 0) // test for command 0 then turn off LED
{
Serial.println("RELAY OFF");
Serial_Status=0;
}*/
recvOneChar();
}
void recvOneChar() {
if (Serial.available() > 0) {
receivedChar = Serial.read();
Serial.print(receivedChar);
newData = true;
}
}
void Serial_call()
{
int HV_value= Take_HV_Reading();
New_String=Old_string+Device_ID+","+Device_Name+","+Mod_current[0]+","+Mod_current[1]+","+Mod_current[2]+","+Mod_current[3]+","+Mod_current[4]+","+Mod_current[5]+","+Mod_current[6]+","+Mod_current[7]+","+Mod_current[8]+","+Mod_current[9]+","+Mod_current[10]+","+Mod_current[11]+ ","+Mod_current[23]+ ","+Mod_current[22]+ ","+Mod_current[21]+ ","+Mod_current[20]+ ","+Mod_current[19]+","+Mod_current[18]+","+Mod_current[17]+","+Mod_current[16]+","+Mod_current[15]+","+Mod_current[14]+","+Mod_current[13]+","+Mod_current[12]+ ","+ 36 +","+ SPD+","+ DISCONNECTOR+","+ HV_value;
if (New_String.startsWith("1"))
{
Serial.println(New_String);
}
else
{
Serial.println("Error device ID not match");
}
}
void Take_Reading() {
for (int row = 0; row < 8; row++)
{
// // get rid of me:
digitalWrite(SO_enable, array[row][0]);
digitalWrite(S1_enable, array[row][1]);
digitalWrite(S2_enable, array[row][2]);
delay(100);
analog_1_pv[row] = ANALOG_SCALING * analogRead(A0);
analog_2_pv[row] = ANALOG_SCALING * analogRead(A1);
analog_3_pv[row] = ANALOG_SCALING * analogRead(A2);
if ( (analog_1_pv[row] <= MINV_RANGE || (analog_1_pv[row] >= MAXV_RANGE) ))
{
Current_Value1[row] = 0.0;
}
else
{
Current_Value1[row] = (analog_1_pv[row] * 12.5) - 31.25;
}
if ( (analog_2_pv[row] <= MINV_RANGE || (analog_2_pv[row] >= MAXV_RANGE) ))
{
Current_Value2[row] = 0.0;
}
else
{
Current_Value2[row] = (analog_2_pv[row] * 12.5) - 31.25;
}
if ( (analog_3_pv[row] <= MINV_RANGE || (analog_3_pv[row] >= MAXV_RANGE) ))
{
Current_Value3[row] = 0.0;
}
else
{
Current_Value3[row] = (analog_3_pv[row] * 12.5) - 31.25;
}
Mod_current[row] = (uint16_t)(Mul_Factor * Current_Value1[row]);
Mod_current[row + 8] = (uint16_t)(Mul_Factor * Current_Value2[row]);
Mod_current[row + 16] = (uint16_t)(Mul_Factor * Current_Value3[row]);
}
}
int SPD_Check()
{
SPD_STATUS = digitalRead(SPD_STATUS_PIN);
return (SPD_STATUS);
}
int DC_Status()
{
DC_STATUS = digitalRead(DC_STATUS_PIN);
return (DC_STATUS);
}
int Take_HV_Reading()
{
//int analog_int= analogRead(A4);
total = total - readings[index];
// read from the sensor:
readings[index] = analogRead(A4);
// add the reading to the total:
total = total + readings[index];
// advance to the next position in the array:
index = index + 1;
// if we're at the end of the array...
if (index >= numReadings1)
{
index = 0;
average = total / numReadings1;
}
else
{
HV_voltage = (average * 5.0) / 1023.0;
}
if (HV_voltage <= 0.25)
{
Conv_HV_voltage = 0.0;
}
else
{
Conv_HV_voltage = 197.837837838 * HV_voltage + 10.8108108108;
}
HV_Reading = (uint16_t)(Conv_HV_voltage * 10);
// HV_Reading = (uint16_t)(Conv_HV_voltage * 10);
return(HV_Reading);
}

Comments
Received_String with first 2 character of New_String values. if matched then only print else dont print.
this data compassion done here
void showParsedData() { Serial.print("data send from PC:"); Serial.println(Received_String); Serial.println(); Serial.println(); Serial.print("Device_name: "); Serial.println(messageFromPC); Serial.print("Device_ID: "); Serial.println(integerFromPC); Serial.println(); Serial.println(); temp_int = Take_Temp_Reading(); temp_int = (uint16_t)temp_int; int HV_value= Take_HV_Reading(); New_String=Old_string+"Device_Name"+","+Device_ID+","+Mod_current[0]+","+Mod_current[1]+","+Mod_current[2]+","+Mod_current[3]+","+Mod_current[4]+","+Mod_current[5]+","+Mod_current[6]+","+Mod_current[7]+","+Mod_current[8]+","+Mod_current[9]+","+Mod_current[10]+","+Mod_current[11]+ ","+Mod_current[23]+ ","+Mod_current[22]+ ","+Mod_current[21]+ ","+Mod_current[20]+ ","+Mod_current[19]+","+Mod_current[18]+","+Mod_current[17]+","+Mod_current[16]+","+Mod_current[15]+","+Mod_current[14]+","+Mod_current[13]+","+Mod_current[12]+ ","+ temp_int +","+ SPD+","+ DISCONNECTOR+","+ HV_value; Serial.println(New_String); Received_String=""; }My actual code
#include <avr/wdt.h> int array[8][3] = { { 0, 0, 0 } , { 0, 0, 1 } , { 0, 1, 0 } , { 0, 1, 1 } , { 1, 0, 0 } , { 1, 0, 1 } , { 1, 1, 0 } , { 1, 1, 1 } }; int counter=0; 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 // variables to hold the parsed data char messageFromPC[numChars] = { 0}; int integerFromPC = 0; float floatFromPC = 0.0; boolean newData = false; int Mod_current[24]; int Mod_current1[8]; int Mod_current2[8]; int Mod_current3[8]; const int numReadings = 10; const int numReadings1 = 10; int Temp_Total = 0; int Temp_readings[numReadings]; int Temp_index = 0; int Temp_Average = 0; float reading[10]; float Total = 0.0; static int i; int readings[numReadings]; int index = 0; int total = 0; int average = 0; // These PinS are use to enable and disable analog MUX int SO_enable = 5; int S1_enable = 4; int S2_enable = 3; // temprature sensor permeter int TempPin = 5; float temp; static int temp_int; // digital mux setting int Output_Read = 2; static int Device_ID=1; float Vref = 2.5; // Used for sensor conversion float ANALOG_SCALING = 0.004887585532746823; //tempfloat ANALOG_SCALING=0.0049560117; int Sensor_Value0 = 0; int Sensor_Value1 = 0; int Sensor_Value2 = 0; float analog_1_pv[8]; float analog_2_pv[8]; float analog_3_pv[8]; int Status_Out[8]; //SPD & DC STATUS PIN static int SPD_STATUS_PIN = 6; static int DC_STATUS_PIN = 7; int row; int Mul_Factor = 10; float Current_Value1[8] = { 0.0 }; float Current_Value2[8] = { 0.0 }; float Current_Value3[8] = { 0.0 }; //HV perameter setting are defined here int HV_SensorValue; int HV_Reading; float HV_voltage; float Conv_HV_voltage; // Truth table OF 8:1 Multiplexer cd74hct4051e uint32_t last_loop = 0; // current value of loop timer: const uint32_t period_loop = 1000; // loop timer preset value, every 1 second: unsigned int DC_STATUS = 0; unsigned int SPD_STATUS = 0; int8_t state = 0; uint16_t au16data[30]; float MINV_RANGE = 2.52; //float MIDV_RANGE1=1.875; float MAXV_RANGE = 4.5; int SPD; int DISCONNECTOR; int Serial_Status=0; void setup() { wdt_enable(WDTO_8S); Serial.begin(57600); //analogReference(INTERNAL); pinMode(3, OUTPUT); pinMode(SO_enable, OUTPUT) ;// pin can enable/disable using digital IO 7 of arduino pinMode(S1_enable, OUTPUT) ;// pin can enable/disable using digital IO 6 of arduino pinMode(S2_enable, OUTPUT) ;// pin can enable/disable using digital IO 5 of arduino // pinMode(Enablepin, OUTPUT) ;// pin can enable/disable using digital IO 4 of arduino pinMode(A0, INPUT) ; pinMode(A5, INPUT) ; //spd & DC STATUS pinMode(SPD_STATUS_PIN, INPUT); pinMode(DC_STATUS_PIN, INPUT); /* if (millis() < 10000) { Device_ID = ID_Check(); } */ Serial.println("Enter data in this style <Device_ID, 12, 24.7> "); } void loop() { wdt_reset(); 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(); showParsedData(); newData = false; } Device_ID = 1; int HV_value= Take_HV_Reading(); Take_Reading(); // Serial_call(); SPD = SPD_Check(); DISCONNECTOR = DC_Status(); // Serial_Command(); } 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 parseData() { // split the data into its parts Serial.println(receivedChars); 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 Received_String=Received_String+messageFromPC+","+integerFromPC; } //============ void showParsedData() { Serial.print("data send from PC:"); Serial.println(Received_String); Serial.println(); Serial.println(); Serial.print("Device_name: "); Serial.println(messageFromPC); Serial.print("Device_ID: "); Serial.println(integerFromPC); Serial.println(); Serial.println(); temp_int = Take_Temp_Reading(); temp_int = (uint16_t)temp_int; int HV_value= Take_HV_Reading(); New_String=Old_string+"Device_Name"+","+Device_ID+","+Mod_current[0]+","+Mod_current[1]+","+Mod_current[2]+","+Mod_current[3]+","+Mod_current[4]+","+Mod_current[5]+","+Mod_current[6]+","+Mod_current[7]+","+Mod_current[8]+","+Mod_current[9]+","+Mod_current[10]+","+Mod_current[11]+ ","+Mod_current[23]+ ","+Mod_current[22]+ ","+Mod_current[21]+ ","+Mod_current[20]+ ","+Mod_current[19]+","+Mod_current[18]+","+Mod_current[17]+","+Mod_current[16]+","+Mod_current[15]+","+Mod_current[14]+","+Mod_current[13]+","+Mod_current[12]+ ","+ temp_int +","+ SPD+","+ DISCONNECTOR+","+ HV_value; Serial.println(New_String); Received_String=""; } void Serial_call() { // Device_ID=ID_Check(); temp_int = Take_Temp_Reading(); temp_int = (uint16_t)temp_int; int HV_value= Take_HV_Reading(); New_String=Old_string+Device_ID+","+Mod_current[0]+","+Mod_current[1]+","+Mod_current[2]+","+Mod_current[3]+","+Mod_current[4]+","+Mod_current[5]+","+Mod_current[6]+","+Mod_current[7]+","+Mod_current[8]+","+Mod_current[9]+","+Mod_current[10]+","+Mod_current[11]+ ","+Mod_current[23]+ ","+Mod_current[22]+ ","+Mod_current[21]+ ","+Mod_current[20]+ ","+Mod_current[19]+","+Mod_current[18]+","+Mod_current[17]+","+Mod_current[16]+","+Mod_current[15]+","+Mod_current[14]+","+Mod_current[13]+","+Mod_current[12]+ ","+ temp_int +","+ SPD+","+ DISCONNECTOR+","+ HV_value; if (New_String.startsWith("1")) { // Serial.println(New_String); } else { Serial.println("Error device ID not match"); } } void Take_Reading() { for (int row = 0; row < 8; row++) { // // get rid of me: digitalWrite(SO_enable, array[row][0]); digitalWrite(S1_enable, array[row][1]); digitalWrite(S2_enable, array[row][2]); delay(100); analog_1_pv[row] = ANALOG_SCALING * analogRead(A0); analog_2_pv[row] = ANALOG_SCALING * analogRead(A1); analog_3_pv[row] = ANALOG_SCALING * analogRead(A2); if ( (analog_1_pv[row] <= MINV_RANGE || (analog_1_pv[row] >= MAXV_RANGE) )) { Current_Value1[row] = 0.0; } else { Current_Value1[row] = (analog_1_pv[row] * 12.5) - 31.25; } if ( (analog_2_pv[row] <= MINV_RANGE || (analog_2_pv[row] >= MAXV_RANGE) )) { Current_Value2[row] = 0.0; } else { Current_Value2[row] = (analog_2_pv[row] * 12.5) - 31.25; } if ( (analog_3_pv[row] <= MINV_RANGE || (analog_3_pv[row] >= MAXV_RANGE) )) { Current_Value3[row] = 0.0; } else { Current_Value3[row] = (analog_3_pv[row] * 12.5) - 31.25; } Mod_current[row] = (uint16_t)(Mul_Factor * Current_Value1[row]); Mod_current[row + 8] = (uint16_t)(Mul_Factor * Current_Value2[row]); Mod_current[row + 16] = (uint16_t)(Mul_Factor * Current_Value3[row]); } } int ID_Check() { int ID_value; for (int row = 0; row < 8; row++) { digitalWrite(SO_enable, array[row][0]); digitalWrite(S1_enable, array[row][1]); digitalWrite(S2_enable, array[row][2]); Status_Out[row] = digitalRead(Output_Read); } ID_value = 1 * Status_Out[7] + 2 * Status_Out[6] + 4 * Status_Out[5] + 8 * Status_Out[4] + 16 * Status_Out[3] + 32 * Status_Out[2] + 64 * Status_Out[1] + 128 * Status_Out[0]; return (ID_value); } int Take_Temp_Reading() { Temp_Total = Temp_Total - Temp_readings[Temp_index]; Temp_readings[Temp_index] = analogRead(A5); Temp_Total = Temp_Total + Temp_readings[Temp_index]; Temp_index = Temp_index + 1; if (Temp_index >= numReadings) { Temp_index = 0; Temp_Average = Temp_Total / numReadings; } temp = (Temp_Average * 5.0) / 1023.0; // temp = float(analogRead(A5)) * 5.0 / 1024.0; temp_int = (int)(temp * 100.0); return (temp_int); // Serial.print("Temp"); Serial.println(temp); } int SPD_Check() { SPD_STATUS = digitalRead(SPD_STATUS_PIN); return (SPD_STATUS); } int DC_Status() { DC_STATUS = digitalRead(DC_STATUS_PIN); return (DC_STATUS); } int Take_HV_Reading() { //int analog_int= analogRead(A4); total = total - readings[index]; // read from the sensor: readings[index] = analogRead(A4); // add the reading to the total: total = total + readings[index]; // advance to the next position in the array: index = index + 1; // if we're at the end of the array... if (index >= numReadings1) { index = 0; average = total / numReadings1; } else { HV_voltage = (average * 5.0) / 1023.0; } if (HV_voltage <= 0.25) { Conv_HV_voltage = 0.0; } else { Conv_HV_voltage = 197.837837838 * HV_voltage + 10.8108108108; } HV_Reading = (uint16_t)(Conv_HV_voltage * 10); // HV_Reading = (uint16_t)(Conv_HV_voltage * 10); return(HV_Reading); } void Serial_Command() { int val = Serial.read() - '0'; if (val == 1) { // test for command 1 then turn on LED Serial.println("RELAY on"); Serial_Status=1; } else if (val == 0) // test for command 0 then turn off LED { Serial.println("RELAY OFF"); Serial_Status=0; } }