User Tools

Site Tools

blog:2022-11-06_c_porting_cananalyst-ii_code_into_project



2022-11-06 C#: Porting CANanalyst-II Code into Project

  • We are porting the CANanalyst-II can bus code into my project.

Steps

  • The steps as follows:
  • 1, import code outside form:
    • /*----------------ZLG的數據類型-------- --*/
      // 1.ZLGCAN系列接口卡信息的數據類型
      public struct VCI_BOARD_INFO
      {
          public UInt16 hw_Version;
          public UInt16 fw_Version;
          public UInt16 dr_Version;
          public UInt16 in_Version;
          public UInt16 irq_Num;
          public byte can_Num;
          [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)] public byte[] str_Serial_Num;
          [MarshalAs(UnmanagedType.ByValArray, SizeConst = 40)]
          public byte[] str_hw_Type;
          [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
          public byte[] Reserved;
      }
      
      //2.定義CAN信息幀的數據類型
      unsafe public struct VCI_CAN_OBJ  //使用不安全代碼
      {
          public uint ID;
          public uint TimeStamp;        //時間標識
          public byte TimeFlag;         //是否使用時間標識
          public byte SendType;         //發送標誌。保留,未用
          public byte RemoteFlag;       //是否是遠程幀
          public byte ExternFlag;       //是否是擴展幀
          public byte DataLen;          //數據長度
          public fixed byte Data[8];    //數據
          public fixed byte Reserved[3];//保留位
      
      }
      
      // 3.初始化CAN的數據類型
      public struct VCI_INIT_CONFIG
      {
          public UInt32 AccCode;
          public UInt32 AccMask;
          public UInt32 Reserved;
          public byte Filter;   // 0或1接收所有幀。2標準幀幀,3是擴展幀幀
          public byte Timing0;  // 波特率參數,具體配置,請查看二次開發庫函數說明書
          public byte Timing1;
          public byte Mode;     // 模式,0表示正常模式,1表示只聽模式,2表示自測模式
      }
      
      /*------------其他數據結構描述---------------------------------*/
      //4.USB-CAN1參數返回卡信息的數據類型,該類型為VCI_FindUsbDevice函數的
      public struct VCI_BOARD_INFO1
      {
          public UInt16 hw_Version;
          public UInt16 fw_Version;
          public UInt16 dr_Version;
          public UInt16 in_Version;
          public UInt16 irq_Num;
          public byte can_Num;
          public byte Reserved;
          [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)] public byte[] str_Serial_Num;
          [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
          public byte[] str_hw_Type;
          [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
          public byte[] str_Usb_Serial;
      }
      
      public struct CHGDESIPANDPORT
      {
          [MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]
          public byte[] szpwd;
          [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
          public byte[] szdesip;
          public Int32 desport;
      
          public void Init()
          {
              szpwd = new byte[10];
              szdesip = new byte[20];
          }
      }
      
      /*------------數據結構描述完成---------------------------------*/
  • 2, Porting codes inside form:
    • const int DEV_USBCAN = 3;
      const int DEV_USBCAN2 = 4;
      /// <summary>
      /// 
      /// </summary>
      /// <param name="DeviceType"></param>
      /// <param name="DeviceInd"></param>
      /// <param name="Reserved"></param>
      /// <returns></returns>
      /*------------兼容ZLG的函数描述---------------------------------*/
      [DllImport("controlcan.dll")]
      static extern UInt32 VCI_OpenDevice(UInt32 DeviceType, UInt32 DeviceInd, UInt32 Reserved);
      [DllImport("controlcan.dll")]
      static extern UInt32 VCI_CloseDevice(UInt32 DeviceType, UInt32 DeviceInd);
      [DllImport("controlcan.dll")]
      static extern UInt32 VCI_InitCAN(UInt32 DeviceType, UInt32 DeviceInd, UInt32 CANInd, ref VCI_INIT_CONFIG pInitConfig);
      
      [DllImport("controlcan.dll")]
      static extern UInt32 VCI_ReadBoardInfo(UInt32 DeviceType, UInt32 DeviceInd, ref VCI_BOARD_INFO pInfo);
      
      [DllImport("controlcan.dll")]
      static extern UInt32 VCI_GetReceiveNum(UInt32 DeviceType, UInt32 DeviceInd, UInt32 CANInd);
      [DllImport("controlcan.dll")]
      static extern UInt32 VCI_ClearBuffer(UInt32 DeviceType, UInt32 DeviceInd, UInt32 CANInd);
      
      [DllImport("controlcan.dll")]
      static extern UInt32 VCI_StartCAN(UInt32 DeviceType, UInt32 DeviceInd, UInt32 CANInd);
      [DllImport("controlcan.dll")]
      static extern UInt32 VCI_ResetCAN(UInt32 DeviceType, UInt32 DeviceInd, UInt32 CANInd);
      
      [DllImport("controlcan.dll")]
      static extern UInt32 VCI_Transmit(UInt32 DeviceType, UInt32 DeviceInd, UInt32 CANInd, ref VCI_CAN_OBJ pSend, UInt32 Len);
      
      [DllImport("controlcan.dll")]
      static extern UInt32 VCI_Receive(UInt32 DeviceType, UInt32 DeviceInd, UInt32 CANInd, ref VCI_CAN_OBJ pReceive, UInt32 Len, Int32 WaitTime);
      
      /*------------其他函数描述---------------------------------*/
      
      [DllImport("controlcan.dll")]
      static extern UInt32 VCI_ConnectDevice(UInt32 DevType, UInt32 DevIndex);
      [DllImport("controlcan.dll")]
      static extern UInt32 VCI_UsbDeviceReset(UInt32 DevType, UInt32 DevIndex, UInt32 Reserved);
      [DllImport("controlcan.dll")]
      static extern UInt32 VCI_FindUsbDevice(ref VCI_BOARD_INFO1 pInfo);
      /*------------函数描述结束---------------------------------*/
  • 3, We will get a compile error of “Unsafe code”

Solution

  • Goto project :
  • Checked the “Allow unsafe code”
  • Then, we can compile it without error

TAGS

  • 119 person(s) visited this page until now.

Permalink blog/2022-11-06_c_porting_cananalyst-ii_code_into_project.txt · Last modified: 2022/11/06 11:06 by jethro

oeffentlich