-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.py
More file actions
148 lines (118 loc) · 4.2 KB
/
app.py
File metadata and controls
148 lines (118 loc) · 4.2 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!flask/bin/python
# -*- coding: utf-8 -*-
import os
from flask import Flask, render_template, flash, request
from wtforms import Form, TextField, TextAreaField, validators, StringField, SubmitField
from pymodbus.client.sync import ModbusTcpClient
# App config.
DEBUG = True
app = Flask(__name__)
app.config.from_object(__name__)
app.config['SECRET_KEY'] = os.urandom(24)
class ReusableForm(Form):
ip = TextField('IP:')
port = TextField('Port:')
address = TextField('Start address:')
value = TextField('Value:')
unitId = TextField('unitId:')
@app.route("/")
def index():
return render_template('index.html')
@app.route("/read-holding-registers", methods=['GET', 'POST'])
def read_holding_regs():
form = ReusableForm(request.form)
print form.errors
if request.method == 'POST':
ip=request.form['ip']
port=request.form['port']
address=request.form['address']
value=request.form['value']
unitId=request.form['unitId']
print ip, " ", port, " ", address
host = ip
port = int(port)
client = ModbusTcpClient(host, port)
client.connect()
rr = client.read_holding_registers(int(address),int(value),unit= int(unitId))
assert(rr.function_code < 0x80) # test that we are not an error
print (rr)
print (str(rr.registers))
if form.validate():
flash(str(rr.registers))
else:
flash("Error")
return render_template('read-hold-regs.html', form=form)
@app.route("/write-holding-register", methods=['GET', 'POST'])
def write_regs():
form = ReusableForm(request.form)
print form.errors
if request.method == 'POST':
ip=request.form['ip']
port=request.form['port']
address=request.form['address']
value=request.form['value']
unitId=request.form['unitId']
print ip, " ", port, " ", address
host = ip
port = int(port)
client = ModbusTcpClient(host, port)
client.connect()
rr = client.write_register(int(address),int(value),unit= int(unitId))
assert(rr.function_code < 0x80) # test that we are not an error
#print (rr)
#print (rr.registers)
if form.validate():
# Save the comment here.
flash("Success!")
#flash('Thanks for registration ' + ip)
else:
flash('Error')
return render_template('write-register.html', form=form)
@app.route("/read-coils", methods=['GET', 'POST'])
def read_coils():
form = ReusableForm(request.form)
print form.errors
if request.method == 'POST':
ip=request.form['ip']
port=request.form['port']
address=request.form['address']
value=request.form['value']
unitId=request.form['unitId']
print ip, " ", port, " ", address
host = ip
port = int(port)
client = ModbusTcpClient(host, port)
client.connect()
rr = client.read_coils(int(address),int(value),unit= int(unitId))
assert(rr.function_code < 0x80) # test that we are not an error
print (rr)
print (rr.bits)
if form.validate():
flash(str(rr.bits))
else:
flash('Error')
return render_template('read-coils.html', form=form)
@app.route("/write-coils", methods=['GET', 'POST'])
def write_coils():
form = ReusableForm(request.form)
print form.errors
if request.method == 'POST':
ip=request.form['ip']
port=request.form['port']
address=request.form['address']
value=request.form['value']
unitId=request.form['unitId']
print ip, " ", port, " ", address
host = ip
port = int(port)
client = ModbusTcpClient(host, port)
client.connect()
rr = client.write_coils(int(address),int(value),unit= int(unitId))
assert(rr.function_code < 0x80) # test that we are not an error
if form.validate():
flash("Success")
else:
flash('Error')
return render_template('write-coils.html', form=form)
if __name__ == "__main__":
app.run()