我已经绞尽脑汁好几天了,试图让 Modbus RTU RS 485 在我的 Beaglebone black 上工作,设置了 Cape comms2 并运行 Debian。
/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