Modbus RTU with function code1
-
Dear all, I am trying to implement modbus RTU with function code 1 . The code i have written implemented for function code 3 . i will get proper response for function code 3. When i implemented for function code 1 i am getting invalid checksum response error. The crc function is working fine . I have checked modscan32, excel sheet of simplymodbus website. Simply Modbus - Enron Modbus Function Code 01 - Read Boolean Variables[^] [^] On-line CRC calculation and free library[^] CRC calculation code
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>= 1;
crc ^= poly;
}
else
crc >>= 1;
}
}
return (crc);
}Main function
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\]=
-
Dear all, I am trying to implement modbus RTU with function code 1 . The code i have written implemented for function code 3 . i will get proper response for function code 3. When i implemented for function code 1 i am getting invalid checksum response error. The crc function is working fine . I have checked modscan32, excel sheet of simplymodbus website. Simply Modbus - Enron Modbus Function Code 01 - Read Boolean Variables[^] [^] On-line CRC calculation and free library[^] CRC calculation code
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>= 1;
crc ^= poly;
}
else
crc >>= 1;
}
}
return (crc);
}Main function
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\]=
You cannot use the same "logic" for function 01 as for function 03. Function 01 deals with "coils"; i.e. bits. Function 03 deals with "registers"; i.e. unsigned shorts. The "sends" are the same except for the function code; on receive however, you get (bit equivalent) "bytes" back for function 01; and 2 bytes for each each register requested in function 03.
"(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal