CrazyEngineers
  • Interfacing Modbus RTU with PIC

    shahrul

    Member

    Updated: Oct 21, 2024
    Views: 1.4K
    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.
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • shahrul

    MemberFeb 19, 2010

    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?
    Are you sure? This action cannot be undone.
    Cancel
  • shahrul

    MemberFeb 23, 2010

    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;j<DataLength;j++){
        CheckSum=CheckSum^response[j];
        for(i=8;i>0;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
    Are you sure? This action cannot be undone.
    Cancel
  • shahrul

    MemberMar 4, 2010

    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);}
    Are you sure? This action cannot be undone.
    Cancel
  • Ashraf HZ

    MemberMar 4, 2010

    This is quite informative stuff. Keep us updated on your progress 😀
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register