Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Hardware & Devices
  4. Modbus RTU with function code1

Modbus RTU with function code1

Scheduled Pinned Locked Moved Hardware & Devices
htmlhelpquestion
2 Posts 2 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Member 10515225
    wrote on last edited by
    #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\]=
    
    L 1 Reply Last reply
    0
    • M Member 10515225

      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\]=
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups