1
0

transport-helpers.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. const modbusProtocol = require('./modbus-rtu/index.js')
  2. const storageAccessProtocol = require('./storage-access/index.js')
  3. const {
  4. getBootloaderResponseLength,
  5. isBootloaderFrame
  6. } = require('./bootloader/index.js')
  7. const {
  8. BYTE_ORDER_HIGH,
  9. hasValidCrc16Ccitt
  10. } = require('../utils/crc.js')
  11. function inspectReceivedBytes(rawBytes, context = {}) {
  12. if (!Array.isArray(rawBytes) || rawBytes.length < 4) return ''
  13. if (isBootloaderFrame(rawBytes)) {
  14. const expectedLength = getBootloaderResponseLength(rawBytes)
  15. if (expectedLength && rawBytes.length < expectedLength) return '片段'
  16. return hasValidCrc16Ccitt(rawBytes, { byteOrder: BYTE_ORDER_HIGH }) ? 'CRC OK' : 'CRC ERR'
  17. }
  18. const pendingProtocol = context.pendingRequest && context.pendingRequest.protocol
  19. if (pendingProtocol === 'storage-access') {
  20. return storageAccessProtocol.hasValidStorageCrc(rawBytes) ? 'CRC OK' : '片段'
  21. }
  22. if (pendingProtocol === 'modbus-rtu') {
  23. return modbusProtocol.hasValidCrc16Modbus(rawBytes, modbusProtocol.MODBUS_CRC_OPTIONS) ? 'CRC OK' : '片段'
  24. }
  25. if (storageAccessProtocol.hasValidStorageCrc(rawBytes)) return 'CRC OK'
  26. return modbusProtocol.hasValidCrc16Modbus(rawBytes, modbusProtocol.MODBUS_CRC_OPTIONS) ? 'CRC OK' : (context.pendingRequest ? '片段' : 'CRC ERR')
  27. }
  28. function parseSendExpected(bytes) {
  29. const storageExpected = storageAccessProtocol.response.parseStorageAccessRequest(bytes)
  30. if (storageExpected) return storageExpected
  31. const expected = modbusProtocol.response.parseModbusRequest(bytes)
  32. if (!expected) return expected
  33. if (expected.slaveAddress === 0x00 && [0x05, 0x06, 0x10].includes(expected.functionCode)) return null
  34. if (expected.slaveAddress === 0x00 && [0x01, 0x02, 0x03, 0x04].includes(expected.functionCode)) return null
  35. return expected
  36. }
  37. function getResponseBufferHint(expected) {
  38. if (expected && expected.protocol === 'storage-access') {
  39. return storageAccessProtocol.response.getReadBufferHint(expected)
  40. }
  41. if (expected && expected.protocol === 'modbus-rtu') {
  42. return modbusProtocol.response.getReadBufferHint(expected)
  43. }
  44. return modbusProtocol.response.getReadBufferHint(expected)
  45. }
  46. function readResponseFromBuffer(buffer, expected, options = {}) {
  47. if (expected && expected.protocol === 'storage-access') {
  48. return storageAccessProtocol.response.readResponseFromBuffer(buffer, expected, options)
  49. }
  50. if (expected && expected.protocol === 'modbus-rtu') {
  51. return modbusProtocol.response.readResponseFromBuffer(buffer, expected, options)
  52. }
  53. return modbusProtocol.response.readResponseFromBuffer(buffer, expected, options)
  54. }
  55. module.exports = {
  56. getResponseBufferHint,
  57. inspectReceivedBytes,
  58. parseSendExpected,
  59. readResponseFromBuffer
  60. }