-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodbusTest.py
More file actions
executable file
·102 lines (70 loc) · 2.19 KB
/
modbusTest.py
File metadata and controls
executable file
·102 lines (70 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
##
##from pymodbus.client.sync import ModbusSerialClient as ModbusClient
##
##client = ModbusClient(method='rtu', baudrate=9600, port='/dev/ttyS0')
###client.write_coil(1, True)
##result = client.read_coils(20,4, unit=34)
##print result.bits
##client.close()
#:00010022004C91
import datetime
#Function definitions
def writeCoils(address, value):
client.write_coil(address, value, unit=34)
def readCoil(address):
result = client.read_coils(address, unit=34)
return result.getBit(0)
def readReg(address):
result = client.read_holding_registers(address, unit=34)
return result.getRegister(0)
def writeReg(address, value):
client.write_register(address, value, unit=34)
def readCoilList(listOfRegs):
result = []
for reg in listOfRegs:
result.append(readCoil(reg))
return result
def writeCoilList(listOfRegs, listOfValues):
while len(listOfRegs) > 0:
reg = listOfRegs.pop()
val = listOfValues.pop()
writeCoils(reg, val)
def readRegList(listOfRegs):
result = []
for reg in listOfRegs:
result.append(readReg(reg))
return result
def writeRegList(listOfRegs, listOfValues):
while len(listOfRegs) > 0:
reg = listOfRegs.pop()
val = listOfValues.pop()
writeReg(reg, val)
#Setup serial interface to the RTU.
from pymodbus.client.sync import ModbusSerialClient as ModbusClient
client = ModbusClient(method='rtu', baudrate=9600, port='/dev/pts/19')
#Want to:
# 1. Write arbitrary set value commands, maybe define a struct or array or something
# to set it up, with an arbitrary poll time
# 2. Poll certain tags on a certain duty cycle, printing output to STDOUT and a log file.
boolean = 1
listOfCoils = [5, 26, 32, 42, 54, 60, 61, 63, 67, 71, 75, 80]
listOfWR = [66, 75, 98, 172]
listOfVals = [1, 102, 1100, 800]
file = open("output.txt", "a")
print "CTRL + C Exits!"
while True:
#try:
# Tags to write to:
# FLOW_SP
# CTRL_MODE
# ???
file.write(datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d %H:%M:%S') + "\n")
file.write("Coils\n")
file.write(str(readCoilList(listOfCoils)) + "\n")
file.write("Regs\n")
file.write(str(readRegList(listOfRegs)) + "\n")
#Do MODBUS writes
writeRegList(listOfWR, listOfVals)
#except:
# break
client.close()