generated from zhangwei/Train_Identify
Compare commits
4 Commits
| Author | SHA1 | Date |
|---|---|---|
|
|
23d0de4e38 | |
|
|
3d4b918a93 | |
|
|
7108d4e85b | |
|
|
67f829258c |
|
|
@ -9,7 +9,27 @@ a = Analysis(
|
||||||
pathex=[],
|
pathex=[],
|
||||||
binaries=[],
|
binaries=[],
|
||||||
datas=[],
|
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=[],
|
hookspath=[],
|
||||||
hooksconfig={},
|
hooksconfig={},
|
||||||
runtime_hooks=[],
|
runtime_hooks=[],
|
||||||
|
|
|
||||||
11
app.py
11
app.py
|
|
@ -313,6 +313,16 @@ def runReadPLC():
|
||||||
logger.error("数据 读取&上传 异常:%s", traceback.print_exc())
|
logger.error("数据 读取&上传 异常:%s", traceback.print_exc())
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
def writePLC_heartbeat():
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
detect.writePLC_heartbeat()
|
||||||
|
except Exception as e:
|
||||||
|
logger.error("心跳(PLC时间读取)数据 读取&上传 异常:%s", traceback.print_exc())
|
||||||
|
continue
|
||||||
|
finally:
|
||||||
|
time.sleep(30)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
# --- 运行方式一 ---
|
# --- 运行方式一 ---
|
||||||
|
|
@ -327,6 +337,7 @@ if __name__ == '__main__':
|
||||||
pool = eventlet.GreenPool()
|
pool = eventlet.GreenPool()
|
||||||
pool.spawn_n(runWebSocket)
|
pool.spawn_n(runWebSocket)
|
||||||
pool.spawn_n(runReadPLC)
|
pool.spawn_n(runReadPLC)
|
||||||
|
pool.spawn_n(writePLC_heartbeat)
|
||||||
pool.waitall()
|
pool.waitall()
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,17 +4,17 @@ track_num = 2
|
||||||
|
|
||||||
[log]
|
[log]
|
||||||
log_path = ./logs
|
log_path = ./logs
|
||||||
stdout_log_level = INFO
|
stdout_log_level = DEBUG
|
||||||
file_log_level = INFO
|
file_log_level = DEBUG
|
||||||
|
|
||||||
[plc]
|
[plc]
|
||||||
ip_address = 10.227.181.106
|
ip_address = 192.168.1.150
|
||||||
slot = 0
|
slot = 0
|
||||||
timeout = 0.9
|
timeout = 0.9
|
||||||
interval_plc = 6000
|
interval_plc = 2000
|
||||||
|
|
||||||
[upload]
|
[upload]
|
||||||
upload_type = 1
|
upload_type = 0
|
||||||
login_web_url = http://192.168.2.167:20004/api/blade-auth/oauth/token
|
login_web_url = http://192.168.2.167:20004/api/blade-auth/oauth/token
|
||||||
upload_pinch_coal_url = http://xxxx
|
upload_pinch_coal_url = http://xxxx
|
||||||
upload_gross_weight_url = http://xxxx
|
upload_gross_weight_url = http://xxxx
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,8 @@ import time
|
||||||
|
|
||||||
from service.interfaceToWeb import interfaceToWeb
|
from service.interfaceToWeb import interfaceToWeb
|
||||||
from service.TagsList import TagsList, plc_info
|
from service.TagsList import TagsList, plc_info
|
||||||
from utils.Tool import logger, config, PLC_Tool
|
from utils.Tool import logger, config, PLC_Tool, g_read_plc, g_write_plc
|
||||||
|
|
||||||
|
|
||||||
plcInfo = plc_info()
|
plcInfo = plc_info()
|
||||||
|
|
||||||
|
|
@ -20,9 +21,9 @@ def writePLC(ip: str, slot: int = 0, tags=None):
|
||||||
if len(tags) < 1:
|
if len(tags) < 1:
|
||||||
return False, "参数tags未写入需要修改的标签点位"
|
return False, "参数tags未写入需要修改的标签点位"
|
||||||
|
|
||||||
plc = PLC_Tool(ip, slot)
|
# plc = PLC_Tool(ip, slot)
|
||||||
|
|
||||||
plc.batch_write_tag(tags)
|
g_read_plc.batch_write_tag(tags)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
@ -69,7 +70,7 @@ def read_loading_tags(tagsList, plc):
|
||||||
# 开始配煤时,记录标志,准备读取掐煤量
|
# 开始配煤时,记录标志,准备读取掐煤量
|
||||||
# 注意:此处之前用 completion_coal_blending_type 从true变为false判断该读掐煤量,后发现PLC在点击“配煤重读”时,此值不准确跳动
|
# 注意:此处之前用 completion_coal_blending_type 从true变为false判断该读掐煤量,后发现PLC在点击“配煤重读”时,此值不准确跳动
|
||||||
# 故 改为 start_coal_blending_type 从false变为true时读取;效果会比前者稍慢
|
# 故 改为 start_coal_blending_type 从false变为true时读取;效果会比前者稍慢
|
||||||
if loadingTagsNamelist[index] == "start_coal_blending_type" \
|
if loadingTagsNamelist[index] == "completion_coal_blending_type" \
|
||||||
and len(plcInfo.loadingDict_List) > track \
|
and len(plcInfo.loadingDict_List) > track \
|
||||||
and loadingTagsNamelist[index] in plcInfo.loadingDict_List[track].keys() \
|
and loadingTagsNamelist[index] in plcInfo.loadingDict_List[track].keys() \
|
||||||
and plcInfo.loadingDict_List[track][loadingTagsNamelist[index]] != tag["value"] \
|
and plcInfo.loadingDict_List[track][loadingTagsNamelist[index]] != tag["value"] \
|
||||||
|
|
@ -252,28 +253,41 @@ def upload_load_type(loadingDict: dict):
|
||||||
else:
|
else:
|
||||||
return -1, ""
|
return -1, ""
|
||||||
|
|
||||||
|
def writePLC_heartbeat():
|
||||||
|
global g_write_plc
|
||||||
|
if g_write_plc is None:
|
||||||
|
g_write_plc = PLC_Tool(config.ip_address, config.slot)
|
||||||
|
g_write_plc.get_plc_time()
|
||||||
|
|
||||||
def interval_read_PLC_up_web():
|
def interval_read_PLC_up_web():
|
||||||
"""
|
"""
|
||||||
循环读取PLC数据 根据条件上传至WEB
|
循环读取PLC数据 根据条件上传至WEB
|
||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
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()
|
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)
|
config.password, config.authorization)
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
up_result = ""
|
up_result = ""
|
||||||
start_time = time.time()
|
start_time = time.time()
|
||||||
load_result, flag_changeds = readPLC(plc)
|
load_result, flag_changeds = readPLC(g_read_plc)
|
||||||
end_time = time.time()
|
end_time = time.time()
|
||||||
# print(end_time, "运行时长:%3.f \n" % (end_time - start_time))
|
# print(end_time, "运行时长:%3.f \n" % (end_time - start_time))
|
||||||
if not load_result:
|
if not load_result:
|
||||||
time.sleep(30)
|
time.sleep(4)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if login_result is not "":
|
if config.upload_type == 1 and login_result is not "":
|
||||||
logger.warn(login_result)
|
logger.warn(login_result)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
@ -363,7 +377,7 @@ def interval_read_PLC_up_web():
|
||||||
(track+1), loadingDict["loading_carriage_order"],
|
(track+1), loadingDict["loading_carriage_order"],
|
||||||
loadingDict.get("gross_weight_refresh", 0)))
|
loadingDict.get("gross_weight_refresh", 0)))
|
||||||
|
|
||||||
plc = PLC_Tool(config.ip_address, config.slot)
|
# plc = PLC_Tool(config.ip_address, config.slot)
|
||||||
|
|
||||||
grossrefresh_Tag = ""
|
grossrefresh_Tag = ""
|
||||||
for grossrefresh_l in TagsList().getLoading_Tags()[track]:
|
for grossrefresh_l in TagsList().getLoading_Tags()[track]:
|
||||||
|
|
@ -371,7 +385,7 @@ def interval_read_PLC_up_web():
|
||||||
grossrefresh_Tag = grossrefresh_l[1]
|
grossrefresh_Tag = grossrefresh_l[1]
|
||||||
if grossrefresh_Tag == "":
|
if grossrefresh_Tag == "":
|
||||||
logger.error("股道%d- 配煤重读点位读取失败 !!!", (track + 1))
|
logger.error("股道%d- 配煤重读点位读取失败 !!!", (track + 1))
|
||||||
success, msg = plc.write_tag(grossrefresh_Tag, 0)
|
success, msg = g_read_plc.write_tag(grossrefresh_Tag, 0)
|
||||||
|
|
||||||
if not success:
|
if not success:
|
||||||
logger.error("股道%d- 配煤重读归零失败 !!!", (track+1))
|
logger.error("股道%d- 配煤重读归零失败 !!!", (track+1))
|
||||||
|
|
@ -458,18 +472,24 @@ def report_open(pound_no: str, total_number_carriage: int, pre_load: list):
|
||||||
elif tag_list[0] == "total_number_carriage":
|
elif tag_list[0] == "total_number_carriage":
|
||||||
total_number_carriage_tag = tag_list[1]
|
total_number_carriage_tag = tag_list[1]
|
||||||
|
|
||||||
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:
|
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
|
over_write = True
|
||||||
old_total_number_carriage = plcInfo.loadingDict_List[pound_no-1]["total_number_carriage"]
|
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_write_plc.get_array(tagsList.getPreLoadWeight_Tags()[pound_no-1][1], int(tagsList.getPreLoadWeight_Tags()[pound_no-1][3]))
|
||||||
|
|
||||||
if success:
|
if success:
|
||||||
old_pre_load = tags_value["value"]
|
old_pre_load = tags_value["value"]
|
||||||
|
|
||||||
success, msg, results = \
|
success, msg, results = \
|
||||||
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)])
|
(total_number_carriage_tag, total_number_carriage)])
|
||||||
|
|
||||||
if not success:
|
if not success:
|
||||||
|
|
@ -484,7 +504,7 @@ def report_open(pound_no: str, total_number_carriage: int, pre_load: list):
|
||||||
pre_load.insert(0, 0.0)
|
pre_load.insert(0, 0.0)
|
||||||
logger.info("股道%d- 总车节数:%d 上传预装量:%s", pound_no, total_number_carriage, str(pre_load))
|
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_write_plc.write_array("AI.load", pre_load)
|
||||||
if not success:
|
if not success:
|
||||||
return 412, msg, {}
|
return 412, msg, {}
|
||||||
|
|
||||||
|
|
@ -548,8 +568,10 @@ def report_close(pound_no: str, total_number_carriage: int):
|
||||||
if len(pre_load_tags_list) < (pound_no - 1):
|
if len(pre_load_tags_list) < (pound_no - 1):
|
||||||
return 412, "股道“预装量”的点位配置未读到,请检查配置文件", {}
|
return 412, "股道“预装量”的点位配置未读到,请检查配置文件", {}
|
||||||
|
|
||||||
plc = PLC_Tool(config.ip_address, config.slot)
|
global g_write_plc
|
||||||
success, msg, results = plc.batch_write_tag([(open_report_tags_list[pound_no - 1], 0),
|
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)])
|
(total_number_carriage_tags_list[pound_no - 1], 0)])
|
||||||
if not success:
|
if not success:
|
||||||
return 412, msg, {}
|
return 412, msg, {}
|
||||||
|
|
@ -560,7 +582,7 @@ def report_close(pound_no: str, total_number_carriage: int):
|
||||||
# return responseMsg(200, msg, results)
|
# return responseMsg(200, msg, results)
|
||||||
|
|
||||||
pre_load = [0] * config.carriage_num_max
|
pre_load = [0] * config.carriage_num_max
|
||||||
success, msg = 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:
|
if not success:
|
||||||
return 412, msg, {}
|
return 412, msg, {}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -31,7 +31,7 @@ class interfaceToWeb:
|
||||||
"grant_type": "password"}
|
"grant_type": "password"}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
res = requests.post(url=url, headers=headers, data=data)
|
res = requests.post(url=url, headers=headers, data=data, timeout=1)
|
||||||
|
|
||||||
if res.status_code == requests.codes.ok:
|
if res.status_code == requests.codes.ok:
|
||||||
jsonData = json.loads(res.text)
|
jsonData = json.loads(res.text)
|
||||||
|
|
@ -68,7 +68,7 @@ class interfaceToWeb:
|
||||||
"poundNo": track_name}
|
"poundNo": track_name}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
res = requests.post(url=url, headers=headers, json=data)
|
res = requests.post(url=url, headers=headers, json=data, timeout=1)
|
||||||
|
|
||||||
if res.status_code == requests.codes.ok:
|
if res.status_code == requests.codes.ok:
|
||||||
jsonData = json.loads(res.text)
|
jsonData = json.loads(res.text)
|
||||||
|
|
@ -108,7 +108,7 @@ class interfaceToWeb:
|
||||||
"houseStatus": 4,
|
"houseStatus": 4,
|
||||||
"poundNo": track_name}
|
"poundNo": track_name}
|
||||||
try:
|
try:
|
||||||
res = requests.post(url=url, headers=headers, json=data)
|
res = requests.post(url=url, headers=headers, json=data, timeout=1)
|
||||||
|
|
||||||
if res.status_code == requests.codes.ok:
|
if res.status_code == requests.codes.ok:
|
||||||
jsonData = json.loads(res.text)
|
jsonData = json.loads(res.text)
|
||||||
|
|
@ -145,7 +145,7 @@ class interfaceToWeb:
|
||||||
"houseStatus": house_status,
|
"houseStatus": house_status,
|
||||||
"poundNo": track_name}
|
"poundNo": track_name}
|
||||||
try:
|
try:
|
||||||
res = requests.post(url=url, headers=headers, json=data)
|
res = requests.post(url=url, headers=headers, json=data, timeout=1)
|
||||||
|
|
||||||
if res.status_code == requests.codes.ok:
|
if res.status_code == requests.codes.ok:
|
||||||
jsonData = json.loads(res.text)
|
jsonData = json.loads(res.text)
|
||||||
|
|
@ -172,7 +172,7 @@ class interfaceToWeb:
|
||||||
"""
|
"""
|
||||||
headers = {'Content-Type': 'application/json', 'blade-auth': self.token}
|
headers = {'Content-Type': 'application/json', 'blade-auth': self.token}
|
||||||
try:
|
try:
|
||||||
res = requests.post(url=url, headers=headers, json=data)
|
res = requests.post(url=url, headers=headers, json=data, timeout=1)
|
||||||
|
|
||||||
if res.status_code == requests.codes.ok:
|
if res.status_code == requests.codes.ok:
|
||||||
jsonData = json.loads(res.text)
|
jsonData = json.loads(res.text)
|
||||||
|
|
|
||||||
|
|
@ -8,19 +8,20 @@ logger = Log(stdout_log_level=logging.FATAL,
|
||||||
fil_log_level=logging.FATAL).getLogger()
|
fil_log_level=logging.FATAL).getLogger()
|
||||||
|
|
||||||
class PLC_Tool:
|
class PLC_Tool:
|
||||||
def __init__(self, plcIp: str, plcSlot: int = 0, timeOut: float = 1):
|
def __init__(self, plcIp: str, plcSlot: int = 0, timeOut: float = 2):
|
||||||
self.plcIp = plcIp
|
self.plcIp = plcIp
|
||||||
self.plcSlot = plcSlot
|
self.plcSlot = plcSlot
|
||||||
self.timeOut = timeOut
|
self.timeOut = timeOut
|
||||||
self.comm = None
|
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)
|
self.comm = PLC(self.plcIp, self.plcSlot, self.timeOut)
|
||||||
logger.info("连接PLC成功")
|
|
||||||
|
self.comm.ConnectionSize = 504
|
||||||
|
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
logger.info("准备断开PLC连接")
|
logger.debug("准备断开PLC连接")
|
||||||
self.comm.Close()
|
self.comm.Close()
|
||||||
logger.info("断开PLC连接完成")
|
logger.debug("断开PLC连接完成")
|
||||||
|
|
||||||
def __isIP(self, ip: str):
|
def __isIP(self, ip: str):
|
||||||
"""
|
"""
|
||||||
|
|
@ -37,12 +38,15 @@ class PLC_Tool:
|
||||||
def connect(self):
|
def connect(self):
|
||||||
logger.warning("准备重新连接PLC")
|
logger.warning("准备重新连接PLC")
|
||||||
self.comm = PLC(self.plcIp, self.plcSlot, self.timeOut)
|
self.comm = PLC(self.plcIp, self.plcSlot, self.timeOut)
|
||||||
logger.info("连接PLC成功")
|
self.comm.ConnectionSize = 504
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
logger.info("准备断开PLC连接")
|
logger.debug("准备断开PLC连接")
|
||||||
self.comm.Close()
|
self.comm.Close()
|
||||||
logger.info("断开PLC连接完成")
|
logger.debug("断开PLC连接完成")
|
||||||
|
|
||||||
|
def is_connected(self):
|
||||||
|
return self.comm.conn.SocketConnected
|
||||||
|
|
||||||
def batch_write_tag(self, tags: list):
|
def batch_write_tag(self, tags: list):
|
||||||
"""
|
"""
|
||||||
|
|
@ -67,7 +71,7 @@ class PLC_Tool:
|
||||||
return 0, "输入的参数格式不对,请参照:[(\"AI.speed\", 10.32),(\"AI.start\", 1)]", None
|
return 0, "输入的参数格式不对,请参照:[(\"AI.speed\", 10.32),(\"AI.start\", 1)]", None
|
||||||
|
|
||||||
# print("准备写入PLC的数据:%s" % str(tags))
|
# print("准备写入PLC的数据:%s" % str(tags))
|
||||||
logger.info("准备写入PLC的数据:%s", str(tags))
|
logger.debug("准备写入PLC的数据:%s", str(tags))
|
||||||
rets = self.comm.Write(tags)
|
rets = self.comm.Write(tags)
|
||||||
|
|
||||||
hasNone = False
|
hasNone = False
|
||||||
|
|
@ -84,7 +88,7 @@ class PLC_Tool:
|
||||||
logger.warning("写入PLC的数据中,存在部分失败:%s", str(results))
|
logger.warning("写入PLC的数据中,存在部分失败:%s", str(results))
|
||||||
return 2, "存在写入失败的标签", results
|
return 2, "存在写入失败的标签", results
|
||||||
else:
|
else:
|
||||||
logger.info("写入PLC成功,写入的内容:%s", str(results))
|
logger.debug("写入PLC成功,写入的内容:%s", str(results))
|
||||||
return 1, "", results
|
return 1, "", results
|
||||||
except IOError as io_error:
|
except IOError as io_error:
|
||||||
if connect_count > 0:
|
if connect_count > 0:
|
||||||
|
|
@ -92,7 +96,6 @@ class PLC_Tool:
|
||||||
logger.error("写入PLC异常,计划写入的内容:%s 。 异常反馈:%s", str(tags), traceback.format_exc())
|
logger.error("写入PLC异常,计划写入的内容:%s 。 异常反馈:%s", str(tags), traceback.format_exc())
|
||||||
return 0, str(io_error), None
|
return 0, str(io_error), None
|
||||||
connect_count += 1
|
connect_count += 1
|
||||||
self.connect()
|
|
||||||
return self.batch_write_tag(tags)
|
return self.batch_write_tag(tags)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(traceback.format_exc())
|
print(traceback.format_exc())
|
||||||
|
|
@ -118,27 +121,27 @@ class PLC_Tool:
|
||||||
# return False, "请输入标签'%s'的新值value:str" % tag
|
# return False, "请输入标签'%s'的新值value:str" % tag
|
||||||
|
|
||||||
# print("需要写入的数据 %s:%s" % (tag, str(value)))
|
# 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)
|
ret = self.comm.Write(tag, value)
|
||||||
|
|
||||||
if ret.Status != "Success":
|
if ret.Status == "Success":
|
||||||
logger.error("存在写入失败的标签, 计划写入的信息:%s:%s", tag, str(value))
|
logger.debug("写入PLC成功,写入的数据:%s:%s", tag, str(value))
|
||||||
return False, "存在写入失败的标签"
|
|
||||||
else:
|
|
||||||
logger.info("写入PLC成功,写入的数据:%s:%s", tag, str(value))
|
|
||||||
return True, ""
|
return True, ""
|
||||||
|
else:
|
||||||
|
logger.error("存在写入失败的标签, 请重试。计划写入的信息:%s:%s", tag, str(value))
|
||||||
|
return False, "存在写入失败的标签,请重试。"
|
||||||
|
|
||||||
except IOError as io_error:
|
except IOError as io_error:
|
||||||
if connect_count > 0:
|
if connect_count > 0:
|
||||||
print(traceback.format_exc())
|
# print(traceback.format_exc())
|
||||||
logger.error("写入PLC异常,计划写入的内容:%s:%s 。 异常反馈:%s", tag, str(value), traceback.format_exc())
|
logger.error("写入PLC异常,请重试。计划写入的内容:%s:%s 。 异常反馈:%s", tag, str(value), traceback.format_exc())
|
||||||
return False, str(io_error)
|
return False, "写入PLC异常,请重试。计划写入的内容:%s:%s 。 异常反馈:%s" % (tag, str(value), traceback.format_exc())
|
||||||
connect_count += 1
|
connect_count += 1
|
||||||
self.connect()
|
|
||||||
return self.write_tag(tag, value)
|
return self.write_tag(tag, value)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(traceback.format_exc())
|
# print(traceback.format_exc())
|
||||||
logger.error("写入PLC异常,计划写入的内容:%s:%s 。 异常反馈:%s", tag, str(value), traceback.format_exc())
|
logger.error("写入PLC异常,请重试。计划写入的内容:%s:%s 。 异常反馈:%s", tag, str(value), traceback.format_exc())
|
||||||
return False, str(e)
|
return False, "写入PLC异常,请重试。计划写入的内容:%s:%s 。 异常反馈:%s" % (tag, str(value), traceback.format_exc())
|
||||||
|
|
||||||
def write_array(self, tag: str, array: list):
|
def write_array(self, tag: str, array: list):
|
||||||
"""
|
"""
|
||||||
|
|
@ -164,24 +167,26 @@ class PLC_Tool:
|
||||||
# "value": ret.Value,
|
# "value": ret.Value,
|
||||||
# "status": ret.Status
|
# "status": ret.Status
|
||||||
# }]
|
# }]
|
||||||
if ret.Status != "Success":
|
if ret.Status == "Success":
|
||||||
|
pass
|
||||||
|
else:
|
||||||
logger.error("数组信息写入失败,计划写入内容:%s:%s", tag, str(array))
|
logger.error("数组信息写入失败,计划写入内容:%s:%s", tag, str(array))
|
||||||
return False, "数组信息写入失败"
|
return False, "数组信息写入失败,计划写入内容:%s:%s" % (tag, str(array))
|
||||||
logger.info("数组信息写入成功,写入内容:%s:%s", tag, str(array))
|
logger.debug("数组信息写入成功,写入内容:%s:%s", tag, str(array))
|
||||||
return True, ""
|
return True, ""
|
||||||
except IOError as io_error:
|
except IOError as io_error:
|
||||||
if connect_count > 0:
|
if connect_count > 0:
|
||||||
print(traceback.format_exc())
|
# print(traceback.format_exc())
|
||||||
logger.error("数组信息写入失败,计划写入内容:%s:%s, 反馈的异常信息:%s", tag, str(array), traceback.format_exc())
|
logger.error("数组信息写入失败,请重试。计划写入内容:%s:%s, 反馈的异常信息:%s", tag, str(array), traceback.format_exc())
|
||||||
return False, str(io_error)
|
return False, "数组信息写入失败,请重试。计划写入内容:%s:%s, 反馈的异常信息:%s" % (tag, str(array), traceback.format_exc())
|
||||||
connect_count += 1
|
connect_count += 1
|
||||||
self.connect()
|
|
||||||
return self.write_array(tag, array)
|
return self.write_array(tag, array)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(traceback.format_exc())
|
# print(traceback.format_exc())
|
||||||
logger.error("数组信息写入失败,计划写入内容:%s:%s, 反馈的异常信息:%s", tag, str(array),
|
logger.error("数组信息写入失败,请重试。计划写入内容:%s:%s, 反馈的异常信息:%s", tag, str(array),
|
||||||
|
traceback.format_exc())
|
||||||
|
return False, "数组信息写入失败,请重试。计划写入内容:%s:%s, 反馈的异常信息:%s" % (tag, str(array),
|
||||||
traceback.format_exc())
|
traceback.format_exc())
|
||||||
return False, str(e)
|
|
||||||
|
|
||||||
def get_tags(self, tags: [str, list]):
|
def get_tags(self, tags: [str, list]):
|
||||||
"""
|
"""
|
||||||
|
|
@ -199,7 +204,7 @@ class PLC_Tool:
|
||||||
if len(tags) == 0:
|
if len(tags) == 0:
|
||||||
return False, "请输入标签tags:list", None
|
return False, "请输入标签tags:list", None
|
||||||
|
|
||||||
logger.info("读取PLC的点位信息,所读点位:%s", str(tags))
|
logger.debug("读取PLC的点位信息,所读点位:%s", str(tags))
|
||||||
rets = self.comm.Read(tags)
|
rets = self.comm.Read(tags)
|
||||||
for ret in rets:
|
for ret in rets:
|
||||||
results.append(
|
results.append(
|
||||||
|
|
@ -210,7 +215,7 @@ class PLC_Tool:
|
||||||
if ret.Status != "Success":
|
if ret.Status != "Success":
|
||||||
logger.error("读取PLC的点位失败,反馈结果:%s", str(results))
|
logger.error("读取PLC的点位失败,反馈结果:%s", str(results))
|
||||||
return False, "读取PLC点位失败", results
|
return False, "读取PLC点位失败", results
|
||||||
logger.info("已读取到PLC的点位信息,读取结果:%s", str(results))
|
logger.debug("已读取到PLC的点位信息,读取结果:%s", str(results))
|
||||||
return True, "", results
|
return True, "", results
|
||||||
except IOError as io_error:
|
except IOError as io_error:
|
||||||
if connect_count > 0:
|
if connect_count > 0:
|
||||||
|
|
@ -219,7 +224,6 @@ class PLC_Tool:
|
||||||
return False, str(io_error), None
|
return False, str(io_error), None
|
||||||
connect_count += 1
|
connect_count += 1
|
||||||
print(traceback.format_exc())
|
print(traceback.format_exc())
|
||||||
self.connect()
|
|
||||||
return self.get_tags(tags)
|
return self.get_tags(tags)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(traceback.format_exc())
|
print(traceback.format_exc())
|
||||||
|
|
@ -244,7 +248,7 @@ class PLC_Tool:
|
||||||
if count <= 0:
|
if count <= 0:
|
||||||
return False, "取数个数应大于0", None
|
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)
|
ret = self.comm.Read(tag, count)
|
||||||
results = {"tagName": ret.TagName,
|
results = {"tagName": ret.TagName,
|
||||||
"value": ret.Value,
|
"value": ret.Value,
|
||||||
|
|
@ -253,7 +257,7 @@ class PLC_Tool:
|
||||||
if ret.Status != "Success":
|
if ret.Status != "Success":
|
||||||
logger.error("读取PLC的数组失败,所读点位:%s[%d], 反馈结果:%s", tag, count, str(results))
|
logger.error("读取PLC的数组失败,所读点位:%s[%d], 反馈结果:%s", tag, count, str(results))
|
||||||
return False, "读取PLC的数组失败", results
|
return False, "读取PLC的数组失败", results
|
||||||
logger.info("已读取PLC的数组信息,读取结果:%s", str(results))
|
logger.debug("已读取PLC的数组信息,读取结果:%s", str(results))
|
||||||
return True, "", results
|
return True, "", results
|
||||||
except IOError as io_error:
|
except IOError as io_error:
|
||||||
if connect_count > 0:
|
if connect_count > 0:
|
||||||
|
|
@ -261,7 +265,6 @@ class PLC_Tool:
|
||||||
logger.error("读取PLC的数组失败,所读点位:%s[%d], 反馈错误信息:%s", tag, count, traceback.format_exc())
|
logger.error("读取PLC的数组失败,所读点位:%s[%d], 反馈错误信息:%s", tag, count, traceback.format_exc())
|
||||||
return False, str(io_error), None
|
return False, str(io_error), None
|
||||||
connect_count += 1
|
connect_count += 1
|
||||||
self.connect()
|
|
||||||
return self.get_array(tag, count)
|
return self.get_array(tag, count)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(traceback.format_exc())
|
print(traceback.format_exc())
|
||||||
|
|
@ -276,11 +279,11 @@ class PLC_Tool:
|
||||||
connect_count: int = 0
|
connect_count: int = 0
|
||||||
results = []
|
results = []
|
||||||
try:
|
try:
|
||||||
logger.info("获取PLC所有标签")
|
logger.debug("获取PLC所有标签")
|
||||||
tags = self.comm.GetTagList()
|
tags = self.comm.GetTagList()
|
||||||
for t in tags.Value:
|
for t in tags.Value:
|
||||||
results.append({t.TagName: t.DataType})
|
results.append({t.TagName: t.DataType})
|
||||||
logger.info("已获取到PLC所有标签, 标签列表:%s", str(results))
|
logger.debug("已获取到PLC所有标签, 标签列表:%s", str(results))
|
||||||
return results
|
return results
|
||||||
except IOError as io_error:
|
except IOError as io_error:
|
||||||
if connect_count > 0:
|
if connect_count > 0:
|
||||||
|
|
@ -288,7 +291,6 @@ class PLC_Tool:
|
||||||
logger.error("获取PLC所有标签失败,反馈错误信息:%s", traceback.format_exc())
|
logger.error("获取PLC所有标签失败,反馈错误信息:%s", traceback.format_exc())
|
||||||
return None
|
return None
|
||||||
connect_count += 1
|
connect_count += 1
|
||||||
self.connect()
|
|
||||||
return self.get_tag_list()
|
return self.get_tag_list()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(traceback.format_exc())
|
print(traceback.format_exc())
|
||||||
|
|
@ -303,9 +305,9 @@ class PLC_Tool:
|
||||||
"""
|
"""
|
||||||
connect_count: int = 0
|
connect_count: int = 0
|
||||||
try:
|
try:
|
||||||
logger.info("开始获取PLC时间")
|
# logger.debug("开始获取PLC时间")
|
||||||
time = self.comm.GetPLCTime(raw)
|
time = self.comm.GetPLCTime(raw)
|
||||||
logger.info("获取PLC时间成功:%s", str(time))
|
# logger.debug("获取PLC时间成功:%s", str(time))
|
||||||
return time
|
return time
|
||||||
except IOError as io_error:
|
except IOError as io_error:
|
||||||
if connect_count > 0:
|
if connect_count > 0:
|
||||||
|
|
@ -313,7 +315,6 @@ class PLC_Tool:
|
||||||
logger.error("获取PLC时间失败,反馈错误信息:%s", traceback.format_exc())
|
logger.error("获取PLC时间失败,反馈错误信息:%s", traceback.format_exc())
|
||||||
return None
|
return None
|
||||||
connect_count += 1
|
connect_count += 1
|
||||||
self.connect()
|
|
||||||
return self.get_plc_time()
|
return self.get_plc_time()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(traceback.format_exc())
|
print(traceback.format_exc())
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,7 @@ class Log(object):
|
||||||
self.fileHandler = RotatingFileHandler(filename="%s/log.log" % self.logPath,
|
self.fileHandler = RotatingFileHandler(filename="%s/log.log" % self.logPath,
|
||||||
mode="a",
|
mode="a",
|
||||||
maxBytes=10485760,
|
maxBytes=10485760,
|
||||||
backupCount=10,
|
backupCount=20,
|
||||||
encoding="utf-8")
|
encoding="utf-8")
|
||||||
|
|
||||||
# 设置日志级别
|
# 设置日志级别
|
||||||
|
|
|
||||||
|
|
@ -36,3 +36,6 @@ logger.info("日志地址:%s 打印日志级别:%s 存储日志级别:%s",
|
||||||
logger.info("PLC地址:%s 插槽:%d 超时:%d秒 最小读取间隔:%d毫秒", config.ip_address, config.slot, config.timeout,
|
logger.info("PLC地址:%s 插槽:%d 超时:%d秒 最小读取间隔:%d毫秒", config.ip_address, config.slot, config.timeout,
|
||||||
config.interval_plc)
|
config.interval_plc)
|
||||||
logger.debug("车厢最大数:%d", config.carriage_num_max)
|
logger.debug("车厢最大数:%d", config.carriage_num_max)
|
||||||
|
|
||||||
|
g_read_plc = None
|
||||||
|
g_write_plc = None
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue