stephane / libmodbus

A Modbus library for Linux, Mac OS, FreeBSD and Windows
http://libmodbus.org
GNU Lesser General Public License v2.1
3.33k stars 1.71k forks source link

CRC error when talking to WEG Drive #114

Closed michealp closed 11 years ago

michealp commented 11 years ago

I am using the mod bus library to talk to a WEG drive. For the WEG drive it has an additional function:

    Function 43 – Read Device Identification

An example usage looks like:

.::Request::.

Slave Address Function MEI Type Reading Code Object Number CRC low CRC High

[01][2B][0E][01][00][70][77]

.::Response::.

Slave Address Function MEI Type Reading Code Confirmity Level More Follows Next Object Number of Objects Object Code Object Size Object Value ... CRC low CRC High

[01][2B][0E][01][81][00][02][01][1B][Hex of 'CFW-11 220 - 230 V 10A /8A'][02][05][Hex of 'V4.50'][B2][8F]

So to send this request I am using the following code (just experimental code, nothing special):

    uint8_t raw_req[] = { 0x01, 0x2B, 0x0E, 0x01, 0x00 };
    int rc = 0;
    uint8_t rsp[MODBUS_RTU_MAX_ADU_LENGTH];

    rc = modbus_send_raw_request(m_modbus, raw_req, 5 * sizeof(uint8_t));
    if (rc == -1 || rc < 5)
    {
        std::cout << "Raw didn't work " << rc << std::endl;
        return;
    }
    rc = modbus_receive_confirmation(m_modbus, rsp);

    if (rc == 0)
    {
        std::cout << "Ignored" << std::endl;
    }
    else if (rc == -1)
    {
        fprintf(stderr, "ID: %s\n", modbus_strerror(errno));
        return;
    }
    else
    {
        for (unsigned int i=0; i < rc; i++) {
            printf("%.2X ", rsp[i]);
        }
    }

With modbus debug mode enabled I am seeing the following output:

    [01][2B][0E][01][00][70][77]
    Waiting for a confirmation...
    01><2B><0E><01><81>
    ERROR CRC received 181 != CRC calculated BF34
    ID: Invalid CRC

It looks like maybe the WEG drive is not writting the full response, or modbus isn't properly reading it? The <01><81> should be 'Reading Code' and 'Conformity Level', but modbus library thinks it is the CRC because it is at the end of the response.

stephane commented 11 years ago

These functions are only for debugging purpose so don't expect too much from them :) It will better to provide a clean support of this function code.

If you would to pursue your quick hack you need to tweak compute_meta_length_after_function() to learn it about your new function code.

I think it's better to talk about new features on libmodbus mailing list.