Modbus Protocol Implimentation

Dear all.

I am trying to implement Modbus protocol.Previously i used Modbus library function.I found We cant send request In below format. library help to store the values and sending request and receiving request will take care itself. The code
I downloaded library from here and its working fine . but they didnt code for CRC error what should be done particular error.

Here is My Serial code without Modbus.

#include "glob.h"
const int BUFFER_SZ = 32;
char sendBuffer[200]; 
static int OverRide=0;

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(150);
    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_Command1()

{
  Take_Reading();
  int Temp_read=Take_Temp_Reading();
  int HV_Read=Take_HV_Reading();
  SPD=SPD_Check();
  DISCONNECTOR=DC_Status();
  // Device_ID=ID_Check();
  Device_ID=1;
  Serial.print("SMCB1");
  Serial.print(",");
  Serial.print(Device_ID);
  Serial.print(",");
  for (int i = 0; i < 14; i++) {
    Serial.print(Mod_current[i]);
    Serial.print(",");
  }

  for (int i = 23; i > 13; i--) {
    Serial.print(Mod_current[i]);
    Serial.print(",");
  }

  Serial.print(Temp_read);
  Serial.print(",");
  Serial.print(HV_Read);
  Serial.print(",");
  Serial.print(SPD);
  Serial.print(",");
  Serial.print(DISCONNECTOR);
  Serial.print(",");

  Serial.println("END");
}

void Serial_Command2(int a)
{
  Take_Reading();
  int Temp_read=Take_Temp_Reading();
  int HV_Read=Take_HV_Reading();
  SPD=SPD_Check();
  DISCONNECTOR=DC_Status();
  Device_ID=1;
  // Serial.print("Command2 executed: ");
  snprintf(sendBuffer, a, "SMCB1,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d ,END",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[12], Mod_current[13], Mod_current[14], 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], Temp_read, HV_Read, SPD, DISCONNECTOR);
  Serial.println(sendBuffer);
  //Serial.println(a);
}

void setup()
{
  Serial.begin(57600);
  Serial.println("Format 1: <SMCB1,1>");
  Serial.println("Format 2: <SMCB1,1,Length>");
  Serial.println("Format 2: <SMCB1,1,Length,ON> ON- turn on timer ,off to turn off timer");
}

// Parse the request, WITHOUT the '<' and '>' delimiters.
void parseRequest(char *request)
{
  // Check the request starts with the prefix "SMCB1,1".
  if (strncmp(request, "SMCB1,1", 7) != 0) {
    Serial.println(F("Error: bad request prefix"));
    //  Serial_Command1();
    return;
  }
  request += 7;
  if (request[0] == '\0') {
    //Serial.println(F("Received format 1"));
    Serial_Command1();
    return;
  }
  if (request[0] != ',') {
    Serial.println(F("Error: ',' expected"));
    return;
  }
  request++;
  char * comma = strchr(request, ',');
  if (!comma) {

    int length = atoi(request);
    Serial_Command2(length);
    return;
  }
  int timer = atoi(request);
  char * on_off = comma + 1;
  char * Compare="ON";
  bool on = strcmp(on_off, "ON") == 0;
  if (!on && strcmp(on_off, "OFF") != 0) {  // sanity check
    Serial.println(F("Error: expected either ON or OFF"));
    return;
  }
  int v = strcmp(on_off, Compare);
  if(v==0)
  {
    Serial.println("Timer is on");
    OverRide=1;
  }
  else
  {
    Serial.println("Timer is OFF");
    OverRide=0;
  }

}

void loop()
{
  static char buffer[BUFFER_SZ];  // received chars
  static size_t pos;              // current position in buffer
  static bool insideRequest;      // are we between '<' and '>'?
  //Serial.println("Loop Begin");
  if (Serial.available()) {
    // Serial.println("Loopsfsfsfsf executed");
    char c = Serial.read();
    if (insideRequest) {
      if (c == '>') {  // end of request received
        buffer[pos] = '\0';  // terminate the string
        parseRequest(buffer);
        pos = 0;             // get ready for the next request
        insideRequest = false;
      }
      else if (pos < BUFFER_SZ-1) {  // add char to buffer
        buffer[pos++] = c;
      }
    }
    else if (c == '<') {  // start of request
      insideRequest = true;
    }
    //  Serial.println("Loop executed");
  }


}

I have attached AVR c code for atmega328 with Modbus response.

Is there any code avilable written in C with implimented modbus functional code.

AVR C CODE.txt (5.16 KB)