0.1.1 • Published 4 years ago

@mangar2/arduinors485 v0.1.1

Weekly downloads
-
License
LGPL-3.0-or-later
Repository
github
Last release
4 years ago

Abstract

Interface service to communicate with several arduino or similar microcontroller based on a RS485 bus The underlying protocol is token based . Tokens are send from one arduino to the other . The arduino having the token may send . Data is send in an internal message format . The message contains :

  • Byte : Sender address
  • Byte : Receiver address
  • Byte : Reply ( bit 1 ) and message format version ( bit 2 . . 8 )
  • Byte : Message length
  • Byte : Command to handle
  • Word : ( 2 bytes ) value , usual positive integer
  • Word : CRC16 for error handling

Contents

Meta

Fileindex.js
AbstractInterface service to communicate with several arduino or similar microcontroller based on a RS485 bus The underlying protocol is token based . Tokens are send from one arduino to the other . The arduino having the token may send . Data is send in an internal message format . The message contains :
  • Byte : Sender address
  • Byte : Receiver address
  • Byte : Reply ( bit 1 ) and message format version ( bit 2 . . 8 )
  • Byte : Message length
  • Byte : Command to handle
  • Word : ( 2 bytes ) value , usual positive integer
  • Word : CRC16 for error handling | | Author | Volker Böhm | | Copyright | Copyright ( c ) 2020 Volker Böhm | | License | This software is licensed under the GNU LESSER GENERAL PUBLIC LICENSE Version 3 . It is furnished "as is" , without any support , and with no warranty , express or implied , as to its usefulness for any purpose . |

Callback definitions

Publish

Callback to publish messages to the mqtt broker

Publish Parameters

NameTypeDescription
messageMessagemessage to publish

Send

Callback to send an array of bytes to a receiver

Send Parameters

NameTypeDescription
byteArrayArrayArray of bytes

Data

Called when at least one byte has been read from the serial connection

Class RS485Interface

new RS485Interface(options)

Creates a service class to communicate with several arduino over a RS485 bus Most options are preset ( see "configuration . js" for all presets ) . You must provide the serial port name in operating system format and a list of addresses as object

Example

const serialPortName = 'COM1'
// Do not use 0 or 1 as addresses. 0 is used for broadcast events and 1 is the
// default address of this service.
const addresses = {
   myFloor/myRoom/myDevice: 2,
   myOtherFloor/myOtherRoom/myDevice: 3
   ...
}
const arduinoService = new ArduinoRS485({ serialPortName, addresses })

RS485Interface Parameters

NameTypeDescription
optionsObjectconfiguration options

options properties

NameTypeAttributeDefaultDescription
serialPortNamestringname of the port to use
baudrateintegeroptional57600baud rate to be used
myAddressintegeroptional1address of this rs485 server
maxVersionintegeroptional1maximal suported interface version
tickDelayintegeroptional100inner clock managing the token , every action is calculated
timeOfDayDelayInSecondsintegeroptional60delay between sending the time of day
qos0, 1, 2optional1quality of service to send messages
tracestringoptional'messages'trace level , supported errors , messages , token

RS485Interface Methods

close

async close ()

Stops the service

getSubscriptions

getSubscriptions () => {Array.<Object.<topic:string, qos:number>>}

Gets the list of required subscriptions for this service

getSubscriptions returns
TypeDescription
Array.<Object.<topic:string, qos:number>>list of subscription strings

on

on (event, callback)

Sets a callback .

on Parameters
NameTypeDescription
eventstringevent name ( not case sensitive ) for the callback ( supported : 'publish' )
callbackPublishfunction ( . . . parameter )
on throws
TypeDescription
Errorif the event is not supported
Errorif the callback is not 'function'

processMessage

processMessage (message)

Processes an incoming mqtt message

processMessage Parameters
NameTypeDescription
messageMessagemqtt message

Class SerialConnection

new SerialConnection()

Class shielding the serial port class and provides async interfaces The functions open , close , and send data are async functions . Read data by registering the callback 'data' with the on function

SerialConnection Methods

close

async close ()

Closes the serial port

listAvailablePorts

async listAvailablePorts ()

Lists all available ports to console

on

on (event, callback)

Sets a callback .

on Parameters
NameTypeDescription
eventstringevent name ( not case sensitive ) for the callback ( supports 'data' )
callbackDatafunction ( . . . parameter )
on throws
TypeDescription
Errorif the event is not supported
Errorif the callback is not 'function'

open

async open (serialPortName, baudrate) => {Promise}

Opens the serial port

open Parameters
NameTypeDescription
serialPortNamestringname of the serial port ( example "COM1" )
baudratenumberbaud rate ( example 9600 )
open returns
TypeDescription
Promise

sendData

async sendData (byteArray) => {}

Sends data to the serial port

sendData Parameters
NameTypeDescription
byteArrayArray.<byte>serial data as byte stream
sendData returns
TypeDescription
| true , if sending was successful