折腾了很久,对于我这个仅仅是看过一遍a byte of python的最最新手来说,好不容易的,终于能够搞到server版与client版在本机上测试通讯成功了,记录一下。
先来看看server端的:

c = modlib.ModbusServerContext(
discrete_inputs_address=1,
discrete_inputs_count=0)
s = modlib.ModbusTCPServer(context=c)
try:
s.serve_forever()
except KeyboardInterrupt:
print "Ctrl+C pressed - exiting..."
s.server_close()

运行后的結果:
INFO:modbus.library:Started new thread to serve client at ('127.0.0.1', 33282)
INFO:modbus.library:Remote party has closed connection.
INFO:modbus.library:Exiting thread that served client at ('127.0.0.1', 33282)

这是client端的:

cn = TCPMasterConnection("127.0.0.1")
# Read Discrete Inputs
req = ReadDiscreteInputsRequest(address=1, count=0)
tr = cn.createTransaction(req)
res = tr.execute()

运行后的結果:
DEBUG:modbus.library:Running transaction 0
INFO:modbus.library:Connecting to: (127.0.0.1, 502)
DEBUG:modbus.library:Exception response received 3.

这是一个里程碑,但是要真正写一个可以用的,还需要很长一段时间。

PS:shellex提供的wp-syntax非常好用,代码高亮很好看的。