-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
222 lines (181 loc) · 7.47 KB
/
MainWindow.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Runtime.InteropServices;
using System.Security.Cryptography.X509Certificates;
using System.Threading;
/*------------兼容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-CAN总线适配器板卡信息的数据类型1,该类型为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;
}
/*------------数据结构描述完成---------------------------------*/
namespace ZLGCAN
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
/*------------兼容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);
/*------------函数描述结束---------------------------------*/
public MainWindow()
{
InitializeComponent();
}
private void but1_Click(object sender, RoutedEventArgs e)
{
UInt32 i = VCI_OpenDevice(4,0,0);
int j = (int)i;
lab1.Content = j.ToString();
VCI_INIT_CONFIG config = new VCI_INIT_CONFIG();
config.AccCode = 0x00000000;
config.AccMask = 0xFFFFFFFF;
config.Timing0 = 0x04;
config.Timing1 = 0x1C;
config.Filter = 0;
config.Mode = 0;
VCI_InitCAN(4, 0, 0, ref config);
}
private void but2_Click(object sender, RoutedEventArgs e)
{
UInt32 i = VCI_CloseDevice(4, 0);
int j = (int)i;
lab1.Content = j.ToString();
}
private void but3_Click(object sender, RoutedEventArgs e)
{
VCI_BOARD_INFO1 caninfo = new VCI_BOARD_INFO1();
uint n = VCI_FindUsbDevice(ref caninfo);
switch (Tbox.Text)
{
case "0":
canbox.Text += "\n";
break;
case "1":
lab1.Content = System.Text.Encoding.Default.GetString(caninfo.str_Usb_Serial);
break;
case "2":
lab1.Content = caninfo.fw_Version;
break;
}
}
private void but_read_Click(object sender, RoutedEventArgs e)
{
canbox.Text = "";
VCI_CAN_OBJ[] mcanobj = new VCI_CAN_OBJ[10];
UInt32 res = new UInt32();
res=VCI_Receive(4, 0, 0, ref mcanobj[0],10, 0);
Thread th = new Thread(upcanbox);
th.IsBackground = true;
th.Start(mcanobj);
}
///更新canbox
public void upcanbox(object candata0)
{
VCI_CAN_OBJ[] candata = (VCI_CAN_OBJ[])candata0;
//Thread.Sleep(10);
this.Dispatcher.BeginInvoke((Action)delegate ()
{
for(int i=0;i<candata.Length;i++)
{
this.canbox.Text += candata[i].TimeFlag + "\n";
}
//this.progressBar.Value = i;
//this.textBox.Text = i.ToString();
//this.canbox.Text += candata[i].ID + "\n";
});
}
}
}