Last active
July 29, 2019 03:28
-
-
Save dews/5cb5ef0819c5ca3647d064f3c75e60a1 to your computer and use it in GitHub Desktop.
modbus_rtu #modbus
This file contains 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 | |
""" | |
Pymodbus Synchronous Client Examples | |
-------------------------------------------------------------------------- | |
The following is an example of how to use the synchronous modbus client | |
implementation from pymodbus. | |
It should be noted that the client can also be used with | |
the guard construct that is available in python 2.5 and up:: | |
with ModbusClient('127.0.0.1') as client: | |
result = client.read_coils(1,10) | |
print result | |
""" | |
# --------------------------------------------------------------------------- # | |
# import the various server implementations | |
# --------------------------------------------------------------------------- # | |
# from pymodbus.client.sync import ModbusTcpClient as ModbusClient | |
# from pymodbus.client.sync import ModbusUdpClient as ModbusClient | |
from pymodbus.client.sync import ModbusSerialClient as ModbusClient | |
# --------------------------------------------------------------------------- # | |
# configure the client logging | |
# --------------------------------------------------------------------------- # | |
import logging | |
FORMAT = ('%(asctime)-15s %(threadName)-15s ' | |
'%(levelname)-8s %(module)-15s:%(lineno)-8s %(message)s') | |
logging.basicConfig(format=FORMAT) | |
log = logging.getLogger() | |
log.setLevel(logging.DEBUG) | |
UNIT = 0x1 | |
# def run_sync_client(): | |
# client = ModbusClient('127.0.0.1', port='5020') | |
# client.connect() | |
# log.debug("Read registeres") | |
# rr = client.read_holding_registers(0, 3, unit=UNIT) | |
# log.info(rr.registers) # test the expected value | |
# # ----------------------------------------------------------------------- # | |
# # close the client | |
# # ----------------------------------------------------------------------- # | |
# client.close() | |
def run_sync_client_rtu(): | |
client = ModbusClient(method='rtu', port='/dev/cu.wchusbserial14310', timeout=1, | |
baudrate=9600, stopbits=2) | |
client.connect() | |
log.debug("Read registeres") | |
rr = client.read_holding_registers(0, 8, unit=UNIT) | |
log.info(rr.registers) # test the expected value | |
# ----------------------------------------------------------------------- # | |
# close the client | |
# ----------------------------------------------------------------------- # | |
client.close() | |
if __name__ == "__main__": | |
run_sync_client_rtu() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment