public static void LIN_SendMsgThread(int DevHandle, byte Channel) { while (LIN_SendMsgFlag[Channel]) { /************************************主机写数据************************************/ int MsgIndex = 0; Byte[] DataBuffer; USB2LIN_EX.LIN_EX_MSG[] LINMsg = new USB2LIN_EX.LIN_EX_MSG[5]; USB2LIN_EX.LIN_EX_MSG[] LINOutMsg = new USB2LIN_EX.LIN_EX_MSG[10]; //添加第一帧数据 LINMsg[MsgIndex] = new USB2LIN_EX.LIN_EX_MSG(); LINMsg[MsgIndex].MsgType = USB2LIN_EX.LIN_EX_MSG_TYPE_BK; //只发送BREAK信号,一般用于唤醒休眠中的从设备 LINMsg[MsgIndex].Timestamp = 10; //发送该帧数据之后的延时时间,最小建议设置为1 MsgIndex++; //添加第二帧数据 LINMsg[MsgIndex] = new USB2LIN_EX.LIN_EX_MSG(); LINMsg[MsgIndex].MsgType = USB2LIN_EX.LIN_EX_MSG_TYPE_MW; //主机发送数据 LINMsg[MsgIndex].DataLen = 8; //实际要发送的数据字节数 LINMsg[MsgIndex].Timestamp = 10; //发送该帧数据之后的延时时间 LINMsg[MsgIndex].CheckType = USB2LIN_EX.LIN_EX_CHECK_EXT; //增强校验 LINMsg[MsgIndex].PID = 0x32; //可以只传入ID,校验位底层会自动计算 LINMsg[MsgIndex].Data = new Byte[8]; //必须分配8字节空间 DataBuffer = new Byte[8] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }; //根据自己实际情况修改数据 for (int i = 0; i < LINMsg[MsgIndex].DataLen; i++) //循环填充8字节数据 { LINMsg[MsgIndex].Data[i] = DataBuffer[i]; } MsgIndex++; //添加第三帧数据 LINMsg[MsgIndex] = new USB2LIN_EX.LIN_EX_MSG(); LINMsg[MsgIndex].MsgType = USB2LIN_EX.LIN_EX_MSG_TYPE_MW; //主机发送数据 LINMsg[MsgIndex].DataLen = 8; //实际要发送的数据字节数 LINMsg[MsgIndex].Timestamp = 10; //发送该帧数据之后的延时时间 LINMsg[MsgIndex].CheckType = USB2LIN_EX.LIN_EX_CHECK_EXT; //增强校验 LINMsg[MsgIndex].PID = 0x32; //可以只传入ID,校验位底层会自动计算 LINMsg[MsgIndex].Data = new Byte[8]; //必须分配8字节空间 DataBuffer = new Byte[8] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }; //根据自己实际情况修改数据 for (int i = 0; i < LINMsg[MsgIndex].DataLen; i++) //循环填充8字节数据 { LINMsg[MsgIndex].Data[i] = DataBuffer[i]; } MsgIndex++; /********************需要发送更多帧数据,请按照前面的方式继续添加********************/ //将数组转换成指针 IntPtr pt = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(USB2LIN_EX.LIN_EX_MSG)) * MsgIndex); int ret = USB2LIN_EX.LIN_EX_MasterSync(DevHandle, Channel, LINMsg, pt, MsgIndex); if (ret < USB2LIN_EX.LIN_EX_SUCCESS) { Console.WriteLine("MasterSync LIN failed!"); //释放内存 Marshal.FreeHGlobal(pt); return; } else { //主机发送数据成功后,也会接收到发送出去的数据,通过接收回来的数据跟发送出去的数据对比,可以判断发送数据的时候,数据是否被冲突 Console.WriteLine("MsgLen = {0}", ret); for (int i = 0; i < ret; i++) { LINOutMsg[i] = (USB2LIN_EX.LIN_EX_MSG)Marshal.PtrToStructure((IntPtr)((UInt32)pt + i * Marshal.SizeOf(typeof(USB2LIN_EX.LIN_EX_MSG))), typeof(USB2LIN_EX.LIN_EX_MSG)); Console.Write("{0} SYNC[{1:X2}] PID[{2:X2}] ", MSGTypeStr[LINOutMsg[i].MsgType], LINOutMsg[i].Sync, LINOutMsg[i].PID); for (int j = 0; j < LINOutMsg[i].DataLen; j++) { Console.Write("{0:X2} ", LINOutMsg[i].Data[j]); } Console.WriteLine("[{0}][{1:X2}] [{2}:{3}:{4}.{5}]", CKTypeStr[LINOutMsg[i].CheckType], LINOutMsg[i].Check, (LINOutMsg[i].Timestamp / 36000000) % 60, (LINOutMsg[i].Timestamp / 600000) % 60, (LINOutMsg[i].Timestamp / 10000) % 60, (LINOutMsg[i].Timestamp / 10) % 1000); } } //释放内存 Marshal.FreeHGlobal(pt); Thread.Sleep(1000); } }
static void Main(string[] args) { usb_device.DEVICE_INFO DevInfo = new usb_device.DEVICE_INFO(); Int32[] DevHandles = new Int32[20]; Int32 DevHandle = 0; bool state; Int32 DevNum, ret = 0; //扫描查找设备 DevNum = usb_device.USB_ScanDevice(DevHandles); if (DevNum <= 0) { Console.WriteLine("No device connected!"); return; } else { Console.WriteLine("Have {0} device connected!", DevNum); } DevHandle = DevHandles[0]; //打开设备 state = usb_device.USB_OpenDevice(DevHandle); if (!state) { Console.WriteLine("Open device error!"); return; } else { Console.WriteLine("Open device success!"); } //获取固件信息 StringBuilder FuncStr = new StringBuilder(256); state = usb_device.DEV_GetDeviceInfo(DevHandle, ref DevInfo, FuncStr); if (!state) { Console.WriteLine("Get device infomation error!"); return; } else { Console.WriteLine("Firmware Info:"); Console.WriteLine(" Name:" + Encoding.Default.GetString(DevInfo.FirmwareName)); Console.WriteLine(" Build Date:" + Encoding.Default.GetString(DevInfo.BuildDate)); Console.WriteLine(" Firmware Version:v{0}.{1}.{2}", (DevInfo.FirmwareVersion >> 24) & 0xFF, (DevInfo.FirmwareVersion >> 16) & 0xFF, DevInfo.FirmwareVersion & 0xFFFF); Console.WriteLine(" Hardware Version:v{0}.{1}.{2}", (DevInfo.HardwareVersion >> 24) & 0xFF, (DevInfo.HardwareVersion >> 16) & 0xFF, DevInfo.HardwareVersion & 0xFFFF); Console.WriteLine(" Functions:" + DevInfo.Functions.ToString("X8")); Console.WriteLine(" Functions String:" + FuncStr); } //初始化配置LIN #if LIN_MASTER_TEST ret = USB2LIN_EX.LIN_EX_Init(DevHandle, 0, 19200, 1); #else ret = USB2LIN_EX.LIN_EX_Init(DevHandle, 0, 19200, 0); #endif if (ret != USB2LIN_EX.LIN_EX_SUCCESS) { Console.WriteLine("Config LIN failed!"); return; } else { Console.WriteLine("Config LIN Success!"); } #if LIN_MASTER_TEST ret = USB2LIN_EX.LIN_EX_Init(DevHandle, 1, 19200, 1); #else ret = USB2LIN_EX.LIN_EX_Init(DevHandle, 1, 19200, 0); #endif if (ret != USB2LIN_EX.LIN_EX_SUCCESS) { Console.WriteLine("Config LIN failed!"); return; } else { Console.WriteLine("Config LIN Success!"); } #if LIN_MASTER_TEST //启动多线程写数据 Thread LIN1SendMsgThread = new Thread(() => LIN_SendMsgThread(DevHandle, 0)); Thread LIN2SendMsgThread = new Thread(() => LIN_SendMsgThread(DevHandle, 1)); LIN_SendMsgFlag[0] = true; LIN_SendMsgFlag[1] = true; LIN1SendMsgThread.Start(); LIN2SendMsgThread.Start(); //按下回车后结束发送接收线程 Console.ReadLine(); LIN_SendMsgFlag[0] = false; LIN_SendMsgFlag[1] = false; LIN1SendMsgThread.Join(); LIN2SendMsgThread.Join(); #else /************************************从机发送数据************************************/ int MsgIndex = 0; Byte[] DataBuffer; //设置ID为LIN_EX_MSG_TYPE_SW模式,从机接收到主机发送的帧头后就会返回预先定义好的数据 USB2LIN_EX.LIN_EX_MSG[] LINSlaveMsg = new USB2LIN_EX.LIN_EX_MSG[3]; //配置第一帧数据 LINSlaveMsg[MsgIndex] = new USB2LIN_EX.LIN_EX_MSG(); LINSlaveMsg[MsgIndex].MsgType = USB2LIN_EX.LIN_EX_MSG_TYPE_SW; //从机发送数据模式 LINSlaveMsg[MsgIndex].CheckType = USB2LIN_EX.LIN_EX_CHECK_EXT; //配置为增强校验 LINSlaveMsg[MsgIndex].PID = 0x34; //可以只传入ID,校验位底层会自动计算 LINSlaveMsg[MsgIndex].Data = new Byte[8]; //必须分配8字节空间 LINSlaveMsg[MsgIndex].DataLen = 8; //实际要发送的数据字节数 DataBuffer = new Byte[8] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }; //从机返回的数据,根据自己实际情况修改数据 for (int i = 0; i < LINSlaveMsg[MsgIndex].DataLen; i++) //循环填充8字节数据 { LINSlaveMsg[MsgIndex].Data[i] = DataBuffer[i]; } MsgIndex++; //配置第二帧数据 LINSlaveMsg[MsgIndex] = new USB2LIN_EX.LIN_EX_MSG(); LINSlaveMsg[MsgIndex].MsgType = USB2LIN_EX.LIN_EX_MSG_TYPE_SW; //从机发送数据模式 LINSlaveMsg[MsgIndex].CheckType = USB2LIN_EX.LIN_EX_CHECK_EXT; //配置为增强校验 LINSlaveMsg[MsgIndex].PID = 0x35; //可以只传入ID,校验位底层会自动计算 LINSlaveMsg[MsgIndex].Data = new Byte[8]; //必须分配8字节空间 LINSlaveMsg[MsgIndex].DataLen = 8; //实际要发送的数据字节数 DataBuffer = new Byte[8] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }; //从机返回的数据,根据自己实际情况修改数据 for (int i = 0; i < LINSlaveMsg[MsgIndex].DataLen; i++) //循环填充8字节数据 { LINSlaveMsg[MsgIndex].Data[i] = DataBuffer[i]; } MsgIndex++; //配置第三帧数据 LINSlaveMsg[MsgIndex] = new USB2LIN_EX.LIN_EX_MSG(); LINSlaveMsg[MsgIndex].MsgType = USB2LIN_EX.LIN_EX_MSG_TYPE_SW; //从机发送数据模式 LINSlaveMsg[MsgIndex].CheckType = USB2LIN_EX.LIN_EX_CHECK_EXT; //配置为增强校验 LINSlaveMsg[MsgIndex].PID = 0x36; //可以只传入ID,校验位底层会自动计算 LINSlaveMsg[MsgIndex].Data = new Byte[8]; //必须分配8字节空间 LINSlaveMsg[MsgIndex].DataLen = 8; //实际要发送的数据字节数 DataBuffer = new Byte[8] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }; //从机返回的数据,根据自己实际情况修改数据 for (int i = 0; i < LINSlaveMsg[MsgIndex].DataLen; i++) //循环填充8字节数据 { LINSlaveMsg[MsgIndex].Data[i] = DataBuffer[i]; } MsgIndex++; /********************需要配置更多帧,请按照前面的方式继续添加********************/ //默认所有ID都是从机接收数据模式,该模式下可以用于数据的监听 //调用该函数后,只会修改传入对应ID的模式,其他的不会被改变 ret = USB2LIN_EX.LIN_EX_SlaveSetIDMode(DevHandle, LINIndex, LINSlaveMsg, MsgIndex); if (ret != USB2LIN_EX.LIN_EX_SUCCESS) { Console.WriteLine("Config LIN ID Mode failed!"); return; } else { Console.WriteLine("Config LIN ID Mode Success!"); } //循环获取接收到的数据,该操作可以用作LIN总线数据监控 Console.WriteLine("Start Get LIN Data..."); int RunTimeMs = 10000;//只运行10秒钟,10秒钟后结束数据监听,可自行修改 while (RunTimeMs > 0) { USB2LIN_EX.LIN_EX_MSG[] LINMsg = new USB2LIN_EX.LIN_EX_MSG[1024];//缓冲区尽量大一点,防止益处 //将数组转换成指针 IntPtr pt = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(USB2LIN_EX.LIN_EX_MSG)) * LINMsg.Length); ret = USB2LIN_EX.LIN_EX_SlaveGetData(DevHandle, LINIndex, pt); for (int i = 0; i < ret; i++) { LINMsg[i] = (USB2LIN_EX.LIN_EX_MSG)Marshal.PtrToStructure((IntPtr)((UInt32)pt + i * Marshal.SizeOf(typeof(USB2LIN_EX.LIN_EX_MSG))), typeof(USB2LIN_EX.LIN_EX_MSG)); Console.WriteLine("{0} SYNC[{1:X2}] PID[{2:X2}] ", MSGTypeStr[LINMsg[i].MsgType], LINMsg[i].Sync, LINMsg[i].PID); for (int j = 0; j < LINMsg[i].DataLen; j++) { Console.Write("{0:X2} ", LINMsg[i].Data[j]); } Console.WriteLine("[{0}][{1:X2}] [{2}]:{3}]:{4}].{5}]]\n", CKTypeStr[LINMsg[i].CheckType], LINMsg[i].Check, (LINMsg[i].Timestamp / 36000000) % 60, (LINMsg[i].Timestamp / 600000) % 60, (LINMsg[i].Timestamp / 10000) % 60, (LINMsg[i].Timestamp / 10) % 1000); } //释放内存 Marshal.FreeHGlobal(pt); System.Threading.Thread.Sleep(10); RunTimeMs -= 10; } #endif Console.WriteLine("Close Device!"); //关闭设备 usb_device.USB_CloseDevice(DevHandle); return; }
static void Main(string[] args) { usb_device.DEVICE_INFO DevInfo = new usb_device.DEVICE_INFO(); Int32[] DevHandles = new Int32[20]; Int32 DevHandle = 0; Byte LINIndex = 0; bool state; Int32 DevNum, ret = 0; String[] MSGTypeStr = new String[10] { "UN", "MW", "MR", "SW", "SR", "BK", "SY", "ID", "DT", "CK" }; String[] CKTypeStr = new String[5] { "STD", "EXT", "USER", "NONE", "ERROR" }; //扫描查找设备 DevNum = usb_device.USB_ScanDevice(DevHandles); if (DevNum <= 0) { Console.WriteLine("No device connected!"); return; } else { Console.WriteLine("Have {0} device connected!", DevNum); } DevHandle = DevHandles[0]; //打开设备 state = usb_device.USB_OpenDevice(DevHandle); if (!state) { Console.WriteLine("Open device error!"); return; } else { Console.WriteLine("Open device success!"); } //获取固件信息 StringBuilder FuncStr = new StringBuilder(256); state = usb_device.DEV_GetDeviceInfo(DevHandle, ref DevInfo, FuncStr); if (!state) { Console.WriteLine("Get device infomation error!"); return; } else { Console.WriteLine("Firmware Info:"); Console.WriteLine(" Name:" + Encoding.Default.GetString(DevInfo.FirmwareName)); Console.WriteLine(" Build Date:" + Encoding.Default.GetString(DevInfo.BuildDate)); Console.WriteLine(" Firmware Version:v{0}.{1}.{2}", (DevInfo.FirmwareVersion >> 24) & 0xFF, (DevInfo.FirmwareVersion >> 16) & 0xFF, DevInfo.FirmwareVersion & 0xFFFF); Console.WriteLine(" Hardware Version:v{0}.{1}.{2}", (DevInfo.HardwareVersion >> 24) & 0xFF, (DevInfo.HardwareVersion >> 16) & 0xFF, DevInfo.HardwareVersion & 0xFFFF); Console.WriteLine(" Functions:" + DevInfo.Functions.ToString("X8")); Console.WriteLine(" Functions String:" + FuncStr); } //初始化配置LIN #if LIN_MASTER_TEST ret = USB2LIN_EX.LIN_EX_Init(DevHandle, LINIndex, 19200, 1); #else ret = USB2LIN_EX.LIN_EX_Init(DevHandle, LINIndex, 19200, 0); #endif if (ret != USB2LIN_EX.LIN_EX_SUCCESS) { Console.WriteLine("Config LIN failed!"); return; } else { Console.WriteLine("Config LIN Success!"); } #if LIN_MASTER_TEST /************************************主机写数据************************************/ int MsgIndex = 0; Byte[] DataBuffer; USB2LIN_EX.LIN_EX_MSG[] LINMsg = new USB2LIN_EX.LIN_EX_MSG[5]; USB2LIN_EX.LIN_EX_MSG[] LINOutMsg = new USB2LIN_EX.LIN_EX_MSG[10]; //添加第一帧数据 LINMsg[MsgIndex] = new USB2LIN_EX.LIN_EX_MSG(); LINMsg[MsgIndex].MsgType = USB2LIN_EX.LIN_EX_MSG_TYPE_BK; //只发送BREAK信号,一般用于唤醒休眠中的从设备 LINMsg[MsgIndex].Timestamp = 10; //发送该帧数据之后的延时时间,最小建议设置为1 MsgIndex++; //添加第二帧数据 LINMsg[MsgIndex] = new USB2LIN_EX.LIN_EX_MSG(); LINMsg[MsgIndex].MsgType = USB2LIN_EX.LIN_EX_MSG_TYPE_MW; //主机发送数据 LINMsg[MsgIndex].DataLen = 8; //实际要发送的数据字节数 LINMsg[MsgIndex].Timestamp = 10; //发送该帧数据之后的延时时间 LINMsg[MsgIndex].CheckType = USB2LIN_EX.LIN_EX_CHECK_EXT; //增强校验 LINMsg[MsgIndex].PID = 0x32; //可以只传入ID,校验位底层会自动计算 LINMsg[MsgIndex].Data = new Byte[8]; //必须分配8字节空间 DataBuffer = new Byte[8] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }; //根据自己实际情况修改数据 for (int i = 0; i < LINMsg[MsgIndex].DataLen; i++) //循环填充8字节数据 { LINMsg[MsgIndex].Data[i] = DataBuffer[i]; } MsgIndex++; //添加第三帧数据 LINMsg[MsgIndex] = new USB2LIN_EX.LIN_EX_MSG(); LINMsg[MsgIndex].MsgType = USB2LIN_EX.LIN_EX_MSG_TYPE_MW; //主机发送数据 LINMsg[MsgIndex].DataLen = 8; //实际要发送的数据字节数 LINMsg[MsgIndex].Timestamp = 10; //发送该帧数据之后的延时时间 LINMsg[MsgIndex].CheckType = USB2LIN_EX.LIN_EX_CHECK_EXT; //增强校验 LINMsg[MsgIndex].PID = 0x32; //可以只传入ID,校验位底层会自动计算 LINMsg[MsgIndex].Data = new Byte[8]; //必须分配8字节空间 DataBuffer = new Byte[8] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }; //根据自己实际情况修改数据 for (int i = 0; i < LINMsg[MsgIndex].DataLen; i++) //循环填充8字节数据 { LINMsg[MsgIndex].Data[i] = DataBuffer[i]; } MsgIndex++; /********************需要发送更多帧数据,请按照前面的方式继续添加********************/ //将数组转换成指针 IntPtr pt = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(USB2LIN_EX.LIN_EX_MSG)) * MsgIndex); ret = USB2LIN_EX.LIN_EX_MasterSync(DevHandle, LINIndex, LINMsg, pt, MsgIndex); if (ret < USB2LIN_EX.LIN_EX_SUCCESS) { Console.WriteLine("MasterSync LIN failed!"); //释放内存 Marshal.FreeHGlobal(pt); return; } else { //主机发送数据成功后,也会接收到发送出去的数据,通过接收回来的数据跟发送出去的数据对比,可以判断发送数据的时候,数据是否被冲突 Console.WriteLine("MsgLen = {0}", ret); for (int i = 0; i < ret; i++) { LINOutMsg[i] = (USB2LIN_EX.LIN_EX_MSG)Marshal.PtrToStructure((IntPtr)((UInt32)pt + i * Marshal.SizeOf(typeof(USB2LIN_EX.LIN_EX_MSG))), typeof(USB2LIN_EX.LIN_EX_MSG)); Console.Write("{0} SYNC[{1:X2}] PID[{2:X2}] ", MSGTypeStr[LINOutMsg[i].MsgType], LINOutMsg[i].Sync, LINOutMsg[i].PID); for (int j = 0; j < LINOutMsg[i].DataLen; j++) { Console.Write("{0:X2} ", LINOutMsg[i].Data[j]); } Console.WriteLine("[{0}][{1:X2}] [{2}:{3}:{4}.{5}]", CKTypeStr[LINOutMsg[i].CheckType], LINOutMsg[i].Check, (LINOutMsg[i].Timestamp / 36000000) % 60, (LINOutMsg[i].Timestamp / 600000) % 60, (LINOutMsg[i].Timestamp / 10000) % 60, (LINOutMsg[i].Timestamp / 10) % 1000); } } //释放内存 Marshal.FreeHGlobal(pt); /************************************主机读数据************************************/ MsgIndex = 0; //添加第一帧数据 LINMsg[MsgIndex] = new USB2LIN_EX.LIN_EX_MSG(); LINMsg[MsgIndex].MsgType = USB2LIN_EX.LIN_EX_MSG_TYPE_BK; //只发送BREAK信号,一般用于唤醒休眠中的从设备 LINMsg[MsgIndex].Timestamp = 10; //发送该帧数据之后的延时时间,最小建议设置为1 MsgIndex++; //添加第二帧数据 LINMsg[MsgIndex] = new USB2LIN_EX.LIN_EX_MSG(); LINMsg[MsgIndex].MsgType = USB2LIN_EX.LIN_EX_MSG_TYPE_MR; //主机读数据 LINMsg[MsgIndex].Timestamp = 10; //发送该帧数据之后的延时时间 LINMsg[MsgIndex].PID = 0x33; //可以只传入ID,校验位底层会自动计算 MsgIndex++; //添加第三帧数据 LINMsg[MsgIndex] = new USB2LIN_EX.LIN_EX_MSG(); LINMsg[MsgIndex].MsgType = USB2LIN_EX.LIN_EX_MSG_TYPE_MR; //主机读数据 LINMsg[MsgIndex].Timestamp = 10; //发送该帧数据之后的延时时间 LINMsg[MsgIndex].PID = 0x33; //可以只传入ID,校验位底层会自动计算 MsgIndex++; /********************需要发送更多帧数据,请按照前面的方式继续添加********************/ //将数组转换成指针 pt = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(USB2LIN_EX.LIN_EX_MSG)) * MsgIndex); ret = USB2LIN_EX.LIN_EX_MasterSync(DevHandle, LINIndex, LINMsg, pt, MsgIndex); if (ret < USB2LIN_EX.LIN_EX_SUCCESS) { Console.WriteLine("MasterSync LIN failed!"); //释放内存 Marshal.FreeHGlobal(pt); return; } else { //主机发送数据成功后,也会接收到发送出去的数据,通过接收回来的数据跟发送出去的数据对比,可以判断发送数据的时候,数据是否被冲突 Console.WriteLine("MsgLen = {0}", ret); for (int i = 0; i < ret; i++) { LINOutMsg[i] = (USB2LIN_EX.LIN_EX_MSG)Marshal.PtrToStructure((IntPtr)((UInt32)pt + i * Marshal.SizeOf(typeof(USB2LIN_EX.LIN_EX_MSG))), typeof(USB2LIN_EX.LIN_EX_MSG)); Console.Write("{0} SYNC[{1:X2}] PID[{2:X2}] ", MSGTypeStr[LINOutMsg[i].MsgType], LINOutMsg[i].Sync, LINOutMsg[i].PID); for (int j = 0; j < LINOutMsg[i].DataLen; j++)//实际读取到的字节数据是LINOutMsg[i].DataLen的值 { Console.Write("{0:X2} ", LINOutMsg[i].Data[j]); } Console.WriteLine("[{0}][{1:X2}] [{2}:{3}:{4}.{5}]", CKTypeStr[LINOutMsg[i].CheckType], LINOutMsg[i].Check, (LINOutMsg[i].Timestamp / 36000000) % 60, (LINOutMsg[i].Timestamp / 600000) % 60, (LINOutMsg[i].Timestamp / 10000) % 60, (LINOutMsg[i].Timestamp / 10) % 1000); } } //释放内存 Marshal.FreeHGlobal(pt); #else /************************************从机发送数据************************************/ int MsgIndex = 0; Byte[] DataBuffer; //设置ID为LIN_EX_MSG_TYPE_SW模式,从机接收到主机发送的帧头后就会返回预先定义好的数据 USB2LIN_EX.LIN_EX_MSG[] LINSlaveMsg = new USB2LIN_EX.LIN_EX_MSG[3]; //配置第一帧数据 LINSlaveMsg[MsgIndex] = new USB2LIN_EX.LIN_EX_MSG(); LINSlaveMsg[MsgIndex].MsgType = USB2LIN_EX.LIN_EX_MSG_TYPE_SW; //从机发送数据模式 LINSlaveMsg[MsgIndex].CheckType = USB2LIN_EX.LIN_EX_CHECK_EXT; //配置为增强校验 LINSlaveMsg[MsgIndex].PID = 0x34; //可以只传入ID,校验位底层会自动计算 LINSlaveMsg[MsgIndex].Data = new Byte[8]; //必须分配8字节空间 LINSlaveMsg[MsgIndex].DataLen = 8; //实际要发送的数据字节数 DataBuffer = new Byte[8] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }; //从机返回的数据,根据自己实际情况修改数据 for (int i = 0; i < LINSlaveMsg[MsgIndex].DataLen; i++) //循环填充8字节数据 { LINSlaveMsg[MsgIndex].Data[i] = DataBuffer[i]; } MsgIndex++; //配置第二帧数据 LINSlaveMsg[MsgIndex] = new USB2LIN_EX.LIN_EX_MSG(); LINSlaveMsg[MsgIndex].MsgType = USB2LIN_EX.LIN_EX_MSG_TYPE_SW; //从机发送数据模式 LINSlaveMsg[MsgIndex].CheckType = USB2LIN_EX.LIN_EX_CHECK_EXT; //配置为增强校验 LINSlaveMsg[MsgIndex].PID = 0x35; //可以只传入ID,校验位底层会自动计算 LINSlaveMsg[MsgIndex].Data = new Byte[8]; //必须分配8字节空间 LINSlaveMsg[MsgIndex].DataLen = 8; //实际要发送的数据字节数 DataBuffer = new Byte[8] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }; //从机返回的数据,根据自己实际情况修改数据 for (int i = 0; i < LINSlaveMsg[MsgIndex].DataLen; i++) //循环填充8字节数据 { LINSlaveMsg[MsgIndex].Data[i] = DataBuffer[i]; } MsgIndex++; //配置第三帧数据 LINSlaveMsg[MsgIndex] = new USB2LIN_EX.LIN_EX_MSG(); LINSlaveMsg[MsgIndex].MsgType = USB2LIN_EX.LIN_EX_MSG_TYPE_SW; //从机发送数据模式 LINSlaveMsg[MsgIndex].CheckType = USB2LIN_EX.LIN_EX_CHECK_EXT; //配置为增强校验 LINSlaveMsg[MsgIndex].PID = 0x36; //可以只传入ID,校验位底层会自动计算 LINSlaveMsg[MsgIndex].Data = new Byte[8]; //必须分配8字节空间 LINSlaveMsg[MsgIndex].DataLen = 8; //实际要发送的数据字节数 DataBuffer = new Byte[8] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }; //从机返回的数据,根据自己实际情况修改数据 for (int i = 0; i < LINSlaveMsg[MsgIndex].DataLen; i++) //循环填充8字节数据 { LINSlaveMsg[MsgIndex].Data[i] = DataBuffer[i]; } MsgIndex++; /********************需要配置更多帧,请按照前面的方式继续添加********************/ //默认所有ID都是从机接收数据模式,该模式下可以用于数据的监听 //调用该函数后,只会修改传入对应ID的模式,其他的不会被改变 ret = USB2LIN_EX.LIN_EX_SlaveSetIDMode(DevHandle, LINIndex, LINSlaveMsg, MsgIndex); if (ret != USB2LIN_EX.LIN_EX_SUCCESS) { Console.WriteLine("Config LIN ID Mode failed!"); return; } else { Console.WriteLine("Config LIN ID Mode Success!"); } //循环获取接收到的数据,该操作可以用作LIN总线数据监控 Console.WriteLine("Start Get LIN Data..."); int RunTimeMs = 10000;//只运行10秒钟,10秒钟后结束数据监听,可自行修改 while (RunTimeMs > 0) { USB2LIN_EX.LIN_EX_MSG[] LINMsg = new USB2LIN_EX.LIN_EX_MSG[1024];//缓冲区尽量大一点,防止益处 //将数组转换成指针 IntPtr pt = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(USB2LIN_EX.LIN_EX_MSG)) * LINMsg.Length); ret = USB2LIN_EX.LIN_EX_SlaveGetData(DevHandle, LINIndex, pt); for (int i = 0; i < ret; i++) { LINMsg[i] = (USB2LIN_EX.LIN_EX_MSG)Marshal.PtrToStructure((IntPtr)((UInt32)pt + i * Marshal.SizeOf(typeof(USB2LIN_EX.LIN_EX_MSG))), typeof(USB2LIN_EX.LIN_EX_MSG)); Console.WriteLine("{0} SYNC[{1:X2}] PID[{2:X2}] ", MSGTypeStr[LINMsg[i].MsgType], LINMsg[i].Sync, LINMsg[i].PID); for (int j = 0; j < LINMsg[i].DataLen; j++) { Console.Write("{0:X2} ", LINMsg[i].Data[j]); } Console.WriteLine("[{0}][{1:X2}] [{2}]:{3}]:{4}].{5}]]\n", CKTypeStr[LINMsg[i].CheckType], LINMsg[i].Check, (LINMsg[i].Timestamp / 36000000) % 60, (LINMsg[i].Timestamp / 600000) % 60, (LINMsg[i].Timestamp / 10000) % 60, (LINMsg[i].Timestamp / 10) % 1000); } //释放内存 Marshal.FreeHGlobal(pt); System.Threading.Thread.Sleep(10); RunTimeMs -= 10; } #endif Console.WriteLine("Close Device!"); //关闭设备 usb_device.USB_CloseDevice(DevHandle); return; }
static void Main(string[] args) { usb_device.DEVICE_INFO DevInfo = new usb_device.DEVICE_INFO(); Int32[] DevHandles = new Int32[20]; Int32 DevHandle = 0; Byte LINIndex = 0; bool state; Int32 DevNum, ret = 0; String[] MSGTypeStr = new String[10] { "UN", "MW", "MR", "SW", "SR", "BK", "SY", "ID", "DT", "CK" }; String[] CKTypeStr = new String[5] { "STD", "EXT", "USER", "NONE", "ERROR" }; //扫描查找设备 DevNum = usb_device.USB_ScanDevice(DevHandles); if (DevNum <= 0) { Console.WriteLine("No device connected!"); return; } else { Console.WriteLine("Have {0} device connected!", DevNum); } DevHandle = DevHandles[0]; //打开设备 state = usb_device.USB_OpenDevice(DevHandle); if (!state) { Console.WriteLine("Open device error!"); return; } else { Console.WriteLine("Open device success!"); } //获取固件信息 StringBuilder FuncStr = new StringBuilder(256); state = usb_device.DEV_GetDeviceInfo(DevHandle, ref DevInfo, FuncStr); if (!state) { Console.WriteLine("Get device infomation error!"); return; } else { Console.WriteLine("Firmware Info:"); Console.WriteLine(" Name:" + Encoding.Default.GetString(DevInfo.FirmwareName)); Console.WriteLine(" Build Date:" + Encoding.Default.GetString(DevInfo.BuildDate)); Console.WriteLine(" Firmware Version:v{0}.{1}.{2}", (DevInfo.FirmwareVersion >> 24) & 0xFF, (DevInfo.FirmwareVersion >> 16) & 0xFF, DevInfo.FirmwareVersion & 0xFFFF); Console.WriteLine(" Hardware Version:v{0}.{1}.{2}", (DevInfo.HardwareVersion >> 24) & 0xFF, (DevInfo.HardwareVersion >> 16) & 0xFF, DevInfo.HardwareVersion & 0xFFFF); Console.WriteLine(" Functions:" + DevInfo.Functions.ToString("X8")); Console.WriteLine(" Functions String:" + FuncStr); } //初始化配置LIN #if LIN_MASTER_TEST ret = USB2LIN_EX.LIN_EX_Init(DevHandle, LINIndex, 19200, 1); #else ret = USB2LIN_EX.LIN_EX_Init(DevHandle, LINIndex, 19200, 0); #endif if (ret != USB2LIN_EX.LIN_EX_SUCCESS) { Console.WriteLine("Config LIN failed!"); return; } else { Console.WriteLine("Config LIN Success!"); } #if LIN_MASTER_TEST //主机写数据 USB2LIN_EX.LIN_EX_MSG[] LINMsg = new USB2LIN_EX.LIN_EX_MSG[5]; USB2LIN_EX.LIN_EX_MSG[] LINOutMsg = new USB2LIN_EX.LIN_EX_MSG[10]; LINMsg[0] = new USB2LIN_EX.LIN_EX_MSG(); LINMsg[0].MsgType = USB2LIN_EX.LIN_EX_MSG_TYPE_BK; //只发送BREAK信号,一般用于唤醒休眠中的从设备 LINMsg[0].Timestamp = 10; //发送该帧数据之后的延时时间,最小建议设置为1 for (int f = 1; f < LINMsg.Length; f++) { LINMsg[f] = new USB2LIN_EX.LIN_EX_MSG(); LINMsg[f].MsgType = USB2LIN_EX.LIN_EX_MSG_TYPE_MW;//主机发送数据 LINMsg[f].DataLen = 8; LINMsg[f].Data = new Byte[8]; for (int i = 0; i < LINMsg[1].DataLen; i++) { LINMsg[f].Data[i] = (Byte)((f << 4) | i); } LINMsg[f].Timestamp = 10; //发送该帧数据之后的延时时间 LINMsg[f].CheckType = USB2LIN_EX.LIN_EX_CHECK_EXT; //增强校验 LINMsg[f].PID = (Byte)(f + 1); } //将数组转换成指针 IntPtr pt = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(USB2LIN_EX.LIN_EX_MSG)) * LINOutMsg.Length); ret = USB2LIN_EX.LIN_EX_MasterSync(DevHandle, LINIndex, LINMsg, pt, LINMsg.Length); if (ret < USB2LIN_EX.LIN_EX_SUCCESS) { Console.WriteLine("MasterSync LIN failed!"); return; } else { //主机发送数据成功后,也会接收到发送出去的数据,通过接收回来的数据跟发送出去的数据对比,可以判断发送数据的时候,数据是否被冲突 Console.WriteLine("MsgLen = {0}", ret); for (int i = 0; i < ret; i++) { LINOutMsg[i] = (USB2LIN_EX.LIN_EX_MSG)Marshal.PtrToStructure((IntPtr)((UInt32)pt + i * Marshal.SizeOf(typeof(USB2LIN_EX.LIN_EX_MSG))), typeof(USB2LIN_EX.LIN_EX_MSG)); Console.Write("{0} SYNC[{1:X2}] PID[{2:X2}] ", MSGTypeStr[LINOutMsg[i].MsgType], LINOutMsg[i].Sync, LINOutMsg[i].PID); for (int j = 0; j < LINOutMsg[i].DataLen; j++) { Console.Write("{0:X2} ", LINOutMsg[i].Data[j]); } Console.WriteLine("[{0}][{1:X2}] [{2}:{3}:{4}.{5}]", CKTypeStr[LINOutMsg[i].CheckType], LINOutMsg[i].Check, (LINOutMsg[i].Timestamp / 36000000) % 60, (LINOutMsg[i].Timestamp / 600000) % 60, (LINOutMsg[i].Timestamp / 10000) % 60, (LINOutMsg[i].Timestamp / 10) % 1000); } } //释放内存 Marshal.FreeHGlobal(pt); #else Console.WriteLine("Start Get LIN Data..."); //设置ID为LIN_EX_MSG_TYPE_SW模式,这样主机就可以读取到数据 USB2LIN_EX.LIN_EX_MSG[] LINSlaveMsg = new USB2LIN_EX.LIN_EX_MSG[10]; for (int i = 0; i < 10; i++) { LINSlaveMsg[i] = new USB2LIN_EX.LIN_EX_MSG(); LINSlaveMsg[i].Data = new Byte[8]; LINSlaveMsg[i].PID = (Byte)i; LINSlaveMsg[i].CheckType = USB2LIN_EX.LIN_EX_CHECK_EXT; LINSlaveMsg[i].DataLen = 7; for (int j = 0; j < LINSlaveMsg[i].DataLen; j++) { LINSlaveMsg[i].Data[j] = (Byte)((i << 4) | j); } LINSlaveMsg[i].MsgType = USB2LIN_EX.LIN_EX_MSG_TYPE_SW;//从机发送数据模式 } //设置从机模式下所有ID都为从接收数据模式,这样就可以获取到主机发送过来的所有数据,初始化配置为从机后,默认所有ID都为接收数据模式,所以若是监听LIN总线数据,这个函数可以不用调用 ret = USB2LIN_EX.LIN_EX_SlaveSetIDMode(DevHandle, LINIndex, LINSlaveMsg, 10); if (ret != USB2LIN_EX.LIN_EX_SUCCESS) { Console.WriteLine("Config LIN ID Mode failed!"); return; } else { Console.WriteLine("Config LIN ID Mode Success!"); } //循环获取接收到的数据,该操作可以用作LIN总线数据监控 Console.WriteLine("Start get data..."); while (true) { USB2LIN_EX.LIN_EX_MSG[] LINMsg = new USB2LIN_EX.LIN_EX_MSG[1024];//缓冲区尽量大一点,防止益处 //将数组转换成指针 IntPtr pt = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(USB2LIN_EX.LIN_EX_MSG)) * LINMsg.Length); ret = USB2LIN_EX.LIN_EX_SlaveGetData(DevHandle, LINIndex, pt); for (int i = 0; i < ret; i++) { LINMsg[i] = (USB2LIN_EX.LIN_EX_MSG)Marshal.PtrToStructure((IntPtr)((UInt32)pt + i * Marshal.SizeOf(typeof(USB2LIN_EX.LIN_EX_MSG))), typeof(USB2LIN_EX.LIN_EX_MSG)); Console.WriteLine("{0} SYNC[{1:X2}] PID[{2:X2}] ", MSGTypeStr[LINMsg[i].MsgType], LINMsg[i].Sync, LINMsg[i].PID); for (int j = 0; j < LINMsg[i].DataLen; j++) { Console.Write("{0:X2} ", LINMsg[i].Data[j]); } Console.WriteLine("[{0}][{1:X2}] [{2}]:{3}]:{4}].{5}]]\n", CKTypeStr[LINMsg[i].CheckType], LINMsg[i].Check, (LINMsg[i].Timestamp / 36000000) % 60, (LINMsg[i].Timestamp / 600000) % 60, (LINMsg[i].Timestamp / 10000) % 60, (LINMsg[i].Timestamp / 10) % 1000); } System.Threading.Thread.Sleep(100); } //释放内存 Marshal.FreeHGlobal(pt); #endif Console.WriteLine("Close Device!"); //关闭设备 usb_device.USB_CloseDevice(DevHandle); return; }
static void Main(string[] args) { Int32[] DevHandles = new Int32[20]; Int32 DevHandle = 0; Byte LINIndex = 0; bool state; Int32 DevNum, ret = 0; LDFDecoder.LDF_CODING LDFCoding = new LDFDecoder.LDF_CODING(); //LDFCoding.pNode[0].pSlaveNames = new StringBuilder[16]; string[] MSGTypeStr = { "UN", "MW", "MR", "SW", "SR", "BK", "SY", "ID", "DT", "CK" }; //扫描查找设备 DevNum = USB_DEVICE.USB_ScanDevice(DevHandles); if (DevNum <= 0) { Console.WriteLine("No device connected!"); return; } else { Console.WriteLine("Have {0} device connected!", DevNum); } DevHandle = DevHandles[0]; //打开设备 state = USB_DEVICE.USB_OpenDevice(DevHandle); if (!state) { Console.WriteLine("Open device error!"); return; } else { Console.WriteLine("Open device success!"); } //ldf文件解析 ret = LDFDecoder.LDF_Decode(DevHandle, "test.ldf", ref LDFCoding); if (ret != LDFDecoder.LDF_SUCCESS) { Console.WriteLine("LDF decode failed"); } //获取帧 int LINFrameLen = LDFCoding.FrameLen; LDFDecoder.LDF_LIN_FRAME[] LINFrames = new LDFDecoder.LDF_LIN_FRAME[LINFrameLen]; #if PRINT_LDF_INFO Console.WriteLine("LINFrameLen = " + string.Format("{0}", LINFrameLen)); for (int i = 0; i < LINFrameLen; i++) { LINFrames[i] = (LDFDecoder.LDF_LIN_FRAME)Marshal.PtrToStructure((IntPtr)((UInt32)LDFCoding.pFrames + i * Marshal.SizeOf(typeof(LDFDecoder.LDF_LIN_FRAME))), typeof(LDFDecoder.LDF_LIN_FRAME)); Console.WriteLine("Name = " + Marshal.PtrToStringAnsi(LINFrames[i].pName)); Console.WriteLine("--ID = 0x" + string.Format("{0:X2}", LINFrames[i].ID)); Console.WriteLine("--Publisher = " + Marshal.PtrToStringAnsi(LINFrames[i].pPublisher)); Console.WriteLine(string.Format("--Size = {0} Bytes", LINFrames[i].Size)); Console.WriteLine(string.Format("--SignalListLen = {0}", LINFrames[i].SignalListLen)); LDFDecoder.LDF_LIN_SIGNAL[] SignalList = new LDFDecoder.LDF_LIN_SIGNAL[LINFrames[i].SignalListLen]; for (int j = 0; j < LINFrames[i].SignalListLen; j++) { SignalList[j] = (LDFDecoder.LDF_LIN_SIGNAL)Marshal.PtrToStructure((IntPtr)((UInt32)LINFrames[i].pSignalList + j * Marshal.SizeOf(typeof(LDFDecoder.LDF_LIN_SIGNAL))), typeof(LDFDecoder.LDF_LIN_SIGNAL)); Console.WriteLine("--SignalName = " + Marshal.PtrToStringAnsi(SignalList[j].pName)); Console.WriteLine(string.Format("----Size = {0} bits", SignalList[j].Size)); Console.WriteLine(string.Format("----Offset = {0} ", SignalList[j].Offset)); Console.Write("----InitValue = "); if (SignalList[j].ValueType == LDFDecoder.LDF_SIGNAL_VALUE_TYPE.LDF_SIGNAL_VALUE_TYPE_SCALAR) { Console.WriteLine(string.Format("{0}", SignalList[j].Value.ScalarValue)); } else { for (int ii = 0; ii < (SignalList[j].Size / 8); ii++) { Console.Write(string.Format("0x{0:X2} ", SignalList[j].Value.ArrayValue[ii])); } Console.WriteLine(""); } Console.WriteLine("----Publisher = " + Marshal.PtrToStringAnsi(SignalList[j].pPublisher)); Console.Write("----Subscribers = "); for (int ii = 0; ii < SignalList[j].SubscriberLen; ii++) { Console.Write(string.Format("{0} ", Marshal.PtrToStringAnsi(SignalList[j].pSubscribers[ii]))); } Console.WriteLine(""); if ((Int32)SignalList[j].SignalCodings.pName != 0) { if (Marshal.PtrToStringAnsi(SignalList[j].SignalCodings.pName).Length > 0) { Console.WriteLine("----SignalCodings = " + Marshal.PtrToStringAnsi(SignalList[j].SignalCodings.pName)); } } Console.WriteLine(""); } } #endif //获取调度表 int SchTableLen = LDFCoding.LINSchLen; LDFDecoder.LDF_LIN_SCH[] SchTables = new LDFDecoder.LDF_LIN_SCH[SchTableLen]; #if PRINT_LDF_INFO Console.WriteLine(string.Format("SchTableLen = {0}", SchTableLen)); for (int i = 0; i < SchTableLen; i++) { SchTables[i] = (LDFDecoder.LDF_LIN_SCH)Marshal.PtrToStructure((IntPtr)((UInt32)LDFCoding.pLINSch + i * Marshal.SizeOf(typeof(LDFDecoder.LDF_LIN_SCH))), typeof(LDFDecoder.LDF_LIN_SCH)); Console.Write("{0}{" + Marshal.PtrToStringAnsi(SchTables[i].pName)); LDFDecoder.LDF_LIN_SCH_CMD[] SCHCmds = new LDFDecoder.LDF_LIN_SCH_CMD[SchTables[i].ScheduleCmdLen]; for (int j = 0; j < SchTables[i].ScheduleCmdLen; j++) { SCHCmds[j] = (LDFDecoder.LDF_LIN_SCH_CMD)Marshal.PtrToStructure((IntPtr)((UInt32)SchTables[i].pScheduleCmdList + j * Marshal.SizeOf(typeof(LDFDecoder.LDF_LIN_SCH_CMD))), typeof(LDFDecoder.LDF_LIN_SCH_CMD)); Console.Write(Marshal.PtrToStringAnsi(SCHCmds[j].Frame.pName) + string.Format(" delay {0} ms;", SCHCmds[j].DelayOfMs)); } Console.WriteLine("}"); } #endif //获取节点信息 LDFDecoder.LDF_LIN_NODE Node = (LDFDecoder.LDF_LIN_NODE)Marshal.PtrToStructure(LDFCoding.pNode, typeof(LDFDecoder.LDF_LIN_NODE)); #if PRINT_LDF_INFO Console.WriteLine("Master=" + Marshal.PtrToStringAnsi(Node.pMasterName) + string.Format(" time_base ={0} ms,jitter={1} ms", Node.time_base, Node.jitter)); Console.Write("Slaves="); for (int i = 0; i < Node.SlaveLen; i++) { Console.Write(Marshal.PtrToStringAnsi(Node.pSlaveNames[i]) + " "); } Console.WriteLine(""); #endif //初始化LIN适配器 ret = USB2LIN_EX.LIN_EX_Init(DevHandle, LINIndex, 9600, 1); if (ret != USB2LIN_EX.LIN_EX_SUCCESS) { Console.WriteLine("Config LIN failed!"); return; } else { Console.WriteLine("Config LIN Success!"); } Console.WriteLine(string.Format("SchTableLen = {0}", SchTableLen)); //执行调度表 for (int i = 0; i < SchTableLen; i++) { for (int j = 0; j < SchTables[i].ScheduleCmdLen; j++) { LDFDecoder.LDF_LIN_SCH_CMD SCHCmds = (LDFDecoder.LDF_LIN_SCH_CMD)Marshal.PtrToStructure((IntPtr)((UInt32)SchTables[i].pScheduleCmdList + j * Marshal.SizeOf(typeof(LDFDecoder.LDF_LIN_SCH_CMD))), typeof(LDFDecoder.LDF_LIN_SCH_CMD)); LDFDecoder.LDF_LIN_FRAME LINFrame = SCHCmds.Frame; USB2LIN_EX.LIN_EX_MSG[] LINMsg = new USB2LIN_EX.LIN_EX_MSG[1]; USB2LIN_EX.LIN_EX_MSG[] LINOutMsg = new USB2LIN_EX.LIN_EX_MSG[10]; if ((Int32)LINFrame.pName != 0)//确保当前帧是有效帧 { if ((Marshal.PtrToStringAnsi(LINFrame.pName).Equals("MasterReq")) || ((LINFrame.Type == LDFDecoder.LDF_FRAME_TYPE.LDF_FRAME_TYPE_NORMAL) && (Marshal.PtrToStringAnsi(LINFrame.pPublisher).Equals(Marshal.PtrToStringAnsi(Node.pMasterName))))) { //该帧为主机发送数据帧 if (LINFrame.Type == LDFDecoder.LDF_FRAME_TYPE.LDF_FRAME_TYPE_NORMAL) { LINMsg[0].CheckType = USB2LIN_EX.LIN_EX_CHECK_EXT;//可以根据ldf文件中的版本号来设置,大于等于2.0的一般都是增强校验,小于2.0的一般都是标准校验 } else { LINMsg[0].CheckType = USB2LIN_EX.LIN_EX_CHECK_STD; } LINMsg[0].MsgType = USB2LIN_EX.LIN_EX_MSG_TYPE_MW; LINMsg[0].PID = LINFrame.ID; LINMsg[0].Timestamp = (UInt32)SCHCmds.DelayOfMs; //将信号进行赋值,此处可以根据实际情况进行赋值 LDFDecoder.LDF_LIN_SIGNAL[] SignalList = new LDFDecoder.LDF_LIN_SIGNAL[LINFrame.SignalListLen]; for (int k = 0; k < LINFrame.SignalListLen; k++) { SignalList[k] = (LDFDecoder.LDF_LIN_SIGNAL)Marshal.PtrToStructure((IntPtr)((UInt32)LINFrame.pSignalList + j * Marshal.SizeOf(typeof(LDFDecoder.LDF_LIN_SIGNAL))), typeof(LDFDecoder.LDF_LIN_SIGNAL)); SignalList[k].Value.ScalarValue = 0; } //将帧信号中的值转换到字节数组中 LINMsg[0].Data = new Byte[8]; LDFDecoder.LDF_DataFrameToRaw(ref LINFrame, LINMsg[0].Data); LINMsg[0].DataLen = LINFrame.Size; IntPtr pt = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(USB2LIN_EX.LIN_EX_MSG)) * LINOutMsg.Length); ret = USB2LIN_EX.LIN_EX_MasterSync(DevHandle, (Byte)LINIndex, LINMsg, pt, 1); if (ret < USB2LIN_EX.LIN_EX_SUCCESS) { Console.WriteLine("MasterSync LIN failed!"); return; } else { Console.WriteLine(string.Format("MsgLen = {0}", ret)); for (int k = 0; k < ret; k++) { LINOutMsg[k] = (USB2LIN_EX.LIN_EX_MSG)Marshal.PtrToStructure((IntPtr)((UInt32)pt + k * Marshal.SizeOf(typeof(USB2LIN_EX.LIN_EX_MSG))), typeof(USB2LIN_EX.LIN_EX_MSG)); Console.Write(Marshal.PtrToStringAnsi(LINFrame.pName) + "[" + MSGTypeStr[LINMsg[0].MsgType] + "]" + string.Format(" SYNC[{0:X2}] PID[{1:X2}] ", LINOutMsg[k].Sync, LINOutMsg[k].PID)); for (int z = 0; z < LINOutMsg[k].DataLen; z++) { Console.Write(string.Format("{0:X2} ", LINOutMsg[k].Data[z])); } Console.WriteLine(string.Format("[{0:X2}] [{1:D2}:{2:D2}:{3:D2}.{4:D3}]", LINOutMsg[k].Check, (LINOutMsg[k].Timestamp / 36000000) % 60, (LINOutMsg[k].Timestamp / 600000) % 60, (LINOutMsg[k].Timestamp / 10000) % 60, (LINOutMsg[k].Timestamp / 10) % 1000)); } } } else { //该帧为主机读数据帧 LINMsg[0].MsgType = USB2LIN_EX.LIN_EX_MSG_TYPE_MR; LINMsg[0].PID = LINFrame.ID; LINMsg[0].Timestamp = (UInt32)SCHCmds.DelayOfMs; LINMsg[0].Data = new Byte[8]; IntPtr pt = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(USB2LIN_EX.LIN_EX_MSG)) * LINOutMsg.Length); ret = USB2LIN_EX.LIN_EX_MasterSync(DevHandle, (Byte)LINIndex, LINMsg, pt, 1); if (ret < USB2LIN_EX.LIN_EX_SUCCESS) { Console.WriteLine("MasterSync LIN failed!"); return; } else { Console.WriteLine(string.Format("MsgLen = {0}", ret)); for (int k = 0; k < ret; k++) { LINOutMsg[k] = (USB2LIN_EX.LIN_EX_MSG)Marshal.PtrToStructure((IntPtr)((UInt32)pt + k * Marshal.SizeOf(typeof(USB2LIN_EX.LIN_EX_MSG))), typeof(USB2LIN_EX.LIN_EX_MSG)); Console.Write(Marshal.PtrToStringAnsi(LINFrame.pName) + "[" + MSGTypeStr[LINMsg[0].MsgType] + "]" + string.Format(" SYNC[{0:X2}] PID[{1:X2}] ", LINOutMsg[k].Sync, LINOutMsg[k].PID)); for (int z = 0; z < LINOutMsg[k].DataLen; z++) { Console.Write(string.Format("{0:X2} ", LINOutMsg[k].Data[z])); } Console.WriteLine(string.Format("[{0:X2}] [{1:D2}:{2:D2}:{3:D2}.{4:D3}]", LINOutMsg[k].Check, (LINOutMsg[k].Timestamp / 36000000) % 60, (LINOutMsg[k].Timestamp / 600000) % 60, (LINOutMsg[k].Timestamp / 10000) % 60, (LINOutMsg[k].Timestamp / 10) % 1000)); if (LINOutMsg[i].DataLen > 0) { //将字节数组的数据解析到帧信号中 LDFDecoder.LDF_DataRawToFrame(ref LINFrame, LINOutMsg[i].Data); //循环显示信号值 for (int x = 0; x < LINFrame.SignalListLen; x++) { LDFDecoder.LDF_LIN_SIGNAL Signal = (LDFDecoder.LDF_LIN_SIGNAL)Marshal.PtrToStructure((IntPtr)((UInt32)LINFrame.pSignalList + x * Marshal.SizeOf(typeof(LDFDecoder.LDF_LIN_SIGNAL))), typeof(LDFDecoder.LDF_LIN_SIGNAL)); if (Signal.ValueType == LDFDecoder.LDF_SIGNAL_VALUE_TYPE.LDF_SIGNAL_VALUE_TYPE_SCALAR) { Console.WriteLine(Marshal.PtrToStringAnsi(LINFrame.pName) + string.Format(" = {0}", Signal.Value.ScalarValue)); } else { Console.Write(Marshal.PtrToStringAnsi(LINFrame.pName) + " = "); for (int y = 0; y < Signal.Size / 8; y++) { Console.Write(string.Format("{0:X2} ", Signal.Value.ArrayValue[k])); } } } } } } } } } } }