@mangar2/rs485interface
v1.0.2
Published
Arduino connection over RS485
Downloads
1
Readme
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
| | | | --- | --- | | File | index.js | | 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 | | 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
| Name | Type | Description |
| ---------- | ------------ | ----------------- |
| message
| Message
| message to publish | |
Send
Callback to send an array of bytes to a receiver
Send Parameters
| Name | Type | Description |
| ---------- | ------------ | ----------------- |
| byteArray
| Array
| Array of bytes | |
Global functions
prepare
prepare (config, rs485Interface)
Creates a new rs485Interface object , if not already existing
prepare Parameters
| Name | Type | Attribute | Default | Description |
| ---------- | ------------ | ------------ | ------------ | ----------------- |
| config
| Object
| | | configuration for the rs485 interface | |
| rs485Interface
| RS485Interface
| optional | null | already 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
| Name | Type | Description |
| ---------- | ------------ | ----------------- |
| address
| number
| address of the element to insert | |
| version
| number
| version 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
| Name | Type | Description |
| ---------- | ------------ | ----------------- |
| options
| Object
| configuration options | |
options properties
| Name | Type | Attribute | Default | Description |
| ---------- | ------------ | ------------ | ------------ | ----------------- |
| serialPortName
| string
| | | name of the port to use | |
| baudrate
| integer
| optional | 57600 | baud rate to be used | |
| myAddress
| integer
| optional | 1 | address of this rs485 server | |
| maxVersion
| integer
| optional | 1 | maximal suported interface version | |
| tickDelay
| integer
| optional | 100 | inner clock managing the token , every action is calculated | |
| timeOfDayDelayInSeconds
| integer
| optional | 60 | delay between sending the time of day | |
| qos
| 0, 1, 2
| optional | 1 | quality of service to send messages | |
| trace
| string
| optional | '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
| Type | Description |
| ---- | ----------- |
| Array.<Object.<topic:string, qos:number>>
| list of subscription strings |
handleMessage
handleMessage (message)
Processes an incoming mqtt message
handleMessage Parameters
| Name | Type | Description |
| ---------- | ------------ | ----------------- |
| message
| Message
| mqtt message | |
on
on (event, callback)
Sets a callback .
on Parameters
| Name | Type | Description |
| ---------- | ------------ | ----------------- |
| event
| string
| event name ( not case sensitive ) for the callback ( supported : 'publish' ) | |
| callback
| Publish
| function ( . . . parameter ) | |
on throws
| Type | Description |
| ---- | ----------- |
| Error
| if the event is not supported |
| Error
| if the callback is not 'function' |
run
async run ()
Starts the server