ctucx.git: smartied

[nimlang] smarthome server

commit b6e16b841aaaa7088d089e78f73d74e4d7c9c20f
parent 822a7f347a07e47f904d5a99ed69787aadfaff9f
Author: Milan Pässler <me@pbb.lc>
Date: Thu, 15 Aug 2019 22:26:36 +0200

use holding registers instead of coils
2 files changed, 13 insertions(+), 7 deletions(-)
M
src/modbus.nim
|
16
+++++++++++-----
M
src/util.nim
|
4
++--
diff --git a/src/modbus.nim b/src/modbus.nim
@@ -11,8 +11,8 @@ proc modbus_connect*(mb: modbus): cint {.importc.}
 proc modbus_close*(mb: modbus): void {.importc.}
 proc modbus_free*(mb: modbus): void {.importc.}
 proc modbus_set_slave*(mb: modbus, ad: cint): void {.importc.}
-proc modbus_read_bits*(mb: modbus, ad: cint, nb: cint, dest: pointer): cint {.importc.}
-proc modbus_write_bit*(mb: modbus, ad: cint, status: cint): cint {.importc.}
+proc modbus_read_registers*(mb: modbus, ad: cint, nb: cint, dest: pointer): cint {.importc.}
+proc modbus_write_register*(mb: modbus, ad: cint, val: cint): cint {.importc.}
 proc modbus_read_input_registers*(mb: modbus, ad: cint, nb: cint, dest: pointer): cint {.importc.}
 proc modbus_get_float_dcba*(src: pointer): cfloat {.importc.}
 proc modbus_strerror*(src: cint): cstring {.importc.}

@@ -52,18 +52,24 @@ proc asyncReadFloat*(mb: modbus, ad: uint8, reg: uint8): Future[float32] {.async
       return (status, float32(res))
     )
 
-proc asyncWriteBit*(mb: modbus, ad: uint8, reg: uint8, val: bool): Future[void] {.async.} =
+proc asyncWriteBits*(mb: modbus, ad: uint8, reg: uint8, val: seq[bool]): Future[void] {.async.} =
+  var data = 0u16
+  for i, v in val.pairs:
+    data = data + uint8(v) shl i
+  echo val
+  echo data
   discard await mb.retry(proc (): (cint, bool) =
       mb.modbus_set_slave(cint(ad))
-      let status = mb.modbus_write_bit(cint(reg), cint(val))
+      let status = mb.modbus_write_register(cint(reg), cint(data))
       return (status, false)
     )
 
 proc asyncReadBits*(mb: modbus, ad: uint8, reg: uint8, nb: uint8): Future[seq[bool]] {.async.} =
   var data: array[256, bool]
+  let num_registers = uint8((nb - 1) * 8) + 1
   discard await mb.retry(proc (): (cint, bool) =
       mb.modbus_set_slave(cint(ad))
-      let status = mb.modbus_read_bits(cint(reg), cint(nb), data.addr)
+      let status = mb.modbus_read_registers(cint(reg), cint(num_registers), data.addr)
       return (status, false)
     )
   return @data[0..nb-1]
diff --git a/src/util.nim b/src/util.nim
@@ -41,7 +41,7 @@ proc handleRequest*(client: Client, req: string): Future[JsonNode] {.async.} =
     let config = server.config.devices[action.setRelayBoard]
 
     server.state[action.setRelayBoard].relays[action.setRelay] = action.setValue
-    await mb.asyncWriteBit(config.address, config.firstRegister + action.setRelay, server.state[action.setRelayBoard].relays[action.setRelay])
+    await mb.asyncWriteBits(config.address, config.firstRegister + action.setRelay, server.state[action.setRelayBoard].relays)
 
     broadcast($(%*server.state))
     return JsonNode()

@@ -49,7 +49,7 @@ proc handleRequest*(client: Client, req: string): Future[JsonNode] {.async.} =
     let config = server.config.devices[action.toggleRelayBoard]
 
     server.state[action.toggleRelayBoard].relays[action.toggleRelay] = not server.state[action.toggleRelayBoard].relays[action.toggleRelay]
-    await mb.asyncWriteBit(config.address, config.firstRegister + action.toggleRelay, server.state[action.toggleRelayBoard].relays[action.toggleRelay])
+    await mb.asyncWriteBits(config.address, config.firstRegister + action.toggleRelay, server.state[action.toggleRelayBoard].relays)
 
     broadcast($(%*server.state))
     return JsonNode()