Interfacing Modbus RTU with PIC

Anybody know the modbus protocol? It can connect to PIC by RS232 connection and PIC can do UART coding. Just want to know how the modbus RTU operation? I keep exploring this and will update the progress.

Replies

  • shahrul
    shahrul
    Now, I see the pattern.

    Example read holding register.
    Request protocol:
    Slave ID: 1 byte
    Function code: 1 byte
    Starting Address: 2 bytes
    Quantity of address: 2 bytes
    CRC-16: 2 bytes
    Response protocol:
    Slave ID: 1 byte
    Function code: 1 byte
    Byte count: 1 byte
    Register value: (2*Quantity of address) bytes
    CRC-16: 2 bytes
    This is the example Tx and Rx data
    Tx: 01 03 00 00 00 03 05 CB
    Rx: 01 03 06 00 01 00 01 00 04 4C B6
    
    Last 2 bytes for Tx and Rx is CRC-16 checking. My further question is, how to calculate CRC-16?
  • shahrul
    shahrul
    Now, I get the CRC-16 function.
    Example
    unsigned int data[]={0x01,0x03,0x00,0x00,0x00,0x03};
    
    int CRC16(int DataLength){
    unsigned int i,j,CheckSum;
    CheckSum=0xFFFF;
    for (j=0;j0;i--){
            if((CheckSum)&0x0001==1) CheckSum=(CheckSum>>1)^0xA001;
            else CheckSum=CheckSum>>1;}}
    CRC_high=(CheckSum>>8)&0x00FF;    
    CRC_low=CheckSum&0x00FF;
    CheckSum=((CheckSum>>8)&0x00FF)|((CheckSum<<8)&0xFF00);
    return CheckSum;
    }
    
    will return value CRC=0x05CB
  • shahrul
    shahrul
    Now, I have problem on parity bit. I have done 8bit UART, that is NONE PARITY.
    The parity bit is setup with 9th bit. I try send this, but it fail at certain data.
    response[0]=0x01;
    response[1]=0x03;
    response[2]=0x06;
    response[3]=0x00;
    response[4]=0x00;
    response[5]=0x00;
    response[6]=0x01;
    response[7]=0x00;
    response[8]=0x02;
    CRC16(9);
    response[9]=CRC_low;
    response[10]=CRC_high;}

    for(i=0;i<=10;i++){
    TX9D=even_parity(response);
    uart_transmit(response);}
  • Ashraf HZ
    Ashraf HZ
    This is quite informative stuff. Keep us updated on your progress 😀

You are reading an archived discussion.

Related Posts

what is the scope of testing as a beginning from fresher level?
I want to get sms alert for every mail, I used way2sms with gmail forwarding feature. way2sms is full of ads that are frustrating and damn slow, I stopped the...
Hello friend i have learned something new and i would like to share it with you. Few children came to my home last week and they began to surf my...
I would like to share my views with the friends . I use fire fox as my web browser. Earlier days i was a fan of IE. But day by...
hey i need to add a propeller to my vehicle any suggestions how to go about it?😕