Error while getting Modbus response

Dear all ,

I am implimenting modbus RTU in c on atmega328. I am sharing part of code. Below code work fine for function 3. But i get checksome error for response for function code1.
I have make ensure CRC function is working well with below code. But sending modbus response i get an error.

I have attached Screen shot of request/response from slave device.

crc calculation:

CRC function

unsigned int crc_fn(unsigned char *dpacket,unsigned int len) // CRC Function(Error calcualtion)
{
 unsigned int crc = 0xffff,poly = 0xa001;
 unsigned int i=0; 
   
 for(i=0;i<len;i++)
 {
    crc^= dpacket[i];
    for(j=0;j<8;j++)
    {
     if(crc & 0x01)
     {
      crc >>= 1;
      crc ^= poly;
     }
     else
    crc >>= 1;
    }
 }
 return (crc); 
}

Serial_data () i am calling in timer interrupt Below code for Function 1

void Serial_Data()
{
 
    
    unsigned int address,crc1,crc2;
 unsigned char length,i;
    unsigned char Data_Len_Count;
    unsigned char length1;
     
     
    // Serial_1_Send_byte(rxbuf[0]);
    crc2=crc_fn(&rxbuf[0],6); //crc function for received protocol from request 
         __delay_ms(10); // Changed on 20.01.2017
  if((rxbuf[6]==(unsigned char)(crc2))&&(rxbuf[7]==((unsigned char)(crc2>>8))))
     {
                
  if(rxbuf[0]==Device_ID )
  {
            
   if(rxbuf[1]==READ_REG)
   {
    address=(((unsigned int)(rxbuf[2]<<8))+((unsigned int)(rxbuf[3]))); 
    if(rxbuf[5]>=1)
    {
                   
     length1=(rxbuf[5]<249)?(rxbuf[5]+7)/8:0X20;
                    length=(rxbuf[5]*1);
     address=(address*1); 
     ser_data[0]=Device_ID ;
     ser_data[1]=rxbuf[1];
     ser_data[2]=length; 
     ser_data[3]=rxbuf[6];
     ser_data[4]=rxbuf[7];
     ser_data[5]=length1;
     ser_data[6]=00;
     ser_data[7]=00;
     ser_data[8]=00;
     ser_data[9]=00; 
                    
     
     crc_data[0]=Device_ID ;
     crc_data[1]=rxbuf[1];
     crc_data[2]=length;
                     
     j=3;
     for(i=address;i<((address+length));i++)
     {
      crc_data[j++]=ser_data[i+3];
                       // ChecksumCal(length);
     }
     crc1 =crc_fn(&crc_data[0],(length+3)); //crc function for response protocol from the device
                   
                    Serial_1_Send_byte(ser_data[0]);
     Serial_1_Send_byte(rxbuf[1]);
     Serial_1_Send_byte(ser_data[2]);
                    
     for(i=address;i<((address+length));i++)
     {
      Serial_1_Send_byte(ser_data[i+3]);
                        
     }
     Serial_1_Send_byte((unsigned char)crc1);
     Serial_1_Send_byte((unsigned char)(crc1>>8));
                     Serial_1_Send_byte(crc_msb);
                             Serial_1_Send_byte(crc_lsb);
                    
                    
                    
                    for(i=0;i<30;i++)
                    {
                       Serial_1_Send_byte(0);
                    }
                    
    } 
   } 
   __delay_ms(5); 
       }
       
 }
  index=0;
 rec_flag = 0;
   
}

I am suspecting on below parts ,

  1. moving the data into serial buf
    2)calculating length of coil.
crc_data[0]=Device_ID ;
     crc_data[1]=rxbuf[1];
     crc_data[2]=length;
                     
     j=3;
     for(i=address;i<((address+length));i++)
     {
      crc_data[j++]=ser_data[i+3];
                       // ChecksumCal(length);
     }
     crc1 =crc_fn(&crc_data[0],(length+3)); //crc function for response protocol from the device
                   
                    Serial_1_Send_byte(ser_data[0]);
     Serial_1_Send_byte(rxbuf[1]);
     Serial_1_Send_byte(ser_data[2]);
                    
     for(i=address;i<((address+length));i++)
     {
      Serial_1_Send_byte(ser_data[i+3]);
                        
     }
     Serial_1_Send_byte((unsigned char)crc1);
     Serial_1_Send_byte((unsigned char)(crc1>>8));

function code1: http://dghcorp.com/modbus/modbusrtu01.asp

Data HI 7C - Data Coils (17-10) Status
Data LO 03 - Data Coils (19-18) Status

function code 3:http://dghcorp.com/modbus/modbusrtu03.asp

Data HI 7C - Data Register HI Byte
Data LO 93 - Data Register LO Byte

Can someone suggest me what changes can be done to make work on function 1

Can someone suggest me what changes can be done to make work on function 1

Post complete code! Most people think to know where the error must be but more than often it's in another location than they thought.
Before posting your code fix the identation. The IDE may help you with that with the "Auto Format" (Ctrl-T) tool.

From the ModBus stream output I would guess that you're sending the request CRC in the reply and you're not calculating the reply's CRC.

I can understand not posting complete code sometimes, but what @Pylon is saying is true. Its very easy to focus on something that isn't even causing the problem when looking at partial code. I used to do quite a bit of MODBUS work, though its been a while. One thing I can tell you is that besides MODBUS often being extended in a long list of ways, none of which is part of the original MODBUS standard, companies are often very lax at implementing all features properly. So sometimes an unexpected response has more to do with a bad implementation at the other end!

Now the fact that you are successfully getting a response to a request with Function code 3 ( which as you know, actually equates to 4 in the data stream), is a good sign. It means a major source of coding errors, the MODBUS CRC, is being done right. But I know I've fallen victim in my own code to THINKING I was getting a crc error to a request, but actually was getting a 5 byte exception response and was not processing the short response it correctly. Or I was neglecting the fact that exception (error) responses have the function code's Hi bit set. Or I was doing something as silly as mistaking no response for a CRC error, or even forgetting to clear my receive buffer before a transaction (which is VERY helpful when you're at the de-bugging stage). Another "gotcha" is that despite standards being set, all MODBUS implementations seldom follow the same guidelines for parity or even the number of data bits. It can be just your luck that one particular command/response comes through correctly, even with the wrong parity setting.

Finally, if you're doing this kind of work, I'd strongly suggest you take the time to make yourself a little "hexprint" utility, which lets you easily display exactly what bytes are sent and received with nice formatting. I realize this means multiple serial ports, and extra hassle. But in any binary protocol I've worked, I've spotted more problems looking at the "playback" of what exactly came and went, than I have just by looking at my own code.