The example code provided by the
Ucos can PCI interface card is VC,VB and Delphi, with no c#.net examples. However, when you call the can API using C #, debugging is cumbersome because these APIs are very strict in data type validation. Groping for some time, and finally using C # 2.0 vs.2008 invoke the can API to normally send receive data.
now provide the API statement as follows, with the same needs, you can go a lot less detours. public sealed class Canapi {//interface card type definition public enum Pcidevicetype {vci_pci5121 =1, vci_pci9810 =2, Vci_usbcan1 =3, Vci_usb CAN2 =4, vci_pci9820 =5, vci_can232 =6, vci_pci5110 =7, Vci_canlite =8, vci_isa9620 =9, vci_isa5420 =10, VCI_PC104CAN = 11 , Vci_canete =12, vci_dnp9810 =13, vci_pci9840 =14, vci_pci9820i = 16}//Function call return status value public static readonly int STATUS_OK =1 ; public static readonly int status_err = 0; public enum ErrorType {//can error code err_can_overflow =0x0001,//can controller internal FIFO overflow err_can_erralarm =0x0002,//can controller error Alarm Err_ca N_passive =0x0004,//can controller negative error err_can_lose =0x0008,//can controller quorum lost Err_can_buserr =0x0010,//can Controller bus error/Universal error code ERR_ deviceopened =0x0100,//device turned on Err_deviceopen =0x0200,//Open device Error Err_devicenotopen =0x0400,//device not open Err_bufferoverflow = 0x0800,//Buffer overflow err_devicenotexist =0x1000,//This device does not exist Err_loadkerneldll =0x2000,//Load dynamic library failed err_cmdfailed =0x4000,// Execute command failure error code err_buffercreate =0x8000//Low Memory}//1.zlgcan SystemThe data type of the column interface card information. [StructLayout (layoutkind.sequential, CharSet = charset.ansi)] public struct Vci_board_info {public ushort hw_version; pu Blic ushort Fw_version; public ushort Dr_version; public ushort In_version; public ushort Irq_num; public byte Can_num; [System.Runtime.InteropServices.MarshalAsAttribute (SYSTEM.RUNTIME.INTEROPSERVICES.UNMANAGEDTYPE.BYVALTSTR, SizeConst =)] public string str_serial_num; [System.Runtime.InteropServices.MarshalAsAttribute (SYSTEM.RUNTIME.INTEROPSERVICES.UNMANAGEDTYPE.BYVALTSTR, SizeConst =)] public string str_hw_type; [System.Runtime.InteropServices.MarshalAsAttribute (System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst = 4, ArraySubType = System.Runtime.InteropServices.UnmanagedType.U2)] [public ushort[] Reserved; //2. Defines the data type of the can information frame. [StructLayout (layoutkind.sequential, CharSet = charset.ansi)] public struct Vci_can_obj {public uint ID; Stamp; public byte Timeflag; public byte Sendtype; Public byte remoteflag;//is a remote frame pWhether the ublic byte externflag;//is an extended frame public byte datalen; [MarshalAs (UnmanagedType.ByValArray, SizeConst = 8, ArraySubType = unmanagedtype.i1)] public byte[] Data; [MarshalAs (UnmanagedType.ByValArray, SizeConst = 3, ArraySubType = unmanagedtype.i1)] public byte[] Reserved; //3. Defines the data type of the can controller state. [StructLayout (layoutkind.sequential, CharSet = charset.ansi)] public struct Vci_can_status {public byte errinterrupt; pu Blic byte Regmode; public byte Regstatus; public byte regalcapture; public byte regeccapture; public byte Regewlimit; public byte Regrecounter; public byte Regtecounter; public UINT Reserved; //4. Defines the data type of the error message. [StructLayout (layoutkind.sequential, CharSet = charset.ansi)] public struct Vci_err_info {public uint Errcode; [System.Runtime.InteropServices.MarshalAsAttribute (System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst = 3, ArraySubType = System.Runtime.InteropServices.UnmanagedType.I1)] [public byte[] passive_errdata; public byte Arlost_errdata; //5. Defines the data classes that initialize canType [StructLayout (layoutkind.sequential, CharSet = charset.ansi)] public struct Vci_init_config {public uint Acccode; publi c UINT Accmask; public UINT Reserved; public byte Filter; public byte Timing0; public byte Timing1; public byte Mode; [StructLayout (layoutkind.sequential, CharSet = charset.ansi)] public struct Chgdesipandport {[ System.Runtime.InteropServices.MarshalAsAttribute (SYSTEM.RUNTIME.INTEROPSERVICES.UNMANAGEDTYPE.BYVALTSTR, SizeConst = Ten)] public string szpwd; [System.Runtime.InteropServices.MarshalAsAttribute (SYSTEM.RUNTIME.INTEROPSERVICES.UNMANAGEDTYPE.BYVALTSTR, SizeConst =)] public string Szdesip; public int desport; #region API functions [DllImport ("ControlCAN.dll", EntryPoint = "Vci_opendevice", CharSet = charset.ansi, callingconvention = Ca Llingconvention.stdcall)] public static extern uint Vci_opendevice (UINT devicetype, uint deviceind, uint Reserved); [DllImport ("ControlCAN.dll", EntryPoint = "Vci_closedevice", CharSet = charset.ansi, callingconvention = CallingcoNvention. stdcall)] public static extern uint Vci_closedevice (UINT devicetype, uint deviceind); [DllImport ("ControlCAN.dll", EntryPoint = "Vci_initcan", CharSet = charset.ansi, callingconvention = Callingconvention.stdcall)] public static extern uint Vci_initcan (UINT devicetype, uint deviceind, uint canind, ref vci_in It_config pinitconfig); [DllImport ("ControlCAN.dll", EntryPoint = "Vci_readboardinfo", CharSet = charset.ansi, callingconvention = Callingconvention.stdcall)] public static extern uint Vci_readboardinfo (UINT devicetype, uint deviceind, ref vci_board_ INFO pinfo); [DllImport ("ControlCAN.dll", EntryPoint = "Vci_readerrinfo", CharSet = charset.ansi, callingconvention = Callingconvention.stdcall)] public static extern uint Vci_readerrinfo (UINT devicetype, uint deviceind, uint canind, ref VC I_err_info perrinfo); [DllImport ("ControlCAN.dll", EntryPoint = "Vci_readcanstatus", CharSet = charset.ansi, callingconvention = Callingconvention.stdcall)] public static extern uint VCI_READCAnstatus (UINT DeviceType, uint deviceind, uint canind, ref vci_can_status pcanstatus); [DllImport ("ControlCAN.dll", EntryPoint = "Vci_getreference", CharSet = charset.ansi, callingconvention = Callingconvention.stdcall)] public static extern uint Vci_getreference (UINT devicetype, uint deviceind, uint canind, uint RefType, Object pData); [DllImport ("ControlCAN.dll", EntryPoint = "Vci_setreference", CharSet = charset.ansi, callingconvention = Callingconvention.stdcall)] public static extern uint Vci_setreference (UINT devicetype, uint deviceind, uint canind, uint RefType, Object pData); [DllImport ("ControlCAN.dll", EntryPoint = "Vci_getreceivenum", CharSet = charset.ansi, callingconvention = Callingconvention.stdcall)] public static extern uint Vci_getreceivenum (UINT devicetype, uint deviceind, uint canind); [DllImport ("ControlCAN.dll", EntryPoint = "Vci_clearbuffer", CharSet = charset.ansi, callingconvention = Callingconvention.stdcall)] public static extern uint Vci_clearbuffer (UINT DevIcetype, uint deviceind, uint canind); [DllImport ("ControlCAN.dll", EntryPoint = "Vci_startcan", CharSet = charset.ansi, callingconvention = Callingconvention.stdcall)] public static extern uint Vci_startcan (UINT devicetype, uint deviceind, uint canind); [DllImport ("ControlCAN.dll", EntryPoint = "Vci_resetcan", CharSet = charset.ansi, callingconvention = Callingconvention.stdcall)] public static extern uint Vci_resetcan (UINT devicetype, uint deviceind, uint canind); [DllImport ("ControlCAN.dll", EntryPoint = "Vci_transmit", CharSet = charset.ansi, callingconvention = Callingconvention.stdcall)] public static extern uint Vci_transmit (UINT devicetype, uint deviceind, uint canind, ref vci_c An_obj psend, uint Len); [DllImport ("ControlCAN.dll", EntryPoint = "Vci_receive", CharSet = charset.ansi, callingconvention = Callingconvention.stdcall)] public static extern uint Vci_receive (UINT devicetype, uint deviceind, uint canind, ref VCI_CA N_obj preceive, uint Len, int waittime); #endregion}
Note: During use, you need to add the ControlCAN.dll and Kerneldlls folders and all of their DLLs below to the project, and set the copy to output file property of these files to regular copy.
Sending and receiving compare easily, the sample code is as follows:
canapi.vci_can_obj[] Frameinfo = new Canapi.vci_can_obj[1]; Frameinfo[0]. Datalen = (Byte) (ss. Length); Frameinfo[0]. data = data; Frameinfo[0]. Reserved = new Byte[3]; Frameinfo[0]. Remoteflag = (byte) this.combFrameFormat.SelectedIndex; Frameinfo[0]. Externflag = (byte) this.combFrameType.SelectedIndex; UINT Frameid = 0; if (!uint. TryParse (This.txtFrameID.Text, System.Globalization.NumberStyles.AllowHexSpecifier, NULL, out Frameid)) Frameid = 0; if (frameinfo[0). Externflag = = 1) {frameinfo[0].id = Frameid;} else {frameinfo[0].id = Frameid | 0x0000ffff;} frameinfo[0]. Sendtype = (byte) this.combSendFormat.SelectedIndex; Canapi.vci_transmit (This.mdevicetype, This.mdeviceindex, This.mcanindex, ref frameinfo[0], 1);
canapi.vci_can_obj[] Frameinfos = new CANAPI.VCI_CAN_OBJ[50]; len = (int) canapi.vci_receive (This.mdevicetype, This.mdeviceindex, This.mcanindex, ref frameinfos[0], 50, 200); if (len <= 0) {//NOTE: If you do not read the data, you must call this function to read out the current error code,//Never omit this step (even if you may not want to know what the error code is) Canapi.vci_readerrinfo ( This.mdevicetype, This.mdeviceindex, This.mcanindex, ref errinfo); } else {...
The Operation effect chart is as follows: