generated from zhangwei/Train_Identify
同PLC的读写改为使用同一个链接
This commit is contained in:
parent
5033872573
commit
67f829258c
|
@ -9,7 +9,27 @@ a = Analysis(
|
|||
pathex=[],
|
||||
binaries=[],
|
||||
datas=[],
|
||||
hiddenimports=[],
|
||||
hiddenimports=[
|
||||
'engineio.async_drivers.eventlet',
|
||||
'eventlet.hubs.epolls',
|
||||
'eventlet.hubs.kqueue',
|
||||
'eventlet.hubs.selects',
|
||||
'dns',
|
||||
'dns.dnssec',
|
||||
'dns.e164',
|
||||
'dns.hash',
|
||||
'dns.namedict',
|
||||
'dns.tsigkeyring',
|
||||
'dns.update',
|
||||
'dns.version',
|
||||
'dns.zone',
|
||||
'dns.asyncbackend',
|
||||
'dns.asyncquery',
|
||||
'dns.asyncresolver',
|
||||
'dns.versioned',
|
||||
'socketserver',
|
||||
'http.server'
|
||||
],
|
||||
hookspath=[],
|
||||
hooksconfig={},
|
||||
runtime_hooks=[],
|
||||
|
|
|
@ -2,7 +2,8 @@ import time
|
|||
|
||||
from service.interfaceToWeb import interfaceToWeb
|
||||
from service.TagsList import TagsList, plc_info
|
||||
from utils.Tool import logger, config, PLC_Tool
|
||||
from utils.Tool import logger, config, PLC_Tool, g_plc
|
||||
|
||||
|
||||
plcInfo = plc_info()
|
||||
|
||||
|
@ -20,9 +21,9 @@ def writePLC(ip: str, slot: int = 0, tags=None):
|
|||
if len(tags) < 1:
|
||||
return False, "参数tags未写入需要修改的标签点位"
|
||||
|
||||
plc = PLC_Tool(ip, slot)
|
||||
# plc = PLC_Tool(ip, slot)
|
||||
|
||||
plc.batch_write_tag(tags)
|
||||
g_plc.batch_write_tag(tags)
|
||||
|
||||
return True
|
||||
|
||||
|
@ -258,22 +259,29 @@ def interval_read_PLC_up_web():
|
|||
循环读取PLC数据 根据条件上传至WEB
|
||||
:return:
|
||||
"""
|
||||
plc = PLC_Tool(config.ip_address, config.slot)
|
||||
global g_plc
|
||||
if g_plc is None:
|
||||
g_plc = PLC_Tool(config.ip_address, config.slot)
|
||||
interface = interfaceToWeb()
|
||||
login_result, token_type, access_token = interface.loginWeb(config.login_web_url, config.username,
|
||||
|
||||
login_result = ""
|
||||
token_type = ""
|
||||
access_token = ""
|
||||
if config.upload_type == 1:
|
||||
login_result, token_type, access_token = interface.loginWeb(config.login_web_url, config.username,
|
||||
config.password, config.authorization)
|
||||
|
||||
while True:
|
||||
up_result = ""
|
||||
start_time = time.time()
|
||||
load_result, flag_changeds = readPLC(plc)
|
||||
load_result, flag_changeds = readPLC(g_plc)
|
||||
end_time = time.time()
|
||||
# print(end_time, "运行时长:%3.f \n" % (end_time - start_time))
|
||||
if not load_result:
|
||||
time.sleep(30)
|
||||
time.sleep(4)
|
||||
continue
|
||||
|
||||
if login_result is not "":
|
||||
if config.upload_type == 1 and login_result is not "":
|
||||
logger.warn(login_result)
|
||||
return
|
||||
|
||||
|
@ -363,7 +371,7 @@ def interval_read_PLC_up_web():
|
|||
(track+1), loadingDict["loading_carriage_order"],
|
||||
loadingDict.get("gross_weight_refresh", 0)))
|
||||
|
||||
plc = PLC_Tool(config.ip_address, config.slot)
|
||||
# plc = PLC_Tool(config.ip_address, config.slot)
|
||||
|
||||
grossrefresh_Tag = ""
|
||||
for grossrefresh_l in TagsList().getLoading_Tags()[track]:
|
||||
|
@ -371,7 +379,7 @@ def interval_read_PLC_up_web():
|
|||
grossrefresh_Tag = grossrefresh_l[1]
|
||||
if grossrefresh_Tag == "":
|
||||
logger.error("股道%d- 配煤重读点位读取失败 !!!", (track + 1))
|
||||
success, msg = plc.write_tag(grossrefresh_Tag, 0)
|
||||
success, msg = g_plc.write_tag(grossrefresh_Tag, 0)
|
||||
|
||||
if not success:
|
||||
logger.error("股道%d- 配煤重读归零失败 !!!", (track+1))
|
||||
|
@ -458,18 +466,21 @@ def report_open(pound_no: str, total_number_carriage: int, pre_load: list):
|
|||
elif tag_list[0] == "total_number_carriage":
|
||||
total_number_carriage_tag = tag_list[1]
|
||||
|
||||
plc = PLC_Tool(config.ip_address, config.slot)
|
||||
global g_plc
|
||||
if g_plc is None:
|
||||
g_plc = PLC_Tool(config.ip_address, config.slot)
|
||||
if "report_open_type" in plcInfo.loadingDict_List[pound_no-1].keys() and plcInfo.loadingDict_List[pound_no-1]["report_open_type"] == 1:
|
||||
over_write = True
|
||||
old_total_number_carriage = plcInfo.loadingDict_List[pound_no-1]["total_number_carriage"]
|
||||
|
||||
success, msg, tags_value = plc.get_array(tagsList.getPreLoadWeight_Tags()[pound_no-1][1], tagsList.getPreLoadWeight_Tags()[pound_no-1][3])
|
||||
logger.info(tagsList.getPreLoadWeight_Tags()[pound_no-1][3])
|
||||
success, msg, tags_value = g_plc.get_array(tagsList.getPreLoadWeight_Tags()[pound_no-1][1], int(tagsList.getPreLoadWeight_Tags()[pound_no-1][3]))
|
||||
|
||||
if success:
|
||||
old_pre_load = tags_value["value"]
|
||||
|
||||
success, msg, results = \
|
||||
plc.batch_write_tag([(report_open_type_tag, 1),
|
||||
g_plc.batch_write_tag([(report_open_type_tag, 1),
|
||||
(total_number_carriage_tag, total_number_carriage)])
|
||||
|
||||
if not success:
|
||||
|
@ -484,7 +495,7 @@ def report_open(pound_no: str, total_number_carriage: int, pre_load: list):
|
|||
pre_load.insert(0, 0.0)
|
||||
logger.info("股道%d- 总车节数:%d 上传预装量:%s", pound_no, total_number_carriage, str(pre_load))
|
||||
|
||||
success, msg = plc.write_array("AI.load", pre_load)
|
||||
success, msg = g_plc.write_array("AI.load", pre_load)
|
||||
if not success:
|
||||
return 412, msg, {}
|
||||
|
||||
|
@ -548,8 +559,10 @@ def report_close(pound_no: str, total_number_carriage: int):
|
|||
if len(pre_load_tags_list) < (pound_no - 1):
|
||||
return 412, "股道“预装量”的点位配置未读到,请检查配置文件", {}
|
||||
|
||||
plc = PLC_Tool(config.ip_address, config.slot)
|
||||
success, msg, results = plc.batch_write_tag([(open_report_tags_list[pound_no - 1], 0),
|
||||
global g_plc
|
||||
if g_plc is None:
|
||||
g_plc= PLC_Tool(config.ip_address, config.slot)
|
||||
success, msg, results = g_plc.batch_write_tag([(open_report_tags_list[pound_no - 1], 0),
|
||||
(total_number_carriage_tags_list[pound_no - 1], 0)])
|
||||
if not success:
|
||||
return 412, msg, {}
|
||||
|
@ -560,7 +573,7 @@ def report_close(pound_no: str, total_number_carriage: int):
|
|||
# return responseMsg(200, msg, results)
|
||||
|
||||
pre_load = [0] * config.carriage_num_max
|
||||
success, msg = plc.write_array(pre_load_tags_list[pound_no - 1], pre_load)
|
||||
success, msg = g_plc.write_array(pre_load_tags_list[pound_no - 1], pre_load)
|
||||
if not success:
|
||||
return 412, msg, {}
|
||||
|
||||
|
|
|
@ -13,14 +13,15 @@ class PLC_Tool:
|
|||
self.plcSlot = plcSlot
|
||||
self.timeOut = timeOut
|
||||
self.comm = None
|
||||
logger.info("准备连接PLC,IP:%s, slot: %d, timeout: %d", self.plcIp, self.plcSlot, self.timeOut)
|
||||
logger.debug("准备连接PLC,IP:%s, slot: %d, timeout: %d", self.plcIp, self.plcSlot, self.timeOut)
|
||||
self.comm = PLC(self.plcIp, self.plcSlot, self.timeOut)
|
||||
logger.info("连接PLC成功")
|
||||
|
||||
self.comm.ConnectionSize = 504
|
||||
|
||||
def __del__(self):
|
||||
logger.info("准备断开PLC连接")
|
||||
logger.debug("准备断开PLC连接")
|
||||
self.comm.Close()
|
||||
logger.info("断开PLC连接完成")
|
||||
logger.debug("断开PLC连接完成")
|
||||
|
||||
def __isIP(self, ip: str):
|
||||
"""
|
||||
|
@ -37,12 +38,12 @@ class PLC_Tool:
|
|||
def connect(self):
|
||||
logger.warning("准备重新连接PLC")
|
||||
self.comm = PLC(self.plcIp, self.plcSlot, self.timeOut)
|
||||
logger.info("连接PLC成功")
|
||||
self.comm.ConnectionSize = 504
|
||||
|
||||
def close(self):
|
||||
logger.info("准备断开PLC连接")
|
||||
logger.debug("准备断开PLC连接")
|
||||
self.comm.Close()
|
||||
logger.info("断开PLC连接完成")
|
||||
logger.debug("断开PLC连接完成")
|
||||
|
||||
def batch_write_tag(self, tags: list):
|
||||
"""
|
||||
|
@ -67,7 +68,7 @@ class PLC_Tool:
|
|||
return 0, "输入的参数格式不对,请参照:[(\"AI.speed\", 10.32),(\"AI.start\", 1)]", None
|
||||
|
||||
# print("准备写入PLC的数据:%s" % str(tags))
|
||||
logger.info("准备写入PLC的数据:%s", str(tags))
|
||||
logger.debug("准备写入PLC的数据:%s", str(tags))
|
||||
rets = self.comm.Write(tags)
|
||||
|
||||
hasNone = False
|
||||
|
@ -84,7 +85,7 @@ class PLC_Tool:
|
|||
logger.warning("写入PLC的数据中,存在部分失败:%s", str(results))
|
||||
return 2, "存在写入失败的标签", results
|
||||
else:
|
||||
logger.info("写入PLC成功,写入的内容:%s", str(results))
|
||||
logger.debug("写入PLC成功,写入的内容:%s", str(results))
|
||||
return 1, "", results
|
||||
except IOError as io_error:
|
||||
if connect_count > 0:
|
||||
|
@ -92,7 +93,6 @@ class PLC_Tool:
|
|||
logger.error("写入PLC异常,计划写入的内容:%s 。 异常反馈:%s", str(tags), traceback.format_exc())
|
||||
return 0, str(io_error), None
|
||||
connect_count += 1
|
||||
self.connect()
|
||||
return self.batch_write_tag(tags)
|
||||
except Exception as e:
|
||||
print(traceback.format_exc())
|
||||
|
@ -118,14 +118,14 @@ class PLC_Tool:
|
|||
# return False, "请输入标签'%s'的新值value:str" % tag
|
||||
|
||||
# print("需要写入的数据 %s:%s" % (tag, str(value)))
|
||||
logger.info("准备写入PLC的数据:%s:%s", tag, str(value))
|
||||
logger.debug("准备写入PLC的数据:%s:%s", tag, str(value))
|
||||
ret = self.comm.Write(tag, value)
|
||||
|
||||
if ret.Status != "Success":
|
||||
logger.error("存在写入失败的标签, 计划写入的信息:%s:%s", tag, str(value))
|
||||
return False, "存在写入失败的标签"
|
||||
else:
|
||||
logger.info("写入PLC成功,写入的数据:%s:%s", tag, str(value))
|
||||
logger.debug("写入PLC成功,写入的数据:%s:%s", tag, str(value))
|
||||
return True, ""
|
||||
except IOError as io_error:
|
||||
if connect_count > 0:
|
||||
|
@ -133,7 +133,6 @@ class PLC_Tool:
|
|||
logger.error("写入PLC异常,计划写入的内容:%s:%s 。 异常反馈:%s", tag, str(value), traceback.format_exc())
|
||||
return False, str(io_error)
|
||||
connect_count += 1
|
||||
self.connect()
|
||||
return self.write_tag(tag, value)
|
||||
except Exception as e:
|
||||
print(traceback.format_exc())
|
||||
|
@ -167,7 +166,7 @@ class PLC_Tool:
|
|||
if ret.Status != "Success":
|
||||
logger.error("数组信息写入失败,计划写入内容:%s:%s", tag, str(array))
|
||||
return False, "数组信息写入失败"
|
||||
logger.info("数组信息写入成功,写入内容:%s:%s", tag, str(array))
|
||||
logger.debug("数组信息写入成功,写入内容:%s:%s", tag, str(array))
|
||||
return True, ""
|
||||
except IOError as io_error:
|
||||
if connect_count > 0:
|
||||
|
@ -175,7 +174,6 @@ class PLC_Tool:
|
|||
logger.error("数组信息写入失败,计划写入内容:%s:%s, 反馈的异常信息:%s", tag, str(array), traceback.format_exc())
|
||||
return False, str(io_error)
|
||||
connect_count += 1
|
||||
self.connect()
|
||||
return self.write_array(tag, array)
|
||||
except Exception as e:
|
||||
print(traceback.format_exc())
|
||||
|
@ -199,7 +197,7 @@ class PLC_Tool:
|
|||
if len(tags) == 0:
|
||||
return False, "请输入标签tags:list", None
|
||||
|
||||
logger.info("读取PLC的点位信息,所读点位:%s", str(tags))
|
||||
logger.debug("读取PLC的点位信息,所读点位:%s", str(tags))
|
||||
rets = self.comm.Read(tags)
|
||||
for ret in rets:
|
||||
results.append(
|
||||
|
@ -210,7 +208,7 @@ class PLC_Tool:
|
|||
if ret.Status != "Success":
|
||||
logger.error("读取PLC的点位失败,反馈结果:%s", str(results))
|
||||
return False, "读取PLC点位失败", results
|
||||
logger.info("已读取到PLC的点位信息,读取结果:%s", str(results))
|
||||
logger.debug("已读取到PLC的点位信息,读取结果:%s", str(results))
|
||||
return True, "", results
|
||||
except IOError as io_error:
|
||||
if connect_count > 0:
|
||||
|
@ -219,7 +217,6 @@ class PLC_Tool:
|
|||
return False, str(io_error), None
|
||||
connect_count += 1
|
||||
print(traceback.format_exc())
|
||||
self.connect()
|
||||
return self.get_tags(tags)
|
||||
except Exception as e:
|
||||
print(traceback.format_exc())
|
||||
|
@ -244,7 +241,7 @@ class PLC_Tool:
|
|||
if count <= 0:
|
||||
return False, "取数个数应大于0", None
|
||||
|
||||
logger.info("读取PLC的数组信息,所读点位:%s[%d]", str(tag), count)
|
||||
logger.debug("读取PLC的数组信息,所读点位:%s[%d]", str(tag), count)
|
||||
ret = self.comm.Read(tag, count)
|
||||
results = {"tagName": ret.TagName,
|
||||
"value": ret.Value,
|
||||
|
@ -253,7 +250,7 @@ class PLC_Tool:
|
|||
if ret.Status != "Success":
|
||||
logger.error("读取PLC的数组失败,所读点位:%s[%d], 反馈结果:%s", tag, count, str(results))
|
||||
return False, "读取PLC的数组失败", results
|
||||
logger.info("已读取PLC的数组信息,读取结果:%s", str(results))
|
||||
logger.debug("已读取PLC的数组信息,读取结果:%s", str(results))
|
||||
return True, "", results
|
||||
except IOError as io_error:
|
||||
if connect_count > 0:
|
||||
|
@ -261,7 +258,6 @@ class PLC_Tool:
|
|||
logger.error("读取PLC的数组失败,所读点位:%s[%d], 反馈错误信息:%s", tag, count, traceback.format_exc())
|
||||
return False, str(io_error), None
|
||||
connect_count += 1
|
||||
self.connect()
|
||||
return self.get_array(tag, count)
|
||||
except Exception as e:
|
||||
print(traceback.format_exc())
|
||||
|
@ -276,11 +272,11 @@ class PLC_Tool:
|
|||
connect_count: int = 0
|
||||
results = []
|
||||
try:
|
||||
logger.info("获取PLC所有标签")
|
||||
logger.debug("获取PLC所有标签")
|
||||
tags = self.comm.GetTagList()
|
||||
for t in tags.Value:
|
||||
results.append({t.TagName: t.DataType})
|
||||
logger.info("已获取到PLC所有标签, 标签列表:%s", str(results))
|
||||
logger.debug("已获取到PLC所有标签, 标签列表:%s", str(results))
|
||||
return results
|
||||
except IOError as io_error:
|
||||
if connect_count > 0:
|
||||
|
@ -288,7 +284,6 @@ class PLC_Tool:
|
|||
logger.error("获取PLC所有标签失败,反馈错误信息:%s", traceback.format_exc())
|
||||
return None
|
||||
connect_count += 1
|
||||
self.connect()
|
||||
return self.get_tag_list()
|
||||
except Exception as e:
|
||||
print(traceback.format_exc())
|
||||
|
@ -303,9 +298,9 @@ class PLC_Tool:
|
|||
"""
|
||||
connect_count: int = 0
|
||||
try:
|
||||
logger.info("开始获取PLC时间")
|
||||
logger.debug("开始获取PLC时间")
|
||||
time = self.comm.GetPLCTime(raw)
|
||||
logger.info("获取PLC时间成功:%s", str(time))
|
||||
logger.debug("获取PLC时间成功:%s", str(time))
|
||||
return time
|
||||
except IOError as io_error:
|
||||
if connect_count > 0:
|
||||
|
@ -313,7 +308,6 @@ class PLC_Tool:
|
|||
logger.error("获取PLC时间失败,反馈错误信息:%s", traceback.format_exc())
|
||||
return None
|
||||
connect_count += 1
|
||||
self.connect()
|
||||
return self.get_plc_time()
|
||||
except Exception as e:
|
||||
print(traceback.format_exc())
|
||||
|
|
|
@ -21,7 +21,7 @@ class Log(object):
|
|||
self.fileHandler = RotatingFileHandler(filename="%s/log.log" % self.logPath,
|
||||
mode="a",
|
||||
maxBytes=10485760,
|
||||
backupCount=10,
|
||||
backupCount=20,
|
||||
encoding="utf-8")
|
||||
|
||||
# 设置日志级别
|
||||
|
|
|
@ -36,3 +36,5 @@ logger.info("日志地址:%s 打印日志级别:%s 存储日志级别:%s",
|
|||
logger.info("PLC地址:%s 插槽:%d 超时:%d秒 最小读取间隔:%d毫秒", config.ip_address, config.slot, config.timeout,
|
||||
config.interval_plc)
|
||||
logger.debug("车厢最大数:%d", config.carriage_num_max)
|
||||
|
||||
g_plc = None
|
Loading…
Reference in New Issue