同PLC的通讯改为两条长连接,分别读写

This commit is contained in:
Mr.V 2024-05-09 16:12:32 +08:00
parent 7108d4e85b
commit 3d4b918a93
3 changed files with 48 additions and 37 deletions

View File

@ -2,7 +2,7 @@ import time
from service.interfaceToWeb import interfaceToWeb
from service.TagsList import TagsList, plc_info
from utils.Tool import logger, config, PLC_Tool, g_plc
from utils.Tool import logger, config, PLC_Tool, g_read_plc, g_write_plc
plcInfo = plc_info()
@ -23,7 +23,7 @@ def writePLC(ip: str, slot: int = 0, tags=None):
# plc = PLC_Tool(ip, slot)
g_plc.batch_write_tag(tags)
g_read_plc.batch_write_tag(tags)
return True
@ -259,9 +259,9 @@ def interval_read_PLC_up_web():
循环读取PLC数据 根据条件上传至WEB
:return:
"""
global g_plc
if g_plc is None:
g_plc = PLC_Tool(config.ip_address, config.slot)
global g_read_plc
if g_read_plc is None:
g_read_plc = PLC_Tool(config.ip_address, config.slot)
interface = interfaceToWeb()
login_result = ""
@ -274,7 +274,7 @@ def interval_read_PLC_up_web():
while True:
up_result = ""
start_time = time.time()
load_result, flag_changeds = readPLC(g_plc)
load_result, flag_changeds = readPLC(g_read_plc)
end_time = time.time()
# print(end_time, "运行时长:%3.f \n" % (end_time - start_time))
if not load_result:
@ -379,7 +379,7 @@ def interval_read_PLC_up_web():
grossrefresh_Tag = grossrefresh_l[1]
if grossrefresh_Tag == "":
logger.error("股道%d- 配煤重读点位读取失败 ", (track + 1))
success, msg = g_plc.write_tag(grossrefresh_Tag, 0)
success, msg = g_read_plc.write_tag(grossrefresh_Tag, 0)
if not success:
logger.error("股道%d- 配煤重读归零失败 ", (track+1))
@ -466,21 +466,24 @@ 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]
global g_plc
if g_plc is None:
g_plc = PLC_Tool(config.ip_address, config.slot)
global g_write_plc
if g_write_plc is None:
g_write_plc = PLC_Tool(config.ip_address, config.slot)
if len(plcInfo.loadingDict_List) < 1:
return 500, "尚未成功读取到PLC的数据请确认PLC链接是否正常", {}
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"]
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]))
success, msg, tags_value = g_write_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 = \
g_plc.batch_write_tag([(report_open_type_tag, 1),
g_write_plc.batch_write_tag([(report_open_type_tag, 1),
(total_number_carriage_tag, total_number_carriage)])
if not success:
@ -495,7 +498,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 = g_plc.write_array("AI.load", pre_load)
success, msg = g_write_plc.write_array("AI.load", pre_load)
if not success:
return 412, msg, {}
@ -559,10 +562,10 @@ def report_close(pound_no: str, total_number_carriage: int):
if len(pre_load_tags_list) < (pound_no - 1):
return 412, "股道“预装量”的点位配置未读到,请检查配置文件", {}
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),
global g_write_plc
if g_write_plc is None:
g_write_plc= PLC_Tool(config.ip_address, config.slot)
success, msg, results = g_write_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, {}
@ -573,7 +576,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 = g_plc.write_array(pre_load_tags_list[pound_no - 1], pre_load)
success, msg = g_write_plc.write_array(pre_load_tags_list[pound_no - 1], pre_load)
if not success:
return 412, msg, {}

View File

@ -45,6 +45,9 @@ class PLC_Tool:
self.comm.Close()
logger.debug("断开PLC连接完成")
def is_connected(self):
return self.comm.conn.SocketConnected
def batch_write_tag(self, tags: list):
"""
Write tags to PLC . 批量写入标签
@ -121,23 +124,24 @@ class PLC_Tool:
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:
if ret.Status == "Success":
logger.debug("写入PLC成功写入的数据%s:%s", tag, str(value))
return True, ""
else:
logger.error("存在写入失败的标签, 请重试。计划写入的信息:%s:%s", tag, str(value))
return False, "存在写入失败的标签,请重试。"
except IOError as io_error:
if connect_count > 0:
print(traceback.format_exc())
logger.error("写入PLC异常计划写入的内容%s:%s 。 异常反馈:%s", tag, str(value), traceback.format_exc())
return False, str(io_error)
# print(traceback.format_exc())
logger.error("写入PLC异常请重试。计划写入的内容:%s:%s 。 异常反馈:%s", tag, str(value), traceback.format_exc())
return False, "写入PLC异常请重试。计划写入的内容%s:%s 。 异常反馈:%s" % (tag, str(value), traceback.format_exc())
connect_count += 1
return self.write_tag(tag, value)
except Exception as e:
print(traceback.format_exc())
logger.error("写入PLC异常计划写入的内容:%s:%s 。 异常反馈:%s", tag, str(value), traceback.format_exc())
return False, str(e)
# print(traceback.format_exc())
logger.error("写入PLC异常请重试。计划写入的内容:%s:%s 。 异常反馈:%s", tag, str(value), traceback.format_exc())
return False, "写入PLC异常请重试。计划写入的内容%s:%s 。 异常反馈:%s" % (tag, str(value), traceback.format_exc())
def write_array(self, tag: str, array: list):
"""
@ -163,23 +167,26 @@ class PLC_Tool:
# "value": ret.Value,
# "status": ret.Status
# }]
if ret.Status != "Success":
if ret.Status == "Success":
pass
else:
logger.error("数组信息写入失败,计划写入内容:%s:%s", tag, str(array))
return False, "数组信息写入失败"
return False, "数组信息写入失败,计划写入内容:%s:%s" % (tag, str(array))
logger.debug("数组信息写入成功,写入内容:%s:%s", tag, str(array))
return True, ""
except IOError as io_error:
if connect_count > 0:
print(traceback.format_exc())
logger.error("数组信息写入失败,计划写入内容:%s:%s 反馈的异常信息:%s", tag, str(array), traceback.format_exc())
return False, str(io_error)
# print(traceback.format_exc())
logger.error("数组信息写入失败,请重试。计划写入内容:%s:%s 反馈的异常信息:%s", tag, str(array), traceback.format_exc())
return False, "数组信息写入失败,请重试。计划写入内容:%s:%s 反馈的异常信息:%s" % (tag, str(array), traceback.format_exc())
connect_count += 1
return self.write_array(tag, array)
except Exception as e:
print(traceback.format_exc())
logger.error("数组信息写入失败,计划写入内容:%s:%s 反馈的异常信息:%s", tag, str(array),
# print(traceback.format_exc())
logger.error("数组信息写入失败,请重试。计划写入内容:%s:%s 反馈的异常信息:%s", tag, str(array),
traceback.format_exc())
return False, "数组信息写入失败,请重试。计划写入内容:%s:%s 反馈的异常信息:%s" % (tag, str(array),
traceback.format_exc())
return False, str(e)
def get_tags(self, tags: [str, list]):
"""

View File

@ -37,4 +37,5 @@ logger.info("PLC地址%s 插槽:%d 超时:%d秒 最小读取间隔:%d
config.interval_plc)
logger.debug("车厢最大数:%d", config.carriage_num_max)
g_plc = None
g_read_plc = None
g_write_plc = None