How to use the crc.crc16modbus function in crc

To help you get started, we’ve selected a few crc examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Cloud-Automation / node-modbus / src / modbus-serial-client.js View on Github external
var onData = function (pdu) {
      this.log.debug('received data')
      if (crc.crc16modbus(pdu) === 0) { /* PDU is valid if CRC across whole PDU equals 0, else ignore and do nothing */
        this.emit('data', pdu.slice(1))
      }
    }.bind(this)
github Cloud-Automation / node-modbus / src / rtu-request-handler.js View on Github external
handle (response) {
    if (!response) {
      return
    }

    let request = this._currentRequest

    if (!request) {
      debug('no current request, no idea where this came from')
      return
    }

    let crc = CRC.crc16modbus(response.body.payload)

    if (response.crc !== crc) {
      debug('CRC does not match')
      request.reject({
        'err': 'crcMismatch',
        'message': 'the response payload does not match the crc'
      })
      this._currentRequest = null
      this._flush()
      return
    }

    /* check that response fc equals request id */
    if (response.body.fc !== request.body.fc) {
      debug('something is weird, request fc and response fc do not match.')
      /* clear all request, client must be reset */
github Cloud-Automation / node-modbus / src / rtu-response.js View on Github external
createPayload () {
    /* Payload is a buffer with:
     * Address/Unit ID = 1 Byte
     * Body = N Bytes
     * CRC = 2 Bytes
     */
    const payload = Buffer.alloc(this.byteCount)
    payload.writeUInt8(this._address, 0)
    const bodyPayload = this._body.createPayload()
    bodyPayload.copy(payload, 1)
    this._crc = CRC.crc16modbus(payload.slice(0, this.byteCount - 2 /* CRC bytes */))
    payload.writeUInt16LE(this._crc, this.byteCount - 2)
    return payload
  }
}
github Cloud-Automation / node-modbus / src / rtu-request.js View on Github external
}

      const address = buffer.readUInt8(0)

      debug(`rtu header complete, address, ${address}`)
      debug('buffer', buffer)

      // NOTE: This is potentially more than the body; the body length isn't know at this point...
      const body = CommonRequestBody.fromBuffer(buffer.slice(1))

      if (!body) {
        return null
      }

      const payloadLength = 1 /* address */ + body.byteCount
      const expectedCrc = CRC.crc16modbus(buffer.slice(0, payloadLength))
      const actualCrc = buffer.readUInt16LE(payloadLength)
      const corrupted = (expectedCrc !== actualCrc)

      return new ModbusRTURequest(address, body, corrupted)
    } catch (e) {
      debug('not enough data to create a rtu request', e)
      return null
    }
  }
  constructor (address, body, corrupted) {
github Cloud-Automation / node-modbus / src / modbus-serial-client.js View on Github external
var onSend = function (pdu) {
      var base = Buffer.from([0x01])
      var buf = Buffer.concat([base, pdu])
      var crc16 = crc.crc16modbus(buf)
      var crcBuf = Buffer.allocUnsafe(2)

      crcBuf.writeUInt16LE(crc16, 0)

      var pkt = Buffer.concat([buf, crcBuf])

      serialport.write(pkt, function (err) {
        if (err) {
          this.emit('error', err)
        }
      }.bind(this))
    }.bind(this)
github Cloud-Automation / node-modbus / src / rtu-request.js View on Github external
createPayload () {
    const bodyPayload = this._body.createPayload()

    this._crc = CRC.crc16modbus(Buffer.concat([Buffer.from([this._address]), bodyPayload]))
    const crBu = Buffer.alloc(2)
    crBu.writeUInt16LE(this._crc)
    const idBuf = Buffer.from([this._address])
    const payload = Buffer.concat([idBuf, bodyPayload, crBu])

    return payload
  }
github Cloud-Automation / node-modbus / src / rtu-client-request-handler.js View on Github external
debug('new response coming in')
    if (!response) {
      return
    }

    const userRequest = this._currentRequest

    if (!userRequest) {
      debug('something is strange, received a respone without a request')
      return
    }

    const buf = Buffer.concat([Buffer.from([response.address]), response.body.createPayload()])
    debug('create crc from response', buf)

    const crc = CRC.crc16modbus(buf)

    if (response.crc !== crc) {
      debug('CRC does not match', response.crc, '!==', crc)
      userRequest.reject({
        'err': 'crcMismatch',
        'message': 'the response payload does not match the crc'
      })
      this._clearAllRequests()
      return
    }

    super.handle(response)
  }
}

crc

Module for calculating Cyclic Redundancy Check (CRC) for Node.js and the browser.

MIT
Latest version published 1 year ago

Package Health Score

73 / 100
Full package analysis