现场修改6
This commit is contained in:
@@ -398,17 +398,16 @@ namespace CapMachine.Wpf.CanDrive
|
|||||||
public void SendCanMsg(List<CanCmdData> CmdData)
|
public void SendCanMsg(List<CanCmdData> CmdData)
|
||||||
{
|
{
|
||||||
var GroupMsg = CmdData.GroupBy(x => x.MsgName);
|
var GroupMsg = CmdData.GroupBy(x => x.MsgName);
|
||||||
USB2CAN.CAN_MSG[] CanMsg = new USB2CAN.CAN_MSG[GroupMsg.Count()];
|
int msgCount = GroupMsg.Count();
|
||||||
for (int i = 0; i < GroupMsg.Count(); i++)
|
|
||||||
{
|
|
||||||
CanMsg[i] = new USB2CAN.CAN_MSG();
|
|
||||||
CanMsg[i].Data = new Byte[8];
|
|
||||||
}
|
|
||||||
|
|
||||||
IntPtr msgPt = IntPtr.Zero;
|
IntPtr msgPt = IntPtr.Zero;
|
||||||
|
IntPtr sendBufferPtr = IntPtr.Zero;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
int canMsgSize = Marshal.SizeOf(typeof(USB2CAN.CAN_MSG));
|
||||||
msgPt = Marshal.AllocHGlobal(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;
|
int Index = 0;
|
||||||
lock (_dbcParserLock)
|
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_SetSignalValue(DBCHandle, new StringBuilder(itemMsg.Key), new StringBuilder(itemSignal.SignalName), itemSignal.SignalCmdValue);
|
||||||
}
|
}
|
||||||
CAN_DBCParser.DBC_SyncValueToCANMsg(DBCHandle, new StringBuilder(itemMsg.Key), msgPt);
|
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++;
|
Index++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
USB2CAN.CAN_SendMsgByPtr(DevHandle, WriteCANIndex, sendBufferPtr, (uint)msgCount);
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
@@ -431,17 +433,10 @@ namespace CapMachine.Wpf.CanDrive
|
|||||||
{
|
{
|
||||||
Marshal.FreeHGlobal(msgPt);
|
Marshal.FreeHGlobal(msgPt);
|
||||||
}
|
}
|
||||||
}
|
if (sendBufferPtr != IntPtr.Zero)
|
||||||
Console.WriteLine("");
|
{
|
||||||
//发送CAN数据
|
Marshal.FreeHGlobal(sendBufferPtr);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -513,17 +508,16 @@ namespace CapMachine.Wpf.CanDrive
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
var GroupMsg = CmdData.GroupBy(x => x.MsgName);
|
var GroupMsg = CmdData.GroupBy(x => x.MsgName);
|
||||||
USB2CAN.CAN_MSG[] CanMsg = new USB2CAN.CAN_MSG[GroupMsg.Count()];
|
int msgCount = GroupMsg.Count();
|
||||||
for (int i = 0; i < GroupMsg.Count(); i++)
|
|
||||||
{
|
|
||||||
CanMsg[i] = new USB2CAN.CAN_MSG();
|
|
||||||
CanMsg[i].Data = new Byte[8];
|
|
||||||
}
|
|
||||||
|
|
||||||
IntPtr msgPtSend = IntPtr.Zero;
|
IntPtr msgPtSend = IntPtr.Zero;
|
||||||
|
IntPtr sendBufferPtr = IntPtr.Zero;
|
||||||
try
|
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;
|
int Index = 0;
|
||||||
lock (_dbcParserLock)
|
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_SetSignalValue(DBCHandle, new StringBuilder(itemMsg.Key), new StringBuilder(itemSignal.SignalName), itemSignal.SignalCmdValue);
|
||||||
}
|
}
|
||||||
CAN_DBCParser.DBC_SyncValueToCANMsg(DBCHandle, new StringBuilder(itemMsg.Key), msgPtSend);
|
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++;
|
Index++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
USB2CAN.CAN_SendMsgByPtr(DevHandle, WriteCANIndex, sendBufferPtr, (uint)msgCount);
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
@@ -546,16 +543,10 @@ namespace CapMachine.Wpf.CanDrive
|
|||||||
{
|
{
|
||||||
Marshal.FreeHGlobal(msgPtSend);
|
Marshal.FreeHGlobal(msgPtSend);
|
||||||
}
|
}
|
||||||
}
|
if (sendBufferPtr != IntPtr.Zero)
|
||||||
//发送CAN数据
|
{
|
||||||
int SendedNum = USB2CAN.CAN_SendMsg(DevHandle, WriteCANIndex, CanMsg, (uint)CanMsg.Length);
|
Marshal.FreeHGlobal(sendBufferPtr);
|
||||||
if (SendedNum >= 0)
|
}
|
||||||
{
|
|
||||||
//Console.WriteLine("Success send frames:{0}", SendedNum);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
//Console.WriteLine("Send CAN data failed! {0}", SendedNum);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
@@ -778,17 +769,16 @@ namespace CapMachine.Wpf.CanDrive
|
|||||||
{
|
{
|
||||||
|
|
||||||
var GroupMsg = CmdData.GroupBy(x => x.MsgName);
|
var GroupMsg = CmdData.GroupBy(x => x.MsgName);
|
||||||
USB2CAN.CAN_MSG[] CanMsg = new USB2CAN.CAN_MSG[GroupMsg.Count()];
|
int msgCount = GroupMsg.Count();
|
||||||
for (int i = 0; i < GroupMsg.Count(); i++)
|
|
||||||
{
|
|
||||||
CanMsg[i] = new USB2CAN.CAN_MSG();
|
|
||||||
CanMsg[i].Data = new Byte[8];
|
|
||||||
}
|
|
||||||
|
|
||||||
IntPtr msgPtSend = IntPtr.Zero;
|
IntPtr msgPtSend = IntPtr.Zero;
|
||||||
|
IntPtr sendBufferPtr = IntPtr.Zero;
|
||||||
try
|
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;
|
int Index = 0;
|
||||||
lock (_dbcParserLock)
|
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_SetSignalValue(DBCHandle, new StringBuilder(itemMsg.Key), new StringBuilder(itemSignal.SignalName), itemSignal.SignalCmdValue);
|
||||||
}
|
}
|
||||||
CAN_DBCParser.DBC_SyncValueToCANMsg(DBCHandle, new StringBuilder(itemMsg.Key), msgPtSend);
|
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++;
|
Index++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
USB2CAN.CAN_SendMsgByPtr(DevHandle, WriteCANIndex, sendBufferPtr, (uint)msgCount);
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
@@ -811,19 +804,10 @@ namespace CapMachine.Wpf.CanDrive
|
|||||||
{
|
{
|
||||||
Marshal.FreeHGlobal(msgPtSend);
|
Marshal.FreeHGlobal(msgPtSend);
|
||||||
}
|
}
|
||||||
}
|
if (sendBufferPtr != IntPtr.Zero)
|
||||||
|
{
|
||||||
//发送CAN数据
|
Marshal.FreeHGlobal(sendBufferPtr);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -108,6 +108,8 @@ namespace CapMachine.Wpf.CanDrive
|
|||||||
public static extern Int32 CAN_StopGetMsg(Int32 DevHandle, Byte CANIndex);
|
public static extern Int32 CAN_StopGetMsg(Int32 DevHandle, Byte CANIndex);
|
||||||
[DllImport("USB2XXX.dll")]
|
[DllImport("USB2XXX.dll")]
|
||||||
public static extern Int32 CAN_SendMsg(Int32 DevHandle, Byte CANIndex, CAN_MSG[] pCanSendMsg, UInt32 SendMsgNum);
|
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")]
|
[DllImport("USB2XXX.dll")]
|
||||||
public static extern Int32 CAN_GetMsg(Int32 DevHandle, Byte CANIndex, IntPtr pCanGetMsg);
|
public static extern Int32 CAN_GetMsg(Int32 DevHandle, Byte CANIndex, IntPtr pCanGetMsg);
|
||||||
[DllImport("USB2XXX.dll")]
|
[DllImport("USB2XXX.dll")]
|
||||||
|
|||||||
Reference in New Issue
Block a user