| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- const {
- MODBUS_CRC_OPTIONS,
- hasValidCrc16Modbus
- } = require('./modbus-rtu/frame.js')
- const {
- getReadBufferHint,
- parseModbusRequest,
- readResponseFromBuffer
- } = require('./modbus-rtu/response.js')
- const {
- getBootloaderResponseLength,
- isBootloaderFrame
- } = require('./bootloader/frame.js')
- const {
- BYTE_ORDER_HIGH,
- hasValidCrc16Ccitt
- } = require('../utils/crc.js')
- function inspectReceivedBytes(rawBytes, context = {}) {
- if (!Array.isArray(rawBytes) || rawBytes.length < 4) return ''
- if (isBootloaderFrame(rawBytes)) {
- const expectedLength = getBootloaderResponseLength(rawBytes)
- if (expectedLength && rawBytes.length < expectedLength) return '片段'
- return hasValidCrc16Ccitt(rawBytes, { byteOrder: BYTE_ORDER_HIGH }) ? 'CRC OK' : 'CRC ERR'
- }
- return hasValidCrc16Modbus(rawBytes, MODBUS_CRC_OPTIONS) ? 'CRC OK' : (context.pendingRequest ? '片段' : 'CRC ERR')
- }
- function parseSendExpected(bytes) {
- const expected = parseModbusRequest(bytes)
- if (!expected) return expected
- if (expected.slaveAddress === 0x00 && [0x05, 0x06, 0x10, 0x42].includes(expected.functionCode)) return null
- if (expected.slaveAddress === 0x00 && [0x01, 0x02, 0x03, 0x04, 0x40, 0x41].includes(expected.functionCode)) return null
- return expected
- }
- module.exports = {
- getResponseBufferHint: getReadBufferHint,
- inspectReceivedBytes,
- parseSendExpected,
- readResponseFromBuffer
- }
|