#!/usr/bin/env python3 # BFi53ER0 Battery-Free Bluetooth Gateway -- example: a first Modbus TCP read. # # python3 modbus_identify.py bfi53er0-xxxx.local # the name on the label # python3 modbus_identify.py 192.168.1.20 # # Turn Modbus TCP on first: settings page, Forwarding -> Modbus TCP. The # gateway itself answers at unit ID 255. This reads its device # identification (function 43/14) and the register map and firmware versions # (function 04, input registers 0-3). The complete register map -- every # switch and sensor decoded, the terminals, RS-485 -- is in the BFi53ER0-BF # System Integration Guide, provided on request (PDF and Markdown). # # Python 3, standard library only; any Modbus TCP client library works too. # # (c) 2026 BANFi Semiconductor Co., Ltd. and FL Semiconductor LLC. You may # use, copy and modify this example to integrate with BFi53ER0 products. import socket import struct import sys if len(sys.argv) < 2: sys.exit("usage: modbus_identify.py GATEWAY") UNIT = 255 def recv_exact(s, n): """n bytes, or an error if the gateway closes the connection first.""" b = b"" while len(b) < n: chunk = s.recv(n - len(b)) if not chunk: raise SystemExit("the gateway closed the connection") b += chunk return b def request(s, tid, pdu): """Send one Modbus TCP request; return the answer's PDU.""" s.sendall(struct.pack(">HHHB", tid, 0, len(pdu) + 1, UNIT) + pdu) head = recv_exact(s, 7) n = struct.unpack(">H", head[4:6])[0] - 1 body = recv_exact(s, n) if body[0] & 0x80: raise SystemExit("exception %d from the gateway" % body[1]) return body with socket.create_connection((sys.argv[1], 502), timeout=5) as s: a = request(s, 1, bytes([43, 14, 1, 0])) # device identification, basic objs, i = {}, 7 for _ in range(a[6]): objs[a[i]] = a[i + 2:i + 2 + a[i + 1]].decode() i += 2 + a[i + 1] print("vendor %s\nproduct %s\nversion %s" % (objs.get(0), objs.get(1), objs.get(2))) a = request(s, 2, struct.pack(">BHH", 4, 0, 4)) # input registers 0-3 v = struct.unpack(">4H", a[2:10]) print("register map version %d, firmware %d.%d.%d" % v)