56 lines
1.2 KiB
Python
56 lines
1.2 KiB
Python
'''
|
|
Created on 05.12.2013
|
|
|
|
@author: Philipp Rauch
|
|
'''
|
|
from pymodbus.client.sync import ModbusTcpClient
|
|
from datetime import datetime
|
|
import struct
|
|
|
|
def setup(conf):
|
|
PACS = []
|
|
|
|
### INITIALIZE ###
|
|
PACS.append(ModbusTcpClient('10.2.6.5'))
|
|
PACS.append(ModbusTcpClient('10.2.6.6'))
|
|
PACS.append(ModbusTcpClient('10.2.6.7'))
|
|
PACS.append(ModbusTcpClient('10.2.6.8'))
|
|
|
|
### CONNECT ###
|
|
for PAC in PACS:
|
|
PAC.connect()
|
|
|
|
return PACS
|
|
|
|
|
|
def loop(PACS, item):
|
|
# PAC[0].write_coil(213, 100,2)
|
|
# result = PAC[0].read_coils(213,1,unit=2)
|
|
#
|
|
# ans = PAC[0].write_registers(213,(1,0),unit=2)
|
|
# print "Antwort: %s" % ans
|
|
|
|
res = PACS[1].read_holding_registers(797,2,unit=2)
|
|
print "Werte: %s" % _to_time(res.registers)
|
|
|
|
def _to_time_abs(reg):
|
|
correction = (6*60*60 - 6*60 - 9) #21231 / 5h 53m 51s
|
|
timestamp = _to_ulong(reg) + correction
|
|
date = datetime.fromtimestamp(timestamp)
|
|
return date
|
|
|
|
def _to_time_rel(reg):
|
|
return _to_ulong(reg)/3600.0
|
|
|
|
def _to_ulong(reg):
|
|
return (reg[0]*int(0xFFFF) + reg[1])
|
|
|
|
def _to_float(reg):
|
|
tmp = int(reg[0]) << 16 | int(reg[1])
|
|
s = struct.pack('=i', tmp)
|
|
return struct.unpack('=f', s)
|
|
|
|
def close(PACS):
|
|
### CLOSE ###
|
|
for PAC in PACS:
|
|
PAC.close()
|