-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
29 lines (24 loc) · 933 Bytes
/
main.py
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
from flask import Flask, jsonify
from flask_cors import CORS
from pymodbus.client import ModbusTcpClient
import os
app = Flask(__name__)
CORS(app)
@app.route('/regval', methods=['GET'])
def get_modbus_val():
modbus_host = os.getenv('MODBUS_HOST')
modbus_port = int(os.getenv('MODBUS_PORT'))
modbus_register = int(os.getenv('MODBUS_REGISTER'))
client = ModbusTcpClient(modbus_host, port=modbus_port)
connection = client.connect()
if connection:
result = client.read_holding_registers(modbus_register, 1, slave=1)
client.close()
if not result.isError():
return jsonify({'value': result.registers[0]})
else:
return jsonify({'error': 'Error reading register'}), 500
else:
return jsonify({'error': 'Unable to connect to the Modbus server'}), 500
if __name__ == '__main__':
app.run(host='0.0.0.0', port=80)