I wrote the following code to trouble shouting the RS485 communication aspect of my firmware.
I use either a Beaglebone black or AI equipped with the cape COMMS 2 (which as a built-in A/B RS485 interface) as the host for this nodejs test code.
[UPDATE A] The cape COMMS description is right there : https://github.com/beagleboard/capes/tree/master/beaglebone/Comms
[UPDATE B]
As with any intermittent problem, have you been able to discern anything you can do that makes comms with the smart battery more, or less, likely to succeed?
No. What I can say is that if I simultaneously plug another (different) software on the bus that also ask for the frames, my code get answers properly too.
[UPDATE C]
Has comms to the battery ever been reliable (different code, different hardware etc.)?
Yes. I have another firmware that runs on a custom PCB I designed (against the same battery hardware) and I always have the communication up and running, with no problem.
Unfortunately, I don't received communication from the slave device (a smart LIFP battery).
The logs are:
$ npm run testRs485
> [email protected] testRs485 /home/debian/Desktop/devel/iot
> node testRs485.js
Opening serial port…
Terminating test
Successfully open
In port.open(): true
About to send message #1: <Buffer a5 40 90 08 00 00 00 00 00 00 00 00 7d>
Send returned true
Sent message successfuly
About to send message #2: <Buffer a5 40 90 08 00 00 00 00 00 00 00 00 7d>
Send returned true
==>etc.
But sometime, this very same code did worked. But I can't pinpoint what trigger on or off the communication.
I'd like to have advice or clue about how to solve the issue.
setInterval(function() {
console.log("timer that keeps nodejs processing running");
}, 1000 * 60);
var SerialPort = require('serialport')
var port = new SerialPort('/dev/ttyS4', {
autoOpen: false,
baudRate: 9600,
dataBits: 8,
stopBits: 1,
parity: 'none'
}, false)
function open () {
if (port.isOpen) return;
console.log('Opening serial port…')
port.open(err => {
if (!err) return
console.log('Port is not open:', err)
// next attempt to open after 10s
setTimeout(open, 10000)
});
}
// == On open…
port.on('open', () => {
console.log('Successfully open')
console.log("In port.open():", port.isOpen);
const messagetoSend = new Buffer.from([0xa5,0x40,0x90,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7d])//pack('001', '00', '740', '=?')
let counter = 1;
setInterval(function() {
console.log(`About to send message #${counter++}:`, messagetoSend)
const sent = port.write(messagetoSend, err => {
if (err) {
return console.error('Send error:',err)
}
console.log('Sent message successfuly')
if (false) {
console.log('Draining serial')
port.drain(() => console.log('Drain done'))
}
})
console.log('Send returned', sent)
}, 15000)
})
port.on('data', data => {
console.log('Received data: ' + data);
})
port.on('close', () => {
console.log('Serial port closed')
console.log('Reopening')
open()
})
port.on('error', err => {
console.error('Error:', err);
})
open()
console.log("Terminating test")
```