@mangar2/arduinors485 v0.1.1
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 |
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
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 |
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' |
processMessage
processMessage (message)
Processes an incoming mqtt message
processMessage Parameters
Name | Type | Description | |
---|---|---|---|
message | Message | mqtt 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
Name | Type | Description | |
---|---|---|---|
event | string | event name ( not case sensitive ) for the callback ( supports 'data' ) | |
callback | Data | function ( . . . parameter ) |
on throws
Type | Description |
---|---|
Error | if the event is not supported |
Error | if the callback is not 'function' |
open
async open (serialPortName, baudrate) => {Promise}
Opens the serial port
open Parameters
Name | Type | Description | |
---|---|---|---|
serialPortName | string | name of the serial port ( example "COM1" ) | |
baudrate | number | baud rate ( example 9600 ) |
open returns
Type | Description |
---|---|
Promise |
sendData
async sendData (byteArray) => {}
Sends data to the serial port
sendData Parameters
Name | Type | Description | |
---|---|---|---|
byteArray | Array.<byte> | serial data as byte stream |
sendData returns
Type | Description |
---|---|
| true , if sending was successful |