#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Location
    Delhi
    Posts
    42
    Rep Power
    6

    Function code 1 PIC c programming


    Dear all,

    I am trying to implement MODBUS RTU with function code 1 -> reading coil resister
    . I am posting my code , i am getting issue with reading response from slave device.
    The same code will work with function code 3 -> holding register ,Please let me know any changes

    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);
    }

    Below function i am using in timer interrupt.Request1.jpg
    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;

    }

    if u check input Vs output. Input request going properly. But response is not coming properly.

    detail are found here.
    Simply Modbus - Enron Modbus Function Code 01 - Read Boolean Variables

    Simply Modbus - Enron Modbus Function Code 03 - Read Numeric Variables
  2. #2
  3. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,971
    Rep Power
    510
    Maybe your program runs too fast, faster than the device can respond. Put in a delay loop. Sleep a bit. I didn't read your program. Good chance to reuse your old computer.

    Maybe ridiculous to cite a problem I had one time, but I beat myself up for quite a while with a program that worked single stepping with the debugger and failed when run full speed.
    Last edited by b49P23TIvg; April 9th, 2018 at 06:07 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo