1.0.2 • Published 2 years ago

@mangar2/rs485interface v1.0.2

Weekly downloads
-
License
LGPL-3.0-or-later
Repository
github
Last release
2 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

Global functions

prepare

prepare (config, rs485Interface)

Creates a new rs485Interface object , if not already existing

prepare Parameters

NameTypeAttributeDefaultDescription
configObjectconfiguration for the rs485 interface
rs485InterfaceRS485Interfaceoptionalnullalready existing interface object

Class AddressChain

new AddressChain()

Contains an ordered chain of addresses

AddressChain Methods

insert

insert (address, version)

Inserts an address , version pair to an ordered address chain

insert Parameters
NameTypeDescription
addressnumberaddress of the element to insert
versionnumberversion of the element to insert

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

handleMessage

handleMessage (message)

Processes an incoming mqtt message

handleMessage Parameters
NameTypeDescription
messageMessagemqtt message

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'

run

async run ()

Starts the server