私はCape comms2を設定し、Debianを実行しながらModbus RTU RS 485をBeaglebone Blackで動作させようとして数日間頭を握りました。
/dev/ttyS0
私は次のように9600ボード、8ビットデータ、1ビットストップ、パリティなしでNodeJSソフトウェアに接続します。
let SerialPort = require("serialport");
let serialport = new SerialPort(this.device, { autoOpen: false, baudRate: 9600, stopbits: 1, databits: 8, parity: 'none' }, false);
this.log('Opening serial connection… (client = ModbusRTU())')
const client = new ModbusRTU(serialport)
this.success('client is', client)
this.log("Port Open Status: " + client.isOpen);
client.open()
// set a timout for requests default is null (no timeout)
client.setTimeout(2e3)
setInterval(() => {
this.log("Port Open Status: " + client.isOpen);
if (!client.isOpen) return
run()
},10e3)
var i=0
const run = () => {
this.success(`Connected!`)
this.success('--> Running…')
if (i&1) read()
else write()
i++
this.log('<-- Leaving run, but wait for the response in the future call back…')
}
/**
* Read the RS485 packet: start adress 0x0C count 1 register
*/
const read = () => {
// read the 2 registers starting at address 5
// on device number 1.
this.log('About to read, setting the ID to 0xD2…')
client.setID(0xD2)
this.log('Now reading 1 register from start address 0x2A, wait for the response…')
client
//.readHoldingRegisters()
.readHoldingRegisters(0x2A, 1)
.then(args => this.log('Received SoC:', ...args))
.catch(err => this.error('Received eror:', err))
.finally(() => this.log('Received: finally'))
}
/**
* Write to the RS485 register
*/
const write = () => {
// write the values 0, 0xffff to registers starting at address 5
// on device number 1.
//this.log('Writing: 0xD2, [0x03, 0x00, 0x0C, 0x00, 0x01, 0x57, 0xAA,]')
this.log('Writing: FC3 0xD2,0x2A,1')
client.setID(0xD2)
if (true) {
client
.writeFC3(0x06,0x2A, 1, (err, data) => {
if (err) {
this.error('writeFC3:', err)
} else {
this.success('writeFC3:', data)
}
})
}
}
私が得たログは次のとおりです。
Pepsr v2.2.236 9:56:14 AM