Serial communication send and receive command
ajit.nayak87
Posts: 76
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
My actual code