cwalter-at / freemodbus

BSD licensed MODBUS RTU/ASCII and TCP slave
720 stars 379 forks source link

eMBSetSlaveID without "Byte Count" #17

Open KVAnton-WEB opened 4 years ago

KVAnton-WEB commented 4 years ago

PI–MBUS–300 Data and Control Functions 49

I checked the utility mbpoll: mbpoll -m rtu -a 0x0A -u -b 38400 -d 8 -s 2 -P none /dev/ttyUSB0 -v and get this result:

[0A][11][C7][1C]
Waiting for a confirmation...
ERROR Connection timed out: select
Report slave ID failed(-1): Connection timed out
<0A><11><34><FF><AA><BB><CC><61><14>

I think this code will be more correct or not (why)?:

eMBErrorCode
eMBSetSlaveID( UCHAR ucSlaveID, BOOL xIsRunning,
               UCHAR const *pucAdditional, USHORT usAdditionalLen )
{
    eMBErrorCode    eStatus = MB_ENOERR;

    /* the first byte in the buffer is reserved for the Byte Count
     * the second byte in the buffer is reserved for the parameter ucSlaveID
     * the third byte in the buffer is reserver for running flag.
     * The rest of the buffer is available for additional data. */
    if( usAdditionalLen + 3 <= MB_FUNC_OTHER_REP_SLAVEID_BUF )
    {
        usMBSlaveIDLen = 0;
        ucMBSlaveID[usMBSlaveIDLen++] = 2 + usAdditionalLen;
        ucMBSlaveID[usMBSlaveIDLen++] = ucSlaveID;
        ucMBSlaveID[usMBSlaveIDLen++] = ( UCHAR )( xIsRunning ? 0xFF : 0x00 );
        if( usAdditionalLen > 0 )
        {
            memcpy( &ucMBSlaveID[usMBSlaveIDLen], pucAdditional,
                    ( size_t )usAdditionalLen );
            usMBSlaveIDLen += usAdditionalLen;
        }
    }
    else
    {
        eStatus = MB_ENORES;
    }
    return eStatus;
}

with this code, I get the correct response

[0A][11][C7][1C]
Waiting for a confirmation...
<0A><11><05><34><FF><AA><BB><CC><12><75>
Length: 5
Id    : 0x34
Status: On
Data  : \AA\BB\CC
pvyleta commented 4 years ago

@KVAnton-WEB You are correct, the Byte Count is missing in the implementation, I checked via pymodbus, and in the standard. Good catch!