uci-i2c-bus/examples/tcp-relay.mjs

53 lines
1.5 KiB
JavaScript

/*
* A tcp customer/client to talk with the i2c bus and scan the bus for devices
*
*/
const HOST = 'sbc'
import Base from '../../uci-base/src/base'
// import Base from '@uci/base'
const delay = time => new Promise(res=>setTimeout(()=>res(),time))
;
(async () => {
let relays = new Base({id:'tcp-i2c-client', sockets:'tc#c>t', tc:{port: 1776, host:HOST}})
relays.reply = function (packet) {
// console.log(packet.bus)
console.log(`response from relays for ${packet.cmd_sent}:`,packet.args, `was ${packet.response}`)
}
await relays.init()
console.log('=============sending============')
let packet = {cmd:'read', args:{address:39 ,cmd: 0} }
console.dir(packet)
await relays.send(packet)
packet = {cmd:'write', args:{address:39,cmd: 0, byte:0} }
console.dir(packet)
await relays.send(packet)
packet = {cmd:'read', args:{address:39 ,cmd:0} }
console.dir(packet)
await relays.send(packet)
packet = {cmd:'write', args:{address:39,cmd: 9, byte:255} }
console.dir(packet)
await relays.send(packet)
packet = {cmd:'read', args:{address:39 ,cmd:9} }
console.dir(packet)
await relays.send(packet)
await delay(1000)
packet = {cmd:'write', args:{address:39,cmd: 9, byte:0} }
console.dir(packet)
await relays.send(packet)
packet = {cmd:'read', args:{address:39 ,cmd:9} }
console.dir(packet)
await relays.send(packet)
await delay(1000)
process.kill(process.pid, 'SIGTERM')
})().catch(err => {
console.error('FATAL: UNABLE TO START SYSTEM!\n',err)
})