现场修改6

This commit is contained in:
2026-05-14 15:08:34 +08:00
parent 873f6ced0a
commit 196df6b181
2 changed files with 43 additions and 57 deletions

View File

@@ -398,17 +398,16 @@ namespace CapMachine.Wpf.CanDrive
public void SendCanMsg(List<CanCmdData> 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);
}
}
}