私はPythonスクリプトをLinux上で周期的に実行し、pymodbusを使用してmodbus tcp経由で外部PLCに接続しています。 PLCのレジスタへの読み書きは、外部センサからデータをフェッチしている間はうまくいきます。バックグラウンドで実行されているPythonスクリプトからデータを取得する動的Webページ
このデータ(ライブ値)を表示するための単純なHTMLページを作成し、ユーザーがいくつかの設定ポイントを入力できるようにします。既存のpythonスクリプトをhtmlウェブサイトにどのように接続できますか?
Pythonは次のようになります。
from pymodbus.client.sync import ModbusTcpClient as ModbusClient
client = ModbusClient(host='192.168.0.1', port=502)
try:
while True:
temp1 = ReadSensor('sensor1') #function returns a temperature, works ok
temp2 = ReadSensor('sensor2')
setpoint = ?? #how do I fetch a given input field
#from html here?
#read holding registers 0 to 4
rr = client.read_holding_registers(0x00,4,unit=0x00)
print rr.registers #This values should appear on html
#send write command to modbus server
address = 11
client.write_registers(address, [temp1, temp2, setpoint])
except KeyboardInterrupt:
pass
client.close()
このスクリプトは、再起動時に開始され、殺されるまで実行されます。 htmlとpythonスクリプトは同じサーバー上で実行されます。
提案していただきありがとうございます。最初のテストはうまくいきました。とにかく、SQL DBを毎秒(modbus /ウェブサイトから来るデータを使って)ラズベリーパイにアップデートすることについて考えています。それが問題かもしれないと思いますか? –