1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- #!/usr/bin/env python
- # scripts/examples/simple_tcp_client.py
- import fcntl
- import sys
- import socket
- import struct
- from serial import Serial
- from serial.serialutil import SerialException
- from umodbus.client.serial import rtu
- import json
-
- s = Serial('/dev/ttyUSB0',19200)
- try:
- s.open()
- except SerialException:
- print('already open')
-
- BOOLEAN=1000
- FLOAT=500
- INT=1250
- try:
- res = {'A':{},'D':{},'I':{}}
-
- # digital - boolean
- req_adu = rtu.read_coils(
- slave_id=1,
- starting_address=1,
- quantity=1)
-
- resp = rtu.send_message(req_adu, s)
-
- address = -1
- try:
- for address in range(0, BOOLEAN, 1):
- res['D'][address+1] = (resp[address] > 0)
- except KeyError as e:
- print(e)
- res['D'][address] = False
- except IndexError:
- print(address)
-
- # analog - float
- for address in range(0, FLOAT, 125):
- req_adu = rtu.read_holding_registers(
- slave_id=1,
- starting_address=1+address,
- quantity=125)
-
- resp = rtu.send_message(req_adu, s)
-
- for n in range(125):
- try:
- res['A'][address + n] = int(resp[n]) / 10
- except Exception as e:
- print(e)
- res['A'][address + n] = 0.
- #res['A'][ str(address + var) ] = int(resp[var]) / 10.
-
- # integer - int
- for address in range(1, INT, 125):
- req_adu = rtu.read_coils(
- slave_id=1,
- starting_address=address+5002,
- quantity=125)
-
- resp = rtu.send_message(req_adu, s)
-
- for n in range(125):
- try:
- res['I'][address + n] = int(resp[n])
- except KeyError as e:
- print(e)
- res['I'][address + n] = 0
- except KeyboardInterrupt:
- print("Quitting")
- pass
-
- s.close()
-
- sys.exit(0)
|