質問編集履歴

1

コードを追加しました。

2020/10/20 04:49

投稿

akirabrian67
akirabrian67

スコア11

test CHANGED
File without changes
test CHANGED
@@ -25,3 +25,179 @@
25
25
 
26
26
 
27
27
  これであればコマンドプロンプト上には開始、終了が通常羅列されるのでしょうか?
28
+
29
+
30
+
31
+ ```python
32
+
33
+ from flask import Flask, render_template
34
+
35
+ from decimal import Decimal
36
+
37
+ import logging
38
+
39
+ import socket
40
+
41
+ import webbrowser
42
+
43
+ import math
44
+
45
+
46
+
47
+ while True:
48
+
49
+ print('開始')
50
+
51
+
52
+
53
+ logging.basicConfig(level=logging.DEBUG, format='%(threadName)s: %(message)s')
54
+
55
+ host_ip = '192.168.24.204' # PLCのIPアドレス
56
+
57
+ host_port = 8501 # 上位リンク通信のポート番号(デフォルト値)
58
+
59
+ logging.debug('start')
60
+
61
+ client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # socket.AF_INETでip4を使うことを指定。socket.SOCK_STREAMでTCPを使うことを指定。
62
+
63
+
64
+
65
+ try:
66
+
67
+ #クライアント接続
68
+
69
+ client.connect((host_ip,host_port)) # サーバーに接続(kv-7500にTCP接続/上位リンク通信)
70
+
71
+ except:
72
+
73
+ print("PLC接続NG")
74
+
75
+
76
+
77
+ ##########################以下から画面表示に関係するパラメータ##############################################
78
+
79
+
80
+
81
+ # ↓上位リンク通信のコマンド(データ読み出しコマンド、左から順に、読み出しコード、IOの番号、?。
82
+
83
+ # ↓○○○ = "RDS DM53000.D 2\r"
84
+
85
+ # ↓[.D]⇒±10進数32BIT表示・・・返り値は10桁の0埋め数値
86
+
87
+
88
+
89
+
90
+
91
+ Production = "RD DM1014.D\r" # DM1014 : 生産数
92
+
93
+ Cycle_Time = "RD DM1302.D\r" # DM1302 : サイクルタイム
94
+
95
+
96
+
97
+ # ↓上位リンク通信のデータコードがASCIIなのでエンコード 
98
+
99
+ # ↓ASCII:文字コード
100
+
101
+ # ↓エンコード:他の形式に変換すること
102
+
103
+
104
+
105
+ client.send(Production.encode("ascii")) # 生産数ををASCIIにエンコード
106
+
107
+ client.send(Cycle_Time.encode("ascii")) # サイクルタイムをASCIIにエンコード
108
+
109
+
110
+
111
+ print("send : " + Production)
112
+
113
+ print("send : " + Cycle_Time)
114
+
115
+
116
+
117
+ # ↓ 受信用バイト配列を定義しておく
118
+
119
+ # ↓ PLCからの返答がbyteデータなのでUTF-8にデコード
120
+
121
+ # ↓ UTF-8: 文字コード ASCIIよりデータサイズが小さい
122
+
123
+ # ↓ デコード:エンコードされたものをもとの形式に戻すこと
124
+
125
+
126
+
127
+ Production_number = client.recv(64)
128
+
129
+ Production_number = Production_number.decode("UTF-8")
130
+
131
+ Production_number = int(Production_number)
132
+
133
+
134
+
135
+ Cycle_time = client.recv(64)
136
+
137
+ Cycle_time = Cycle_time.decode("UTF-8")
138
+
139
+ Cycle_time = int(Cycle_time)
140
+
141
+ Cycle_time = Cycle_time * Decimal('0.1')
142
+
143
+
144
+
145
+ Goal = 86400 / Cycle_time # 目標数: 24時間 / サイクルタイム
146
+
147
+ Goal = math.floor(Goal)
148
+
149
+
150
+
151
+ Operation_Rate = ( 3600 / Cycle_time ) / Production_number
152
+
153
+ Operation_Rate = Operation_Rate * 100
154
+
155
+ Operation_Rate = math.floor(Operation_Rate)
156
+
157
+ # 進捗、稼働率 時間をどうするか検討必要
158
+
159
+
160
+
161
+ client.close()
162
+
163
+ logging.debug('end')
164
+
165
+
166
+
167
+ print("Received :" , Production_number)
168
+
169
+ print("Received :" , Cycle_time)
170
+
171
+ print("Received :" , Goal)
172
+
173
+ print(Operation_Rate, "%")
174
+
175
+
176
+
177
+ #######################################ここまでがソケット通信のコード#########################################################################
178
+
179
+
180
+
181
+ app = Flask(__name__) # アプリの設定
182
+
183
+
184
+
185
+ @app.route("/") # どのページで実行する関数か設定
186
+
187
+
188
+
189
+ def index():
190
+
191
+ return render_template("index1.html", production_number=Production_number, cycle_time=Cycle_time, goal=Goal, operation_rate=Operation_Rate) # Hello, World! を出力
192
+
193
+
194
+
195
+ print('終了')
196
+
197
+
198
+
199
+ if __name__ == "__main__": # 実行されたら
200
+
201
+ app.run(debug=False, host='localhost', port=9999) # debug=False でRestartingを無効にする、host="'0'0'0'0"ではなくローカルホストにすること
202
+
203
+ ```