https://github.com/ljean/modbus-tk/
install pip…
sudo apt-get install python-pip
install...
download modbus_tk-x.x.x.tar.gz
tar zxvf modbus_tk-x.x.x.tar.gz
python setup.py install
make sure your port is open...
sudo iptables -A INPUT -p tcp --dport 502 -j ACCEPT # for slaver
sudo iptables -A OUTPUT -p tcp --dport 502 -j ACCEPT # for master
run example as root
a simple example...
import sys import logging import threading import modbus_tk import modbus_tk.defines as cst import modbus_tk.modbus as modbus import modbus_tk.modbus_tcp as modbus_tcp import time logger = modbus_tk.utils.create_logger(name="console", record_format="%(message)s") # CREATE server server = modbus_tcp.TcpServer() #DEFAULT PORT=502 slaver = server.add_slave(1) #ID=1 def setup(): slaver.add_block("coil", cst.COILS, 0, 16) slaver.set_values("coil", 0, 16*[0])
def loop(): logger.info("running...") # START server.start() while True: values = slaver.get_values("coil", 0, 8) #print values[0] str = '' for i in range(0, 8): if values[i] == 1: str = str + '1' else: str = str + '0' print str # DELAY time.sleep(1) def destory(): logger.info("destory") # STOP server.stop() if __name__ == "__main__": setup() try: loop() except KeyboardInterrupt: destory()
沒有留言:
張貼留言