teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

コードを追加しました。

2020/10/20 04:49

投稿

akirabrian67
akirabrian67

スコア11

title CHANGED
File without changes
body CHANGED
@@ -11,4 +11,92 @@
11
11
  print('終了')
12
12
  ```
13
13
 
14
- これであればコマンドプロンプト上には開始、終了が通常羅列されるのでしょうか?
14
+ これであればコマンドプロンプト上には開始、終了が通常羅列されるのでしょうか?
15
+
16
+ ```python
17
+ from flask import Flask, render_template
18
+ from decimal import Decimal
19
+ import logging
20
+ import socket
21
+ import webbrowser
22
+ import math
23
+
24
+ while True:
25
+ print('開始')
26
+
27
+ logging.basicConfig(level=logging.DEBUG, format='%(threadName)s: %(message)s')
28
+ host_ip = '192.168.24.204' # PLCのIPアドレス
29
+ host_port = 8501 # 上位リンク通信のポート番号(デフォルト値)
30
+ logging.debug('start')
31
+ client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # socket.AF_INETでip4を使うことを指定。socket.SOCK_STREAMでTCPを使うことを指定。
32
+
33
+ try:
34
+ #クライアント接続
35
+ client.connect((host_ip,host_port)) # サーバーに接続(kv-7500にTCP接続/上位リンク通信)
36
+ except:
37
+ print("PLC接続NG")
38
+
39
+ ##########################以下から画面表示に関係するパラメータ##############################################
40
+
41
+ # ↓上位リンク通信のコマンド(データ読み出しコマンド、左から順に、読み出しコード、IOの番号、?。
42
+ # ↓○○○ = "RDS DM53000.D 2\r"
43
+ # ↓[.D]⇒±10進数32BIT表示・・・返り値は10桁の0埋め数値
44
+
45
+
46
+ Production = "RD DM1014.D\r" # DM1014 : 生産数
47
+ Cycle_Time = "RD DM1302.D\r" # DM1302 : サイクルタイム
48
+
49
+ # ↓上位リンク通信のデータコードがASCIIなのでエンコード 
50
+ # ↓ASCII:文字コード
51
+ # ↓エンコード:他の形式に変換すること
52
+
53
+ client.send(Production.encode("ascii")) # 生産数ををASCIIにエンコード
54
+ client.send(Cycle_Time.encode("ascii")) # サイクルタイムをASCIIにエンコード
55
+
56
+ print("send : " + Production)
57
+ print("send : " + Cycle_Time)
58
+
59
+ # ↓ 受信用バイト配列を定義しておく
60
+ # ↓ PLCからの返答がbyteデータなのでUTF-8にデコード
61
+ # ↓ UTF-8: 文字コード ASCIIよりデータサイズが小さい
62
+ # ↓ デコード:エンコードされたものをもとの形式に戻すこと
63
+
64
+ Production_number = client.recv(64)
65
+ Production_number = Production_number.decode("UTF-8")
66
+ Production_number = int(Production_number)
67
+
68
+ Cycle_time = client.recv(64)
69
+ Cycle_time = Cycle_time.decode("UTF-8")
70
+ Cycle_time = int(Cycle_time)
71
+ Cycle_time = Cycle_time * Decimal('0.1')
72
+
73
+ Goal = 86400 / Cycle_time # 目標数: 24時間 / サイクルタイム
74
+ Goal = math.floor(Goal)
75
+
76
+ Operation_Rate = ( 3600 / Cycle_time ) / Production_number
77
+ Operation_Rate = Operation_Rate * 100
78
+ Operation_Rate = math.floor(Operation_Rate)
79
+ # 進捗、稼働率 時間をどうするか検討必要
80
+
81
+ client.close()
82
+ logging.debug('end')
83
+
84
+ print("Received :" , Production_number)
85
+ print("Received :" , Cycle_time)
86
+ print("Received :" , Goal)
87
+ print(Operation_Rate, "%")
88
+
89
+ #######################################ここまでがソケット通信のコード#########################################################################
90
+
91
+ app = Flask(__name__) # アプリの設定
92
+
93
+ @app.route("/") # どのページで実行する関数か設定
94
+
95
+ def index():
96
+ return render_template("index1.html", production_number=Production_number, cycle_time=Cycle_time, goal=Goal, operation_rate=Operation_Rate) # Hello, World! を出力
97
+
98
+ print('終了')
99
+
100
+ if __name__ == "__main__": # 実行されたら
101
+ app.run(debug=False, host='localhost', port=9999) # debug=False でRestartingを無効にする、host="'0'0'0'0"ではなくローカルホストにすること
102
+ ```