增加了初始弹窗,但是没有成功
更改了CAN和LIN协调 更改了配置程序的名称顺序
This commit is contained in:
423
CapMachine.Wpf/LinDrive/ToomossLin.cs
Normal file
423
CapMachine.Wpf/LinDrive/ToomossLin.cs
Normal file
@@ -0,0 +1,423 @@
|
||||
using CapMachine.Wpf.CanDrive;
|
||||
using CapMachine.Wpf.Services;
|
||||
using Microsoft.VisualBasic;
|
||||
using Prism.Ioc;
|
||||
using Prism.Mvvm;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Security.Cryptography.Xml;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CapMachine.Wpf.LinDrive
|
||||
{
|
||||
/// <summary>
|
||||
/// Toomoss 的LIN驱动
|
||||
/// </summary>
|
||||
public class ToomossLin : BindableBase
|
||||
{
|
||||
/// <summary>
|
||||
/// 设备Handles集合
|
||||
/// </summary>
|
||||
public Int32[] DevHandles { get; set; } = new Int32[20];
|
||||
/// <summary>
|
||||
/// 设备Handles
|
||||
/// 设备句柄,本质为设备序号的低4字节,可以通过调用USB_ScanDevice函数获得
|
||||
/// </summary>
|
||||
public Int32 DevHandle { get; set; } = 0;
|
||||
/// <summary>
|
||||
/// Lin的Index
|
||||
/// LIN通道索引号,填0或者1,若只有一个通道LIN,则填0.
|
||||
/// </summary>
|
||||
public Byte LINIndex { get; set; } = 0;
|
||||
/// <summary>
|
||||
/// Lin的连接State
|
||||
/// </summary>
|
||||
public bool state { get; set; }
|
||||
/// <summary>
|
||||
/// Lin的连接设备数量
|
||||
/// </summary>
|
||||
public Int32 DevNum { get; set; }
|
||||
/// <summary>
|
||||
/// Lin的Dll文件的路径
|
||||
/// </summary>
|
||||
public string dllFilePath { get; set; } = "USB2XXX.dll";
|
||||
|
||||
private readonly IContainerProvider ContainerProvider;
|
||||
|
||||
public ToomossLin(IContainerProvider containerProvider)
|
||||
{
|
||||
ContainerProvider = containerProvider;
|
||||
HighSpeedDataService = ContainerProvider.Resolve<HighSpeedDataService>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// HighSpeedDataService 实例
|
||||
/// </summary>
|
||||
public HighSpeedDataService HighSpeedDataService { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 开始LDF文件写入
|
||||
/// </summary>
|
||||
public ObservableCollection<LinLdfModel> StartLdf(string LdfPath)
|
||||
{
|
||||
LDF_Parser(LdfPath);
|
||||
return ListLinLdfModel;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 开始Lin的驱动
|
||||
/// </summary>
|
||||
public void StartLinDrive()
|
||||
{
|
||||
IsExistsDllFile();
|
||||
ScanDevice();
|
||||
OpenDevice();
|
||||
|
||||
}
|
||||
|
||||
private bool _IsCycleRevice;
|
||||
/// <summary>
|
||||
/// 是否循环接收数据
|
||||
/// </summary>
|
||||
public bool IsCycleRevice
|
||||
{
|
||||
get { return _IsCycleRevice; }
|
||||
set { _IsCycleRevice = value; RaisePropertyChanged(); }
|
||||
}
|
||||
|
||||
private bool _IsCycleSend;
|
||||
/// <summary>
|
||||
/// 是否循环发送数据
|
||||
/// </summary>
|
||||
public bool IsCycleSend
|
||||
{
|
||||
get { return _IsCycleSend; }
|
||||
set { _IsCycleSend = value; RaisePropertyChanged(); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 循环发送数据
|
||||
/// </summary>
|
||||
public ushort SendCycle { get; set; } = 100;
|
||||
|
||||
/// <summary>
|
||||
/// 循环接受数据
|
||||
/// </summary>
|
||||
public ushort ReviceCycle { get; set; } = 500;
|
||||
|
||||
/// <summary>
|
||||
/// CycleRevice 扫描Task
|
||||
/// </summary>
|
||||
private static Task CycleReviceTask { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// CycleSend 扫描Task
|
||||
/// </summary>
|
||||
private static Task CycleSendTask { get; set; }
|
||||
|
||||
private bool _OpenState;
|
||||
/// <summary>
|
||||
/// 打开设备的状态
|
||||
/// </summary>
|
||||
public bool OpenState
|
||||
{
|
||||
get { return _OpenState; }
|
||||
set { _OpenState = value; RaisePropertyChanged(); }
|
||||
}
|
||||
|
||||
|
||||
private bool _LdfParserState;
|
||||
/// <summary>
|
||||
/// LDF解析的状态
|
||||
/// </summary>
|
||||
public bool LdfParserState
|
||||
{
|
||||
get { return _LdfParserState; }
|
||||
set { _LdfParserState = value; RaisePropertyChanged(); }
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// LDFHandle
|
||||
/// </summary>
|
||||
public UInt64 LDFHandle { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// LDF消息集合
|
||||
/// 包括读取的实时值和数据
|
||||
/// </summary>
|
||||
public ObservableCollection<LinLdfModel> ListLinLdfModel { get; set; } = new ObservableCollection<LinLdfModel>();
|
||||
|
||||
/// <summary>
|
||||
/// 要发送的LIN数据
|
||||
/// </summary>
|
||||
public List<LinCmdData> CmdData { get; set; } = new List<LinCmdData>();
|
||||
|
||||
/// <summary>
|
||||
/// ******************【1】*********************
|
||||
/// 是否存在Dll文件
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public bool IsExistsDllFile()
|
||||
{
|
||||
if (!File.Exists(dllFilePath))
|
||||
{
|
||||
Console.WriteLine("请先将USB2XXX.dll和libusb-1.0.dll文件复制到exe程序文件输出目录下!");
|
||||
Console.WriteLine("dll文件在‘usb2can_lin_pwm_example/sdk/libs/windows’目录下!");
|
||||
Console.WriteLine("程序是32位的就复制‘x86’目录下文件,程序是64位的就复制‘x86_64’目录下文件!");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// ******************【2】*********************
|
||||
/// 扫描查找设备,并将每个设备的唯一设备号存放到数组中,后面的函数需要用到
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public bool ScanDevice()
|
||||
{
|
||||
DevNum = USB_DEVICE.USB_ScanDevice(DevHandles);
|
||||
if (DevNum <= 0)
|
||||
{
|
||||
Console.WriteLine("No device connected!");
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("Have {0} device connected!", DevNum);
|
||||
DevHandle = DevHandles[0];//获取第一个设备的设备号
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// ******************【3】*********************
|
||||
/// 打开设备
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public bool OpenDevice()
|
||||
{
|
||||
//打开设备
|
||||
OpenState = USB_DEVICE.USB_OpenDevice(DevHandle);
|
||||
if (!OpenState)
|
||||
{
|
||||
Console.WriteLine("Open device error!");
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("Open device success!");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// ******************【4】*********************
|
||||
/// 解析LDF信息
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public void LDF_Parser(string Path)
|
||||
{
|
||||
LDFHandle = LDFParser.LDF_ParserFile(DevHandle, LINIndex, 1, new StringBuilder(Path));
|
||||
if (LDFHandle == 0)
|
||||
{
|
||||
Console.WriteLine("解析LDF文件失败!");
|
||||
LdfParserState = false;
|
||||
return;
|
||||
}
|
||||
|
||||
//读取LDF文件信息
|
||||
Console.WriteLine("ProtocolVersion = {0}", LDFParser.LDF_GetProtocolVersion(LDFHandle));
|
||||
Console.WriteLine("LINSpeed = {0}", LDFParser.LDF_GetLINSpeed(LDFHandle));
|
||||
|
||||
//读取主机名称
|
||||
StringBuilder MasterName = new StringBuilder(64);
|
||||
LDFParser.LDF_GetMasterName(LDFHandle, MasterName);
|
||||
Console.WriteLine("Master Name = {0}", MasterName);
|
||||
|
||||
ListLinLdfModel.Clear();
|
||||
//读取信息
|
||||
int FrameLen = LDFParser.LDF_GetFrameQuantity(LDFHandle);
|
||||
for (int i = 0; i < FrameLen; i++)
|
||||
{
|
||||
//读取帧名称和发布者
|
||||
StringBuilder FrameName = new StringBuilder(64);
|
||||
string IsMasterFrame = "";
|
||||
if (LDFParser.LDF_PARSER_OK == LDFParser.LDF_GetFrameName(LDFHandle, i, FrameName))
|
||||
{
|
||||
StringBuilder PublisherName = new StringBuilder(64);
|
||||
LDFParser.LDF_GetFramePublisher(LDFHandle, FrameName, PublisherName);
|
||||
if (MasterName.Equals(PublisherName))
|
||||
{
|
||||
IsMasterFrame = "是";
|
||||
//当前帧为主机发送数据帧
|
||||
Console.WriteLine("[MW]Frame[{0}].Name={1},Publisher={2}", i, FrameName, PublisherName);
|
||||
}
|
||||
else
|
||||
{
|
||||
IsMasterFrame = "否";
|
||||
//当前帧为主机读数据帧
|
||||
Console.WriteLine("[MR]Frame[{0}].Name={1},Publisher={2}", i, FrameName, PublisherName);
|
||||
}
|
||||
|
||||
//读取信号信息
|
||||
int SignalNum = LDFParser.LDF_GetFrameSignalQuantity(LDFHandle, FrameName);
|
||||
for (int j = 0; j < SignalNum; j++)
|
||||
{
|
||||
StringBuilder SignalName = new StringBuilder(64);
|
||||
if (LDFParser.LDF_PARSER_OK == LDFParser.LDF_GetFrameSignalName(LDFHandle, FrameName, j, SignalName))
|
||||
{
|
||||
Console.WriteLine("\tSignal[{0}].Name={1}", j, SignalName);
|
||||
ListLinLdfModel.Add(new LinLdfModel()
|
||||
{
|
||||
MsgName = FrameName.ToString(),
|
||||
Publisher = PublisherName.ToString(),
|
||||
IsMasterFrame = IsMasterFrame,
|
||||
SignalName = SignalName.ToString()
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//解析成功
|
||||
LdfParserState = true;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 发送LIN数据
|
||||
/// 发送一次
|
||||
/// </summary>
|
||||
public void SendLinMsg(List<LinCmdData> CmdData)
|
||||
{
|
||||
try
|
||||
{
|
||||
//防止有多个不同的消息名称/帧,每个帧单独处理发送
|
||||
var GroupMsg = CmdData.GroupBy(x => x.MsgName);
|
||||
foreach (var itemMsg in GroupMsg)
|
||||
{
|
||||
foreach (var itemSignal in itemMsg)
|
||||
{
|
||||
//主机写操作,发送数据给从机
|
||||
LDFParser.LDF_SetSignalValue(LDFHandle, new StringBuilder(itemMsg.Key), new StringBuilder(itemSignal.SignalName), itemSignal.SignalCmdValue);
|
||||
}
|
||||
LDFParser.LDF_ExeFrameToBus(LDFHandle, new StringBuilder(itemMsg.Key), 1);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 读取信号值
|
||||
/// </summary>
|
||||
private StringBuilder ReadValueStr = new StringBuilder(64);
|
||||
|
||||
/// <summary>
|
||||
/// 循环
|
||||
/// 主机读操作,读取从机返回的数据值
|
||||
/// </summary>
|
||||
public void StartCycleReviceMsg()
|
||||
{
|
||||
CycleReviceTask = Task.Run(async () =>
|
||||
{
|
||||
while (IsCycleRevice)
|
||||
{
|
||||
await Task.Delay(ReviceCycle);
|
||||
try
|
||||
{
|
||||
//主机读操作,读取从机返回的数据值
|
||||
foreach (var item in ListLinLdfModel)
|
||||
{
|
||||
LDFParser.LDF_ExeFrameToBus(LDFHandle, new StringBuilder(item.MsgName), 1);
|
||||
LDFParser.LDF_GetSignalValueStr(LDFHandle, new StringBuilder(item.MsgName), new StringBuilder(item.SignalName), ReadValueStr);
|
||||
item.SignalRtValueSb = ReadValueStr;
|
||||
}
|
||||
|
||||
//StringBuilder ValueStr = new StringBuilder(64);
|
||||
//LDFParser.LDF_ExeFrameToBus(LDFHandle, new StringBuilder("ID_DATA"), 1);
|
||||
//LDFParser.LDF_GetSignalValueStr(LDFHandle, new StringBuilder("ID_DATA"), new StringBuilder("Supplier_ID"), ValueStr);
|
||||
//Console.WriteLine("ID_DATA.Supplier_ID={0}", ValueStr);
|
||||
//LDFParser.LDF_GetSignalValueStr(LDFHandle, new StringBuilder("ID_DATA"), new StringBuilder("Machine_ID"), ValueStr);
|
||||
//Console.WriteLine("ID_DATA.Machine_ID={0}", ValueStr);
|
||||
//LDFParser.LDF_GetSignalValueStr(LDFHandle, new StringBuilder("ID_DATA"), new StringBuilder("Chip_ID"), ValueStr);
|
||||
//Console.WriteLine("ID_DATA.Chip_ID={0}", ValueStr);
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//LogService.Info($"时间:{DateTime.Now.ToString()}-【Meter】-{ex.Message}");
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 循环发送数据
|
||||
/// </summary>
|
||||
public void StartCycleSendMsg()
|
||||
{
|
||||
CycleSendTask = Task.Run(async () =>
|
||||
{
|
||||
while (IsCycleSend)
|
||||
{
|
||||
await Task.Delay(SendCycle);
|
||||
try
|
||||
{
|
||||
//防止有多个不同的消息名称/帧,每个帧单独处理发送
|
||||
var GroupMsg = CmdData.GroupBy(x => x.MsgName);
|
||||
foreach (var itemMsg in GroupMsg)
|
||||
{
|
||||
foreach (var itemSignal in itemMsg)
|
||||
{
|
||||
//主机写操作,发送数据给从机
|
||||
LDFParser.LDF_SetSignalValue(LDFHandle, new StringBuilder(itemMsg.Key), new StringBuilder(itemSignal.SignalName), itemSignal.SignalCmdValue);
|
||||
}
|
||||
LDFParser.LDF_ExeFrameToBus(LDFHandle, new StringBuilder(itemMsg.Key), 1);
|
||||
}
|
||||
|
||||
////主机写操作,发送数据给从机
|
||||
//LDFParser.LDF_SetSignalValue(LDFHandle, new StringBuilder("LIN_CONTROL"), new StringBuilder("Reg_Set_Voltage"), 13.5);
|
||||
//LDFParser.LDF_SetSignalValue(LDFHandle, new StringBuilder("LIN_CONTROL"), new StringBuilder("Ramp_Time"), 3);
|
||||
//LDFParser.LDF_SetSignalValue(LDFHandle, new StringBuilder("LIN_CONTROL"), new StringBuilder("Cut_Off_Speed"), 4);
|
||||
//LDFParser.LDF_SetSignalValue(LDFHandle, new StringBuilder("LIN_CONTROL"), new StringBuilder("Exc_Limitation"), 15.6);
|
||||
//LDFParser.LDF_SetSignalValue(LDFHandle, new StringBuilder("LIN_CONTROL"), new StringBuilder("Derat_Shift"), 2);
|
||||
//LDFParser.LDF_SetSignalValue(LDFHandle, new StringBuilder("LIN_CONTROL"), new StringBuilder("MM_Request"), 2);
|
||||
//LDFParser.LDF_SetSignalValue(LDFHandle, new StringBuilder("LIN_CONTROL"), new StringBuilder("Reg_Blind"), 1);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//LogService.Info($"时间:{DateTime.Now.ToString()}-【Meter】-{ex.Message}");
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 关闭设备
|
||||
/// </summary>
|
||||
public void CloseDevice()
|
||||
{
|
||||
//关闭设备
|
||||
USB_DEVICE.USB_CloseDevice(DevHandle);
|
||||
OpenState = false;
|
||||
LdfParserState = false;
|
||||
IsCycleRevice = false;
|
||||
IsCycleSend = false;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user