From 196df6b181bb2daa09869c363d25d64f95bff269 Mon Sep 17 00:00:00 2001 From: Tyrone CT Date: Thu, 14 May 2026 15:08:34 +0800 Subject: [PATCH] =?UTF-8?q?=E7=8E=B0=E5=9C=BA=E4=BF=AE=E6=94=B96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CapMachine.Wpf/CanDrive/ToomossCan.cs | 98 +++++++++++---------------- CapMachine.Wpf/CanDrive/usb2can.cs | 2 + 2 files changed, 43 insertions(+), 57 deletions(-) diff --git a/CapMachine.Wpf/CanDrive/ToomossCan.cs b/CapMachine.Wpf/CanDrive/ToomossCan.cs index bf74fbf..2b7bbe7 100644 --- a/CapMachine.Wpf/CanDrive/ToomossCan.cs +++ b/CapMachine.Wpf/CanDrive/ToomossCan.cs @@ -398,17 +398,16 @@ namespace CapMachine.Wpf.CanDrive public void SendCanMsg(List CmdData) { 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[8]; - } + int msgCount = GroupMsg.Count(); IntPtr msgPt = IntPtr.Zero; + IntPtr sendBufferPtr = IntPtr.Zero; try { + int canMsgSize = Marshal.SizeOf(typeof(USB2CAN.CAN_MSG)); msgPt = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(USB2CAN.CAN_MSG))); + sendBufferPtr = Marshal.AllocHGlobal(canMsgSize * msgCount); + byte[] frameBytes = new byte[canMsgSize]; int Index = 0; lock (_dbcParserLock) { @@ -420,10 +419,13 @@ namespace CapMachine.Wpf.CanDrive CAN_DBCParser.DBC_SetSignalValue(DBCHandle, new StringBuilder(itemMsg.Key), new StringBuilder(itemSignal.SignalName), itemSignal.SignalCmdValue); } CAN_DBCParser.DBC_SyncValueToCANMsg(DBCHandle, new StringBuilder(itemMsg.Key), msgPt); - CanMsg[Index] = (USB2CAN.CAN_MSG)Marshal.PtrToStructure(msgPt, typeof(USB2CAN.CAN_MSG)); + var targetPtr = IntPtr.Add(sendBufferPtr, Index * canMsgSize); + Marshal.Copy(msgPt, frameBytes, 0, canMsgSize); + Marshal.Copy(frameBytes, 0, targetPtr, canMsgSize); Index++; } } + USB2CAN.CAN_SendMsgByPtr(DevHandle, WriteCANIndex, sendBufferPtr, (uint)msgCount); } finally { @@ -431,17 +433,10 @@ namespace CapMachine.Wpf.CanDrive { Marshal.FreeHGlobal(msgPt); } - } - 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); + if (sendBufferPtr != IntPtr.Zero) + { + Marshal.FreeHGlobal(sendBufferPtr); + } } } @@ -513,17 +508,16 @@ namespace CapMachine.Wpf.CanDrive 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[8]; - } + int msgCount = GroupMsg.Count(); IntPtr msgPtSend = IntPtr.Zero; + IntPtr sendBufferPtr = IntPtr.Zero; try { - msgPtSend = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(USB2CAN.CAN_MSG))); + int canMsgSize = Marshal.SizeOf(typeof(USB2CAN.CAN_MSG)); + msgPtSend = Marshal.AllocHGlobal(canMsgSize); + sendBufferPtr = Marshal.AllocHGlobal(canMsgSize * msgCount); + byte[] frameBytes = new byte[canMsgSize]; int Index = 0; lock (_dbcParserLock) { @@ -535,10 +529,13 @@ namespace CapMachine.Wpf.CanDrive 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)); + var targetPtr = IntPtr.Add(sendBufferPtr, Index * canMsgSize); + Marshal.Copy(msgPtSend, frameBytes, 0, canMsgSize); + Marshal.Copy(frameBytes, 0, targetPtr, canMsgSize); Index++; } } + USB2CAN.CAN_SendMsgByPtr(DevHandle, WriteCANIndex, sendBufferPtr, (uint)msgCount); } finally { @@ -546,16 +543,10 @@ namespace CapMachine.Wpf.CanDrive { Marshal.FreeHGlobal(msgPtSend); } - } - //发送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); + if (sendBufferPtr != IntPtr.Zero) + { + Marshal.FreeHGlobal(sendBufferPtr); + } } } catch (Exception ex) @@ -778,17 +769,16 @@ namespace CapMachine.Wpf.CanDrive { 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[8]; - } + int msgCount = GroupMsg.Count(); IntPtr msgPtSend = IntPtr.Zero; + IntPtr sendBufferPtr = IntPtr.Zero; try { - msgPtSend = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(USB2CAN.CAN_MSG))); + int canMsgSize = Marshal.SizeOf(typeof(USB2CAN.CAN_MSG)); + msgPtSend = Marshal.AllocHGlobal(canMsgSize); + sendBufferPtr = Marshal.AllocHGlobal(canMsgSize * msgCount); + byte[] frameBytes = new byte[canMsgSize]; int Index = 0; lock (_dbcParserLock) { @@ -800,10 +790,13 @@ namespace CapMachine.Wpf.CanDrive 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)); + var targetPtr = IntPtr.Add(sendBufferPtr, Index * canMsgSize); + Marshal.Copy(msgPtSend, frameBytes, 0, canMsgSize); + Marshal.Copy(frameBytes, 0, targetPtr, canMsgSize); Index++; } } + USB2CAN.CAN_SendMsgByPtr(DevHandle, WriteCANIndex, sendBufferPtr, (uint)msgCount); } finally { @@ -811,19 +804,10 @@ namespace CapMachine.Wpf.CanDrive { Marshal.FreeHGlobal(msgPtSend); } - } - - //发送CAN数据 - int SendedNum = USB2CAN.CAN_SendMsg(DevHandle, WriteCANIndex, CanMsg, (uint)CanMsg.Length); - if (SendedNum >= 0) - { - //Console.WriteLine("Success send frames:{0}", SendedNum); - //IsSendOk = true; - } - else - { - //Console.WriteLine("Send CAN data failed! {0}", SendedNum); - //IsSendOk = false; + if (sendBufferPtr != IntPtr.Zero) + { + Marshal.FreeHGlobal(sendBufferPtr); + } } } diff --git a/CapMachine.Wpf/CanDrive/usb2can.cs b/CapMachine.Wpf/CanDrive/usb2can.cs index 672bc5c..7c84ca3 100644 --- a/CapMachine.Wpf/CanDrive/usb2can.cs +++ b/CapMachine.Wpf/CanDrive/usb2can.cs @@ -108,6 +108,8 @@ namespace CapMachine.Wpf.CanDrive public static extern Int32 CAN_StopGetMsg(Int32 DevHandle, Byte CANIndex); [DllImport("USB2XXX.dll")] public static extern Int32 CAN_SendMsg(Int32 DevHandle, Byte CANIndex, CAN_MSG[] pCanSendMsg, UInt32 SendMsgNum); + [DllImport("USB2XXX.dll", EntryPoint = "CAN_SendMsg")] + public static extern Int32 CAN_SendMsgByPtr(Int32 DevHandle, Byte CANIndex, IntPtr pCanSendMsg, UInt32 SendMsgNum); [DllImport("USB2XXX.dll")] public static extern Int32 CAN_GetMsg(Int32 DevHandle, Byte CANIndex, IntPtr pCanGetMsg); [DllImport("USB2XXX.dll")]