Created
September 21, 2017 22:42
-
-
Save Felhamed/a55b5fda43fe3f209efd91c4273cc1ec to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| # -*- coding: utf-8 -*- | |
| # read_register | |
| # read 10 registers and print result on stdout | |
| # you can use the tiny modbus server "mbserverd" to test this code | |
| # mbserverd is here: https://github.com/sourceperl/mbserverd | |
| # the command line modbus client mbtget can also be useful | |
| # mbtget is here: https://github.com/sourceperl/mbtget | |
| from pyModbusTCP.client import ModbusClient | |
| import time | |
| SERVER_HOST = "localhost" | |
| SERVER_PORT = 502 | |
| c = ModbusClient() | |
| # uncomment this line to see debug message | |
| #c.debug(True) | |
| # define modbus server host, port | |
| c.host(SERVER_HOST) | |
| c.port(SERVER_PORT) | |
| while True: | |
| # open or reconnect TCP to server | |
| if not c.is_open(): | |
| if not c.open(): | |
| print("unable to connect to "+SERVER_HOST+":"+str(SERVER_PORT)) | |
| # if open() is ok, read register (modbus function 0x03) | |
| if c.is_open(): | |
| # read 10 registers at address 0, store result in regs list | |
| regs = c.read_holding_registers(0, 10) | |
| # if success display registers | |
| if regs: | |
| print("reg ad #0 to 9: "+str(regs)) | |
| # sleep 2s before next polling | |
| time.sleep(2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment