5.3 Communication Protocol


The reading and writing of data involve different types. Following are common ones:

  • byte: one byte
  • short: 2-byte signed integer
  • long: 4-byte signed integer
  • float: 4-byte float
  • block: buffer data smaller than 64k (generating rule: [length:short, data[0]:byte, ... data[length-1]], 2-byte length/0~65535


Strings have 3 types:

  • string0: normal string with \0 ending
  • string: ASCII or UTF8 strings (generating rule: [length:byte, string[0]:byte, ... string[length-1]], 1-byte length / 0~255)
  • bigstring: string longer than 256 bytes but smaller than 64k (generating rule: [length:short, string[0]:byte, ... string[length-1]], 2-byte length / 0~65535

Value blocks


// ...
// request data device.asyncWriteProtocol

// match received protocol and read
let data = await device.asyncReadProtocol('f3f4', [0x81, 'byte', 'ffff', 'short'])
  console.error('timeout error');
let [byteVal, shortVal] = data
console.log('take two value: ', byteVal, shortVal);
// ...
// subscribe to receive active or continuous protocol
device.subscribeReadProtocol('f3f4', [0x81, 'byte'], (msg)=>{
  console.log('Potentiometer value: '+ msg[0])

Event blocks

The logic of a hat block can be realized:

  1. Use onAdd to create a loop: activate all hat blocks of the same type when an event is detected
  2. Use onRun to detect whether the event meets the requirements of hat block parameter(s)
  3. Use onRemove to stop the loop

results matching ""

    No results matching ""