| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace CCDCount.DLL.CanBus
- {
- public class CanManagerClass
- {
- UInt32 m_devtype = 4;//USBCAN2
- UInt32 m_devind = 0;
- UInt32 m_canind = 0;
- int DataLen = 8;
- int SendCount = 0;
- VCI_INIT_CONFIG config = new VCI_INIT_CONFIG();
- public void OpenDevice()
- {
- if (CanLibraryClass.VCI_OpenDevice(m_devtype, m_devind, 0) == 0)
- {
- Console.WriteLine("打开设备失败,请检查设备类型和设备索引号是否正确");
- return;
- }
- }
- public void InitCan()
- {
- //初始化CAN
- config.AccCode = Convert.ToUInt32("0x00000000", 16);
- config.AccMask = Convert.ToUInt32("0xFFFFFFFF", 16);
- config.Timing0 = Convert.ToByte("0x00", 16);
- config.Timing1 = Convert.ToByte("0x14", 16);
- config.Filter = (Byte)1;
- config.Mode = (Byte)0;
- if (CanLibraryClass.VCI_InitCAN(m_devtype, m_devind, m_canind, ref config) == 0)
- {
- Console.WriteLine("初始化Can失败");
- return;
- }
- if (CanLibraryClass.VCI_StartCAN(m_devtype, m_devind, m_canind) == 0)
- {
- Console.WriteLine("开启Can失败");
- return;
- }
- }
- public unsafe void SenMessage(byte[] bytes)
- {
- VCI_CAN_OBJ sendobj = new VCI_CAN_OBJ();
- sendobj.RemoteFlag = (byte)(0);
- sendobj.ExternFlag = (byte)(0);
- sendobj.ID = Convert.ToUInt32(Convert.ToString(SendCount, 16), 16);
- sendobj.DataLen = Convert.ToByte(bytes.Length > 8 ? 8 : bytes.Length);
- for (int i = 0; i < DataLen; i++)
- {
- sendobj.Data[i] = bytes[i];
- }
- if (CanLibraryClass.VCI_Transmit(m_devtype, m_devind, m_canind, ref sendobj, 1) == 0)
- {
- Console.WriteLine("发送失败");
- return;
- }
- }
- public void CloseCan()
- {
- CanLibraryClass.VCI_ResetCAN(m_devtype, m_devind, m_canind);
- }
- }
- }
|