using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
uint m_devtype = 4;//设备类型号
uint m_devind = 0;//设备索引号
int m_canid = 0;//can口ID
///5.定义初始化CAN的数据类型
struct VCI_INIT_CONFIG
{
public int AccCode;
public int AccMask;
public int Reserved;
public int Filter;
public int Timing0;
public int Timing1;
public int Mode;
}
///2.定义CAN信息帧的数据类型。
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 byte []Data; ///Data[8]
public byte []Reserved; //Reserved[0] 第0位表示特殊的空行或者高亮帧 Reserved[3]
}
///4.定义错误信息的数据类型。
struct VCI_ERR_INFO
{
public uint ErrCode;
public byte []Passive_ErrData;/// Passive_ErrData[3]
public byte ArLost_ErrData;
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
VCI_INIT_CONFIG InitConfig = new VCI_INIT_CONFIG();
/*
InitConfig.AccCode = 00;
InitConfig.AccMask = -1;
InitConfig.Filter = 1;
InitConfig.Reserved = 0;
InitConfig.Mode = 0;
InitConfig.Timing0 = 0;//测试....
InitConfig.Timing1 = 20;
*/
uint a = VCI_CloseDevice(m_devtype, m_devind);//调用DLL
uint b = VCI_OpenDevice(m_devtype, m_devind, 0);
if (b != 1)//reserverd
{
MessageBox.Show("打开设备错误" );
return;
}
if (VCI_InitCAN(3, 0, 0, ref InitConfig) == 1)//canid
{
if (VCI_StartCAN(3, 0, 0) == 1)//canid
{
/*
VCI_CAN_OBJ vio = new VCI_CAN_OBJ();
vio.DataLen = 8;
vio.ExternFlag = 0;
vio.RemoteFlag = 0;
vio.SendType = 2;
vio.ID = 1;
vio.data0 = (byte)11;
vio.data1 = (byte)11;
vio.data2 = (byte)11;
vio.data3 = (byte)11;
vio.data4 = (byte)11;
vio.data5 = (byte)11;
vio.data6 = (byte)11;
vio.data7 = (byte)11;
vio.Reserved0 = 0;
vio.Reserved1 = 0;
vio.Reserved2 = 0;
int smap = VCI_Transmit(3, 0, 0, ref vio, 1);
MessageBox.Show(smap.ToString());
*/
}
}
else
{
MessageBox.Show("初始化错误");
return;
}
}
[DllImport("ControlCAN.dll")]
static extern uint VCI_OpenDevice(uint DevType, uint DevIndex, uint Reserved);//Reserved系统保留字段
[DllImport("ControlCAN.dll")]
static extern uint VCI_CloseDevice(uint DevType, uint DevIndex);
[DllImport("ControlCAN.dll")]
static extern int VCI_InitCAN(int Devicetype, int DeviceInd, int CANInd, ref VCI_INIT_CONFIG InitConfig);//初始化指定CAN
[DllImport("ControlCAN.dll")]
static extern int VCI_StartCAN(int Devicetype, int DeviceInd, int CANInd);//启动CAN
[DllImport("ControlCAN.dll")]
static extern int VCI_ResetCAN(int Devicetype, int DeviceInd, int CANInd);//复位CAN
[DllImport("ControlCAN.dll")]
static extern int VCI_Transmit(int Devicetype, int DeviceInd, int CANInd, ref VCI_CAN_OBJ vio, int length);//发送数据
[DllImport("ControlCAN.dll")]
static extern int VCI_ReadErrInfo(int Devicetype, int DeviceInd, int CANInd, ref VCI_ERR_INFO errinfo);//接收最后1条错误信息
[DllImport("ControlCAN.dll")]
static extern int VCI_Receive(int Devicetype, int DeviceInd, int CANInd, ref VCI_CAN_OBJ vri, int length, int WaitTime);//接收信息
}
}
设备打开那边就出错了,有大佬知道为啥吗