import os
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QObject, pyqtSignal, QThread, QTimer
from PyQt5.QtWidgets import QFileDialog, QSlider
import requests
import json
from ui_mainwindow import Ui_MainWindow
import os
import sys
import subprocess
import time
import msvcrt
from win32com.client import *
from win32com.client.connect import *
# Vector Canoe Class
class CANoe:
def __init__(self):
self.application = None
self.application = DispatchEx("CANoe.Application")
self.ver = self.application.Version
print('Loaded CANoe version ',
self.ver.major, '.',
self.ver.minor, '.',
self.ver.Build, '...') # , sep,''
self.Measurement = self.application.Measurement.Running
def open_cfg(self, cfgname):
# open CANoe simulation
if self.application is not None:
# check for valid file and it is *.cfg file
if os.path.isfile(cfgname) and (os.path.splitext(cfgname)[1] == ".cfg"):
self.application.Open(cfgname)
print("opening..." + cfgname)
else:
raise RuntimeError("Can't find CANoe cfg file")
else:
raise RuntimeError("CANoe Application is missing,unable to open simulation")
def close_cfg(self):
# close CANoe simulation
if self.application is not None:
print("close cfg ...")
# self.stop_Measurement()
self.application.Quit()
self.application = None
def start_Measurement(self):
retry = 0
retry_counter = 5
# try to establish measurement within 5s timeout
while not self.application.Measurement.Running and (retry < retry_counter):
self.application.Measurement.Start()
time.sleep(1)
retry += 1
if retry == retry_counter:
raise RuntimeWarning("CANoe start measuremet failed, Please Check Connection!")
def stop_Measurement(self):
if self.application.Measurement.Running:
self.application.Measurement.Stop()
else:
pass
def get_SigVal(self, channel_num, msg_name, sig_name, bus_type="CAN"):
"""
@summary Get the value of a raw CAN signal on the CAN simulation bus
@param channel_num - Integer value to indicate from which channel we will read the signal, usually start from 1,
Check with CANoe can channel setup.
@param msg_name - String value that indicate the message name to which the signal belong. Check DBC setup.
@param sig_name - String value of the signal to be read
@param bus_type - String value of the bus type - e.g. "CAN", "LIN" and etc.
@return The CAN signal value in floating point value.
Even if the signal is of integer type, we will still return by
floating point value.
@exception None
"""
if self.application is not None:
result = self.application.GetBus(bus_type).GetSignal(channel_num, msg_name, sig_name)
return result.Value
else:
raise RuntimeError("CANoe is not open,unable to GetVariable")
def get_EnvVar(self, var):
if self.application is not None:
result = self.application.Environment.GetVariable(var)
return result.Value
else:
raise RuntimeError("CANoe is not open,unable to GetVariable")
def set_EnvVar(self, var, value):
result = None
if self.application is not None:
# set the environment varible
result = self.application.Environment.GetVariable(var)
result.Value = value
checker = self.get_EnvVar(var)
# check the environment varible is set properly?
while (checker != value):
checker = self.get_EnvVar(var)
else:
raise RuntimeError("CANoe is not open,unable to SetVariable")
def get_SysVar(self, ns_name, sysvar_name):
if (self.application != None):
systemCAN = self.application.System.Namespaces
sys_namespace = systemCAN(ns_name)
sys_value = sys_namespace.Variables(sysvar_name)
return sys_value.Value
else:
raise RuntimeError("CANoe is not open,unable to GetVariable")
def set_SysVar(self, ns_name, sysvar_name, var):
if (self.application != None):
systemCAN = self.application.System.Namespaces
sys_namespace = systemCAN(ns_name)
sys_value = sys_namespace.Variables(sysvar_name)
sys_value.Value = var
else:
raise RuntimeError("CANoe is not open,unable to GetVariable")
def DoEvents(self):
pythoncom.PumpWaitingMessages()
time.sleep(1)
def ding_push_message(msg):
web_url = "群机器人的token"
# 构建请求头部
header = {
"Content-Type": "application/json",
"Charset": "UTF-8"
}
# 构建请求数据
message = {
"msgtype": "text",
"text": {
"content": msg
},
"at": {
"isAtAll": True
}
}
# 对请求的数据进行json封装
message_json = json.dumps(message)
# 发送请求
info = requests.post(url=web_url, data=message_json, headers=header)
# 打印返回的结果
print(info.text)
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.pushButton_start.clicked.connect(self.on_canoe_start)
self.pushButton_stop.clicked.connect(self.on_canoe_stop)
self.pushButton_start_Measurement.clicked.connect(self.on_emc_start_Measurement)
self.pushButton_stop_Measurement.clicked.connect(self.on_emc_stop_Measurement)
cover_img = os.path.abspath('resource/robot.png')
image = QtGui.QPixmap(cover_img).scaled(20, 80)
self.label.setPixmap(image)
self.label.setScaledContents(True)
self.timer = QTimer() # 初始化定时器
self.timer.timeout.connect(self.time)
def on_canoe_start(self):
global app
app = CANoe() # 定义CANoe为app
app.open_cfg(r"XXXX.cfg") # 导入某个CANoe congif
def on_canoe_stop(self):
global app
app.close_cfg()
def on_emc_start_Measurement(self):
global app
time.sleep(2)
app.start_Measurement()
msg = "钉钉助手,EMC实验开始。。:"
self.textEdit_show_log.append(msg)
ding_push_message(msg)
self.timer.start(4 * 1000)
def on_emc_stop_Measurement(self):
global app
app.stop_Measurement()
self.timer.stop()
run_time = app.get_SysVar("Time", "run_time")
msg = "EMC RUN Time:" + str(run_time) + "min"
self.textEdit_show_log.append(msg)
ding_push_message(msg)
msg = "钉钉助手,EMC实验结束。。。:"
self.textEdit_show_log.append(msg)
ding_push_message(msg)
def time(self):
global app
msg = app.get_SysVar("DD_Helper", "dd_emc_string")
if msg != '':
msg = "EMC-ERROR:" + msg
ding_push_message(msg)