Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does the CRC check in the Modbus RTU code have any effect? #560

Open
covered0000 opened this issue Aug 12, 2024 · 0 comments
Open

Does the CRC check in the Modbus RTU code have any effect? #560

covered0000 opened this issue Aug 12, 2024 · 0 comments

Comments

@covered0000
Copy link

covered0000 commented Aug 12, 2024

servers\serverserial.js

class ServerSerial extends EventEmitter {
    ...
    modbus._server.on("data", function(data) {
        let recvBuffer = Buffer.from([]);

        modbusSerialDebug({ action: "socket data", data: data });
        recvBuffer = Buffer.concat([recvBuffer, data], recvBuffer.length + data.length);

        while (recvBuffer.length > ADDR_LEN) {
            const requestBuffer = Buffer.alloc(recvBuffer.length);
            recvBuffer.copy(requestBuffer, 0, 0, recvBuffer.length);

            // Move receive buffer on
            recvBuffer = recvBuffer.slice(recvBuffer.length);

            const crc = crc16(requestBuffer.slice(0, -2));
            requestBuffer.writeUInt16LE(crc, requestBuffer.length - 2);
        }
    });




function _parseModbusBuffer(requestBuffer, vector, serverUnitID, sockWriter, options) {
        ...
        if (crc !== crc16(requestBuffer.slice(0, -2))) {
            modbusSerialDebug("wrong CRC of request Buffer");
            return;
        }
    }
}

Explanation:

Before performing the CRC check, the result of const crc = crc16(requestBuffer.slice(0, -2)); is written into requestBuffer using requestBuffer.writeUInt16LE(crc, requestBuffer.length - 2);. This method will always result in the CRC check being correct because the CRC is calculated and immediately written into the buffer. Consequently, when checking the CRC later, it will always match since it was calculated and set in the same manner.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant