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);//接收信息
}
}
...全文
434 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
sherrycancer 2010-11-12
  • 打赏
  • 举报
回复
我的问题解决了,之前调用的库是错的。因为我们工程上用的都是类似的库,你查查你的库是不是也找错了。
sherrycancer 2010-11-10
  • 打赏
  • 举报
回复
你好,我看到你发的“C#串口通讯usbcan2 “我也遇到这样的问题,跟着程序逐步观察,我看到我发送时VCI_Transmit返回值为1,但是周立功上现实什么都没有收到,请问你的问题解决了么?怎么解决的?
dai78 2009-06-01
  • 打赏
  • 举报
回复
你用的CAN总线通讯?
wddming 2009-06-01
  • 打赏
  • 举报
回复
跟踪侠,虽然麻烦点,可是排错还是很好用的。
zenowolf 2009-06-01
  • 打赏
  • 举报
回复
看该产品的通信协议
typeof 2009-06-01
  • 打赏
  • 举报
回复
单步调试看看
wenblue7 2009-06-01
  • 打赏
  • 举报
回复
顶起
redleafe 2009-06-01
  • 打赏
  • 举报
回复
关注中,帮顶。
我不懂电脑 2009-05-31
  • 打赏
  • 举报
回复
建议你看他的demo做啊。
你可以设置断点,单步调试,看哪些代码没有执行,会有哪些后果。

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧