您的位置:首页 > 服装鞋帽 > 男装 > Advantech 硬件控制卡的 c# 接口函数

Advantech 硬件控制卡的 c# 接口函数

luyued 发布于 2011-03-15 22:56   浏览 N 次  
Advantech 硬件控制卡的 c# 接口函数2010-04-03 20:17

using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Runtime.InteropServices;
5
6namespace Hardware.Advantech.Interface
7{
8 PT_DEVLIST#region PT_DEVLIST
9
10 /**////


11 /// 安装在主机上所有的自动控制板卡设备结构。
12 ///

13 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack=1)]
14 public struct PT_DEVLIST
15 {
16 public UInt32 dwDeviceNum;
17 /**////
18 /// 设备名,50个字符长。
19 ///

20 [MarshalAs(UnmanagedType.ByValTStr, SizeConst=50)]
21 public string szDeviceName;
22 /**////
23 /// 设备的端口数,一般为8个端口。
24 ///

25 public Int16 nNumOfSubdevices;
26 }
27
28 /**////
29 /// 用于 DRV_DeviceGetList 调用返回的设备列表结构。
30 /// 这一块真难改呀~~,耗费了我一整个晚上的时间~~~~~~~
31 /// 网上也找不到资料,最可气的是.Net带的函数都不支持结构数组,只好变相再来一个结构了~~
32 /// xiefang 2007/09/02
33 ///

34 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
35 public struct PT_DEVLISTARRAY
36 {
37 [MarshalAs(UnmanagedType.ByValArray, SizeConst = AdvantechAPI.MaxEntries)]
38 public PT_DEVLIST[] Devices;
39 }
40
41 #endregion
42
43 PT_DeviceGetFeatures#region PT_DeviceGetFeatures
44
45 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
46 internal struct PT_DeviceGetFeatures
47 {
48 public IntPtr Buffer;
49 public int Size;
50 }
51
52 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
53 public struct GainListBlob
54 {
55 [MarshalAs(UnmanagedType.ByValArray, SizeConst = 448)]
56 public byte[] gainArr;
57 }
58
59 /**////
60 /// Define hardware board(device) features.
61 ///

62 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
63 public struct DEVFEATURES
64 {
65 /**////
66 /// device driver version, array[0..7]
67 ///

68 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 8)]
69 public string szDriverVer;
70 /**////
71 /// device driver name, array[0..MAX_DRIVER_NAME_LEN-1]
72 ///

73 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = AdvantechAPI.MAX_DRIVER_NAME_LEN)]
74 public string szDriverName;
75 /**////
76 /// board ID, DWORD 4 bytes
77 ///

78 public uint dwBoardID;
79 /**////
80 /// Max. number of differential channel
81 ///

82 public ushort usMaxAIDiffChl;
83 /**////
84 /// Max. number of single-end channel
85 ///

86 public ushort usMaxAISiglChl;
87 /**////
88 /// Max. number of D/A channel
89 ///

90 public ushort usMaxAOChl;
91 /**////
92 /// Max. number of digital out channel
93 ///

94 public ushort usMaxDOChl;
95 /**////
96 /// Max. number of digital input channel
97 ///

98 public ushort usMaxDIChl;
99 /**////
100 /// specifies if programmable or not
101 ///

102 public ushort usDIOPort;
103 /**////
104 /// Max. number of Counter/Timer channel
105 ///

106 public ushort usMaxTimerChl;
107 /**////
108 /// Max number of alram channel
109 ///

110 public ushort usMaxAlarmChl;
111 /**////
112 /// number of bits for A/D converter
113 ///

114 public ushort usNumADBit;
115 /**////
116 /// A/D channel width in bytes.
117 ///

118 public ushort usNumADByte;
119 /**////
120 /// number of bits for D/A converter.
121 ///

122 public ushort usNumDABit;
123 /**////
124 /// D/A channel width in bytes.
125 ///

126 public ushort usNumDAByte;
127 /**////
128 /// Max. number of gain code
129 ///

130 public ushort usNumGain;
131 /**////
132 /// Gain listing array[0..15]
133 ///

134 public GainListBlob glGainList;
135 /**////
136 /// Permutation array[0..3]
137 /// Bit 0: Software AI
138 /// Bit 1: DMA AI
139 /// Bit 2: Interrupt AI
140 /// Bit 3: Condition AI
141 /// Bit 4: Software AO
142 /// Bit 5: DMA AO
143 /// Bit 6: Interrupt AO
144 /// Bit 7: Condition AO
145 /// Bit 8: Software DI
146 /// Bit 9: DMA DI
147 /// Bit 10: Interrupt DI
148 /// Bit 11: Condition DI
149 /// Bit 12: Software DO
150 /// Bit 13: DMA DO
151 /// Bit 14: Interrupt DO
152 /// Bit 15: Condition DO
153 /// Bit 16: High Gain
154 /// Bit 17: Auto Channel Scan
155 /// Bit 18: Pacer Trigger
156 /// Bit 19: External Trigger
157 /// Bit 20: Down Counter
158 /// Bit 21: Dual DMA
159 /// Bit 22: Monitoring
160 /// Bit 23: QCounter
161 ///

162 public uint dwPermutation0;
163 public uint dwPermutation1;
164 public uint dwPermutation2;
165 public uint dwPermutation3;
166 }
167
168 #endregion
169
170 /**////
171 /// 写入设备的数据结构。
172 ///

173 [StructLayout(LayoutKind.Explicit, CharSet = CharSet.Ansi, Pack = 1)]
174 internal struct PT_DioWritePortByte
175 {
176 [FieldOffset(0)]
177 public short Port;
178 [FieldOffset(2)]
179 public short Mask;
180 [FieldOffset(4)]
181 public short State;
182 }
183
184 /**////
185 /// 读取IO设备的数据结构。
186 ///

187 [StructLayout(LayoutKind.Explicit, CharSet = CharSet.Ansi, Pack = 1)]
188 internal struct PT_DioReadPortByte
189 {
190 [FieldOffset(0)]
191 public short Port;
192 [FieldOffset(4)]
193 public IntPtr value;
194 }
195
196 /**////
197 /// 硬件调用的 API 函数封装类。
198 ///

199 public static class AdvantechAPI
200 {
201 /**////
202 /// 最大设备个数
203 ///

204 public const short MaxEntries = 255;
205 public const short MaxDevices = 255;
206 public const short MAX_DRIVER_NAME_LEN = 16;
207
208 interface#region interface
209
210 /**////
211 /// 获取设备列表。
212 ///

213 /// 返回的设备列表清单数组
214 /// 最大的设备数
215 /// 返回的设备数
216 /// 返回值为 0 则表示执行成功,否则执行失败。
217 [DllImport("adsapi32.dll", CharSet = CharSet.Ansi)]
218 static extern int DRV_DeviceGetList(IntPtr deviceList, Int16 maxEntries, ref short outEntries);
219
220 [DllImport("adsapi32.dll", CharSet = CharSet.Ansi)]
221 static extern int DRV_DeviceGetFeatures(int DriverHandle, ref PT_DeviceGetFeatures lpDeviceGetFeatures);
222
223 /**////
224 /// 获取设备列表个数,我就基本用不到它,姑且留着。
225 ///

226 ///
227 ///
228 [DllImport("adsapi32.dll", CharSet = CharSet.Ansi)]
229 public static extern int DRV_DeviceGetNumOfList(ref short numOfDevices);
230
231 /**////
232 /// 打开设备。
233 ///

234 /// 设备号
235 /// 设备句柄
236 ///
237 [DllImport("adsapi32.dll", CharSet = CharSet.Ansi)]
238 public static extern int DRV_DeviceOpen(uint deviceNum, ref int driverHandle);
239
240 /**////
241 /// 向端口写数字信号。
242 ///

243 ///
244 ///
245 ///
246 [DllImport("adsapi32.dll", CharSet = CharSet.Ansi)]
247 static extern int DRV_DioWritePortByte(int driverHandle, ref PT_DioWritePortByte lpDioWritePortByte);
248
249 [DllImport("adsapi32.dll", CharSet = CharSet.Ansi)]
250 static extern int DRV_DioReadPortByte(int driverHandle, ref PT_DioReadPortByte lpDioReadPortByte);
251
252 #endregion
253
254 /**////
255 /// 获取主机上安装的设备列表。
256 ///

257 /// 返回错误代码,如果无错误,则返回0。
258 public static int GetDeviceList(out PT_DEVLISTARRAY deviceList, ref short outEntries)
259 {
260 deviceList = new PT_DEVLISTARRAY();
261 int _DeviceListLenght = Marshal.SizeOf(deviceList);
262 IntPtr _DeviceListPoint = Marshal.AllocHGlobal(_DeviceListLenght);
263 //打开设备的检查过程
264 int errorCode = AdvantechAPI.DRV_DeviceGetList(_DeviceListPoint, MaxEntries, ref outEntries);
265 deviceList = (PT_DEVLISTARRAY)Marshal.PtrToStructure(_DeviceListPoint, typeof(PT_DEVLISTARRAY));
266 Marshal.FreeHGlobal(_DeviceListPoint);
267 return errorCode;
268 }
269
270 public static int GetFeatures(int deviceHandle, out DEVFEATURES outDevFeatures)
271 {
272 int iLength;
273 int ErrCde = 0;
274 PT_DeviceGetFeatures ptDevGetFeatures = new PT_DeviceGetFeatures();
275 outDevFeatures = new DEVFEATURES();
276
277 outDevFeatures.szDriverVer = "?";
278 outDevFeatures.szDriverName = "?";
279 iLength = Marshal.SizeOf(outDevFeatures);
280 //and reserve the space
281 IntPtr DevFeaturesPointer = Marshal.AllocHGlobal(iLength);
282 //Copy the pointer into the struct
283 ptDevGetFeatures.Buffer = DevFeaturesPointer;
284 //and get the features
285 ErrCde = DRV_DeviceGetFeatures(deviceHandle, ref ptDevGetFeatures);
286 if (ErrCde == 0)
287 {
288 outDevFeatures = (DEVFEATURES)Marshal.PtrToStructure(DevFeaturesPointer, typeof(DEVFEATURES));
289 }
290 else
291 {
292 //Error
293 }
294 Marshal.FreeHGlobal(DevFeaturesPointer);
295 return ErrCde;
296 }
297
298 /**////
299 /// 数字信号按端口输出。
300 ///

301 ///
302 ///
303 ///
304 ///
305 ///
306 public static int Digital_WriteByteToPort(int deviceHandle, short port, short value, short mask)
307 {
308 PT_DioWritePortByte data = new PT_DioWritePortByte();
309 data.Port = port;
310 data.State = value;
311 data.Mask = mask;
312 return DRV_DioWritePortByte(deviceHandle, ref data);
313 }
314
315 public static int Digital_WriteByteToPort(int deviceHandle, short port, short value)
316 {
317 return Digital_WriteByteToPort(deviceHandle, port, value, 255);
318 }
319
320 /**////
321 /// 从指定的端口获取数据。
322 ///

323 ///
324 ///
325 ///
326 ///
327 public static int Digital_ReadByteFromPort(int deviceHandle, short port, out short value)
328 {
329 int error = 0;
330 short tempValue = 0;
331 IntPtr vpoint = Marshal.AllocHGlobal(Marshal.SizeOf(tempValue));
332 try
333 {
334 Marshal.StructureToPtr(tempValue, vpoint, false);
335 PT_DioReadPortByte data = new PT_DioReadPortByte();
336 data.Port = port;
337 data.value = vpoint;
338 error = DRV_DioReadPortByte(deviceHandle, ref data);
339 tempValue = (short)Marshal.PtrToStructure(vpoint, typeof(short));
340 value = tempValue;
341 }
342 finally
343 {
344 Marshal.FreeHGlobal(vpoint);
345 }
346 return error;
347 }
348 }
349}
350


阅读全文
类别:硬件接口 查看评论
广告赞助商