一些优化:CAN和PLC地址的优化
This commit is contained in:
@@ -405,7 +405,6 @@ namespace CapMachine.Wpf.CanDrive
|
||||
//通过DBC写入数据后生成CanMsg
|
||||
//将信号值填入CAN消息里面
|
||||
|
||||
|
||||
//释放申请的临时缓冲区
|
||||
Marshal.FreeHGlobal(msgPt);
|
||||
Console.WriteLine("");
|
||||
@@ -421,18 +420,115 @@ namespace CapMachine.Wpf.CanDrive
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private bool _IsCycleRevice;
|
||||
/// <summary>
|
||||
/// 是否循环接收数据
|
||||
/// </summary>
|
||||
public bool IsCycleRevice { get; set; }
|
||||
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>
|
||||
/// CycleRevice扫描Task
|
||||
/// 循环发送数据
|
||||
/// </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; }
|
||||
|
||||
StringBuilder ValueSb = new StringBuilder(16);
|
||||
|
||||
/// <summary>
|
||||
/// 要发送的数据
|
||||
/// </summary>
|
||||
public List<CanCmdData> CmdData { get; set; } = new List<CanCmdData>();
|
||||
|
||||
/// <summary>
|
||||
/// 循环发送数据
|
||||
/// </summary>
|
||||
public void StartCycleSendMsg()
|
||||
{
|
||||
CycleSendTask = Task.Run(async () =>
|
||||
{
|
||||
while (IsCycleSend)
|
||||
{
|
||||
await Task.Delay(SendCycle);
|
||||
try
|
||||
{
|
||||
var GroupMsg = CmdData.GroupBy(x => x.MsgName);
|
||||
USB2CAN.CAN_MSG[] CanMsg = new USB2CAN.CAN_MSG[GroupMsg.Count()];
|
||||
for (int i = 0; i < GroupMsg.Count(); i++)
|
||||
{
|
||||
CanMsg[i] = new USB2CAN.CAN_MSG();
|
||||
CanMsg[i].Data = new Byte[64];
|
||||
}
|
||||
|
||||
IntPtr msgPtSend = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(USB2CAN.CAN_MSG)));
|
||||
int Index = 0;
|
||||
//循环给MSG赋值数据
|
||||
foreach (var itemMsg in GroupMsg)
|
||||
{
|
||||
foreach (var itemSignal in itemMsg)
|
||||
{
|
||||
CAN_DBCParser.DBC_SetSignalValue(DBCHandle, new StringBuilder(itemMsg.Key), new StringBuilder(itemSignal.SignalName), itemSignal.SignalCmdValue);
|
||||
}
|
||||
CAN_DBCParser.DBC_SyncValueToCANMsg(DBCHandle, new StringBuilder(itemMsg.Key), msgPtSend);
|
||||
CanMsg[Index] = (USB2CAN.CAN_MSG)Marshal.PtrToStructure(msgPtSend, typeof(USB2CAN.CAN_MSG));
|
||||
Index++;
|
||||
}
|
||||
//通过DBC写入数据后生成CanMsg
|
||||
//将信号值填入CAN消息里面
|
||||
|
||||
//释放申请的临时缓冲区
|
||||
Marshal.FreeHGlobal(msgPtSend);
|
||||
Console.WriteLine("");
|
||||
//发送CAN数据
|
||||
int SendedNum = USB2CAN.CAN_SendMsg(DevHandle, WriteCANIndex, CanMsg, (uint)CanMsg.Length);
|
||||
if (SendedNum >= 0)
|
||||
{
|
||||
Console.WriteLine("Success send frames:{0}", SendedNum);
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("Send CAN data failed! {0}", SendedNum);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//LogService.Info($"时间:{DateTime.Now.ToString()}-【Meter】-{ex.Message}");
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 循环获取CAN消息
|
||||
/// </summary>
|
||||
@@ -442,23 +538,24 @@ namespace CapMachine.Wpf.CanDrive
|
||||
{
|
||||
while (IsCycleRevice)
|
||||
{
|
||||
await Task.Delay(500);
|
||||
await Task.Delay(ReviceCycle);
|
||||
try
|
||||
{
|
||||
//另外一个CAN通道读取数据
|
||||
USB2CAN.CAN_MSG[] CanMsgBuffer = new USB2CAN.CAN_MSG[10];
|
||||
msgPt = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(USB2CAN.CAN_MSG)) * CanMsgBuffer.Length);
|
||||
int CanNum = USB2CAN.CAN_GetMsgWithSize(DevHandle, ReadCANIndex, msgPt, CanMsgBuffer.Length);
|
||||
USB2CAN.CAN_MSG[] CanMsgBuffer = new USB2CAN.CAN_MSG[128];
|
||||
//申请数据缓冲区
|
||||
IntPtr msgPtRead = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(USB2CAN.CAN_MSG)) * CanMsgBuffer.Length);
|
||||
int CanNum = USB2CAN.CAN_GetMsgWithSize(DevHandle, ReadCANIndex, msgPtRead, CanMsgBuffer.Length);
|
||||
if (CanNum > 0)
|
||||
{
|
||||
Console.WriteLine("Read CanMsgNum = {0}", CanNum);
|
||||
for (int i = 0; i < CanNum; i++)
|
||||
{
|
||||
//CanMsgBuffer[i] = (USB2CAN.CAN_MSG)Marshal.PtrToStructure((IntPtr)((UInt32)msgPt + i * Marshal.SizeOf(typeof(USB2CAN.CAN_MSG))), typeof(USB2CAN.CAN_MSG)); //有溢出报错
|
||||
CanMsgBuffer[i] = (USB2CAN.CAN_MSG)Marshal.PtrToStructure((IntPtr)(msgPt + i * Marshal.SizeOf(typeof(USB2CAN.CAN_MSG))), typeof(USB2CAN.CAN_MSG));
|
||||
//CanMsgBuffer[i] = (USB2CAN.CAN_MSG)Marshal.PtrToStructure((IntPtr)((UInt32)msgPtRead + i * Marshal.SizeOf(typeof(USB2CAN.CAN_MSG))), typeof(USB2CAN.CAN_MSG)); //有溢出报错
|
||||
CanMsgBuffer[i] = (USB2CAN.CAN_MSG)Marshal.PtrToStructure((IntPtr)(msgPtRead + i * Marshal.SizeOf(typeof(USB2CAN.CAN_MSG))), typeof(USB2CAN.CAN_MSG));
|
||||
|
||||
Console.WriteLine("CanMsg[{0}].ID = 0x{1}", i, CanMsgBuffer[i].ID.ToString("X8"));
|
||||
//Console.WriteLine("CanMsg[{0}].TimeStamp = {1}",i,CanMsgBuffer[i].TimeStamp);
|
||||
Console.WriteLine("CanMsg[{0}].TimeStamp = {1}", i, CanMsgBuffer[i].TimeStamp);
|
||||
Console.Write("CanMsg[{0}].Data = ", i);
|
||||
for (int j = 0; j < CanMsgBuffer[i].DataLen; j++)
|
||||
{
|
||||
@@ -466,12 +563,13 @@ namespace CapMachine.Wpf.CanDrive
|
||||
}
|
||||
Console.WriteLine("");
|
||||
|
||||
//报文给高速记录的服务
|
||||
HighSpeedDataService.AppendOrUpdateMsg(new Models.HighSpeed.CommMsg()
|
||||
{
|
||||
Category="CAN",
|
||||
MsgInfo="0x"+CanMsgBuffer[i].ID.ToString("X8"),
|
||||
MsgData= CanMsgBuffer[i].Data.ToArrayString(),
|
||||
Time=DateTime.Now.ToString()
|
||||
Category = "CAN",
|
||||
MsgInfo = "0x" + CanMsgBuffer[i].ID.ToString("X8"),
|
||||
MsgData = BitConverter.ToString(CanMsgBuffer[i].Data),
|
||||
Time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -486,7 +584,7 @@ namespace CapMachine.Wpf.CanDrive
|
||||
Console.WriteLine("");
|
||||
|
||||
//将CAN消息数据填充到信号里面,用DBC解析数据
|
||||
CAN_DBCParser.DBC_SyncCANMsgToValue(DBCHandle, msgPt, CanNum);
|
||||
CAN_DBCParser.DBC_SyncCANMsgToValue(DBCHandle, msgPtRead, CanNum);
|
||||
|
||||
//循环获取消息的数据
|
||||
foreach (var item in ListCanDbcModel)
|
||||
@@ -496,10 +594,14 @@ namespace CapMachine.Wpf.CanDrive
|
||||
//{
|
||||
CAN_DBCParser.DBC_GetSignalValueStr(DBCHandle, new StringBuilder(item.MsgName), new StringBuilder(item.SignalName), ValueSb);
|
||||
item.SignalRtValueSb = ValueSb;
|
||||
|
||||
Console.Write(ValueSb.ToString());
|
||||
//}
|
||||
}
|
||||
|
||||
//释放数据缓冲区,必须释放,否则程序运行一段时间后会报内存不足
|
||||
Marshal.FreeHGlobal(msgPtRead);
|
||||
Thread.Sleep(10);
|
||||
|
||||
////获取信号值并打印出来
|
||||
//StringBuilder ValueStr = new StringBuilder(32);
|
||||
//CAN_DBCParser.DBC_GetSignalValueStr(DBCHandle, new StringBuilder("msg_moto_speed"), new StringBuilder("moto_speed"), ValueStr);
|
||||
|
||||
Reference in New Issue
Block a user