C#串口通讯usbcan2进来99我
锐宝宝 2009-05-31 03:57:31 我用的周力功的CAN口,可以连接上,发不了数据,强人帮我!~谢谢
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Microsoft.VisualBasic;
namespace TestCan
{
public partial class Form1 : Form
{
int m_devtype = 3;//设备类型号
int m_devind = 0;//设备索引号
int m_canid = 0;//can口ID
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
VCI_INIT_CONFIG InitConfig = new VCI_INIT_CONFIG();
InitConfig.AccCode = 00;
InitConfig.AccMask = -1;
InitConfig.Filter_Renamed = 1;
InitConfig.mode = 0;
InitConfig.Timing0 = 0;//测试....
InitConfig.Timing1 = 20;
VCI_CloseDevice(m_devtype, m_devind);//调用DLL
if (VCI_OpenDevice(m_devtype, m_devind, 0) != 1)//reserverd
{
int i = VCI_OpenDevice(3, 0, 9600);
MessageBox.Show("打开设备错误" + i);
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 int VCI_CloseDevice(int Devicetype, int DeviceInd);//关闭设备
[DllImport("ControlCAN.dll")]
static extern int VCI_OpenDevice(int Devicetype, int DeviceInd, int Reserved);//打开设备
[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);//接收信息
}
}