Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. #!/usr/bin/env python
  2. """
  3. Pymodbus Synchronous Client Examples
  4. --------------------------------------------------------------------------
  5.  
  6. The following is an example of how to use the synchronous modbus client
  7. implementation from pymodbus.
  8.  
  9. It should be noted that the client can also be used with
  10. the guard construct that is available in python 2.5 and up::
  11.  
  12. with ModbusClient('127.0.0.1') as client:
  13. result = client.read_coils(1,10)
  14. print result
  15. """
  16. # --------------------------------------------------------------------------- #
  17. # import the various server implementations
  18. # --------------------------------------------------------------------------- #
  19. # from pymodbus.client.sync import ModbusTcpClient as ModbusClient
  20. # from pymodbus.client.sync import ModbusUdpClient as ModbusClient
  21. from pymodbus.client.sync import ModbusSerialClient as ModbusClient
  22.  
  23. # --------------------------------------------------------------------------- #
  24. # configure the client logging
  25. # --------------------------------------------------------------------------- #
  26. import logging
  27. FORMAT = ('%(asctime)-15s %(threadName)-15s '
  28. '%(levelname)-8s %(module)-15s:%(lineno)-8s %(message)s')
  29. logging.basicConfig(format=FORMAT)
  30. log = logging.getLogger()
  31. log.setLevel(logging.DEBUG)
  32.  
  33. UNIT = 0x1
  34.  
  35.  
  36. def run_sync_client():
  37. client = ModbusClient(method='rtu', port='/dev/cu.wchusbserial14310', timeout=1,
  38. baudrate=9600, stopbits=2)
  39. client.connect()
  40.  
  41. log.debug("Read registeres")
  42. rr = client.read_holding_registers(0, 8, unit=UNIT)
  43. log.info(rr.registers[0]) # test the expected value
  44.  
  45. # ----------------------------------------------------------------------- #
  46. # close the client
  47. # ----------------------------------------------------------------------- #
  48. client.close()
  49.  
  50.  
  51. if __name__ == "__main__":
  52. run_sync_client()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement