python如何调用canoe发报文

python如何调用canoe发报文

有两种方式

一、

Python 调用 canoe com API接口 实现报文收发

Python 调用 canoe com API接口 操作canoe的系统变量,capl脚本监听canoe 系统变量 来实现收发报文

第一种和第二种 其实是将 Python 组装can报文操作 移动到 capl脚本中实现,capl监听canoe 系统变量 就是一个变量 ,canoe系统变量本身不存放 can信号,只是作为一个变量来 出发capl脚本特定信号的条件使用

二、

接下来,通过Python脚本调用Canoe的COM接口:

import win32com.client
创建Canoe应用程序对象
canoe_app = win32com.client.Dispatch('CANoe.Application')
加载Canoe配置文件
canoe_app.Open('path_to_your_configuration.cfg')
获取Measurement对象
measurement = canoe_app.Measurement
启动测量
measurement.Start()
获取Canoe COM接口的CAN通道
canoe_com = canoe_app.Networks(1).CANChannels(1)
创建报文
message = canoe_com.Messages.Add('MyMessage')
设置报文属性
message.ID = 0x123
message.DLC = 8
message.Data = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]
发送报文
message.Send()
停止测量
measurement.Stop()
  1. 优点和缺点
    优点:
    灵活性高:可以完全控制Canoe的功能。
    集成性强:可以与其他Windows应用程序互操作。
    缺点:
    复杂性高:需要了解COM接口的相关知识。
    依赖性强:只能在Windows系统上运行。

使用CAPL脚本

(1)编写python脚本
import time
from win32com.client import *

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.verminor, '.',
              self.ver.Build, '...') # , sep,''
          self.Measurement = self.application.Measurement.Running

    def start_Measurement(self):
             self.application.Measurement.Start()
     
    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(ns_name)
                 systemCAN.Variables(sysvar_name).Value = var
           else:
                 raise RuntimeError("CANoe is not open,unable to GetVariable")

if __name__ == '__main__':
       app = CANoe()  # 实例化对象
       canoePrj = "C:\\Users\\wu.y\\Desktop\\CAN__FD_500kBaud_4MBaud_2ch_system_variable\\CAN_FD_500kBaud_4MBaud_2ch_system_variable.cfg"
       app.application.Open(canoePrj)
       app.start_Measurement()
       EngineSpeedDspMeter = app.get_SysVar("Engine", "EngineSpeedDspMeter")
      # 因为python脚本进程 和启动的canoe进程是异步的,所以我们python脚本要睡眠一会等待 canoe进程走完 否则 set_sysvar 可能不生效 因为canoe进程此时可能还没走到这里
     time.sleep(10)  
     print(EngineSpeedDspMeter)
     if (EngineSpeedDspMeter == 3):
           app.set_SysVar("Engine", "EngineSpeedDspMeter", 2)
           print("over")
           # 这里的睡眠同理
           time.sleep(10)
          EngineSpeedDspMeter1 = app.get_SysVar("Engine", "EngineSpeedDspMeter")
          print("EngineSpeedDspMeter1 "+ str(EngineSpeedDspMeter1))
(2)在canoe中设置系统变量 并初始值赋予3

Engine EngineSpeedDspMeter 这个系统变量赋值

(3)编写capl脚本

Capl脚本 根据系统变量 值得变化 发送开车门的信号,然后将系统变量的值赋予 4

/*@!Encoding:65001*/
includes
{
  
}
variables
{
  //定义一个ID为0x123 名称为vehicleDoor的CAN报文帧变量
  message 0x294 vehicleDoor;//由于没有DBC,只能使用以ID形式声明CAN报文
  }
  on start
{
  write("Hello world111");
}
on sysvar Engine::EngineSpeedDspMeter{
  if (@Engine::EngineSpeedDspMeter == 2){
     write("Hello world");
     vehicleDoor.dlc = 8;
     vehicleDoor.byte(0) = 0x55;
    output(vehicleDoor);
    @Engine::EngineSpeedDspMeter =4;
    }
    }
(4)调用python脚本 执行程序

Canoe 执行的输出
图片

Python端的打印
系统变量获得了 最新的值=4
Loaded CANoe version 17 . 3 . 91 …
3
over
EngineSpeedDspMeter1 4

作者:A初夏1

物联沃分享整理
物联沃-IOTWORD物联网 » python如何调用canoe发报文

发表回复