我做了个东西,想在程序中给外部设备留一个接口,比如扫描仪、打印机等,可是不知道程序该怎么写,请各位大侠指点一下!

解决方案 »

  1.   


    Advantech 硬件控制卡的 c# 接口函数
    2009-04-09 21:05
    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     /**//// <summary>
    11     /// 安装在主机上所有的自动控制板卡设备结构。
    12     /// </summary>
    13     [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack=1)]
    14     public struct PT_DEVLIST
    15     {
    16         public UInt32 dwDeviceNum;
    17         /**//// <summary>
    18         /// 设备名,50个字符长。
    19         /// </summary>
    20         [MarshalAs(UnmanagedType.ByValTStr, SizeConst=50)] 
    21         public string szDeviceName;
    22         /**//// <summary>
    23         /// 设备的端口数,一般为8个端口。
    24         /// </summary>
    25         public Int16 nNumOfSubdevices;
    26     }
    27
    28     /**//// <summary>
    29     /// 用于 DRV_DeviceGetList 调用返回的设备列表结构。
    30     /// 这一块真难改呀~~,耗费了我一整个晚上的时间~~~~~~~
    31     /// 网上也找不到资料,最可气的是.Net带的函数都不支持结构数组,只好变相再来一个结构了~~
    32     /// xiefang 2007/09/02
    33     /// </summary>
    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     /**//// <summary>
    60     /// Define hardware board(device) features.
    61     /// </summary>
    62     [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
    63     public struct DEVFEATURES
    64     {
    65         /**//// <summary>
    66         /// device driver version, array[0..7]
    67         /// </summary>
    68         [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 8)]
    69         public string szDriverVer;
    70         /**//// <summary>
    71         /// device driver name, array[0..MAX_DRIVER_NAME_LEN-1]
    72         /// </summary>
    73         [MarshalAs(UnmanagedType.ByValTStr, SizeConst = AdvantechAPI.MAX_DRIVER_NAME_LEN)]
    74         public string szDriverName;
    75         /**//// <summary>
    76         /// board ID, DWORD 4 bytes
    77         /// </summary>
    78         public uint dwBoardID;
    79         /**//// <summary>
    80         /// Max. number of differential channel
    81         /// </summary>
    82         public ushort usMaxAIDiffChl;
    83         /**//// <summary>
    84         /// Max. number of single-end channel
    85         /// </summary>
    86         public ushort usMaxAISiglChl;
    87         /**//// <summary>
    88         /// Max. number of D/A channel
    89         /// </summary>
    90         public ushort usMaxAOChl;
    91         /**//// <summary>
    92         /// Max. number of digital out channel
    93         /// </summary>
    94         public ushort usMaxDOChl;
    95         /**//// <summary>
    96         /// Max. number of digital input channel
    97         /// </summary>
    98         public ushort usMaxDIChl;
    99         /**//// <summary>
    100         /// specifies if programmable or not
    101         /// </summary>
    102         public ushort usDIOPort;
    103         /**//// <summary>
    104         /// Max. number of Counter/Timer channel
    105         /// </summary>
    106         public ushort usMaxTimerChl;
    107         /**//// <summary>
    108         /// Max number of   alram channel
    109         /// </summary>
    110         public ushort usMaxAlarmChl;
    111         /**//// <summary>
    112         /// number of bits for A/D converter
    113         /// </summary>
    114         public ushort usNumADBit;
    115         /**//// <summary>
    116         /// A/D channel width in bytes.
    117         /// </summary>
    118         public ushort usNumADByte;
    119         /**//// <summary>
    120         /// number of bits for D/A converter. 
    121         /// </summary>
    122         public ushort usNumDABit;
    123         /**//// <summary>
    124         /// D/A channel width in bytes.
    125         /// </summary>
    126         public ushort usNumDAByte;
    127         /**//// <summary>
    128         /// Max. number of gain code
    129         /// </summary>
    130         public ushort usNumGain;
    131         /**//// <summary>
    132         /// Gain listing array[0..15]
    133         /// </summary>
    134         public GainListBlob glGainList;
    135         /**//// <summary>
    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         /// </summary>
    162         public uint dwPermutation0;
    163         public uint dwPermutation1;
    164         public uint dwPermutation2;
    165         public uint dwPermutation3;
    166     }
    167
    168     #endregion
    169
    170     /**//// <summary>
    171     /// 写入设备的数据结构。
    172     /// </summary>
    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     /**//// <summary>
    185     /// 读取IO设备的数据结构。
    186     /// </summary>
    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
      

  2.   


    196     /**//// <summary>
    197     /// 硬件调用的 API 函数封装类。
    198     /// </summary>
    199     public static class AdvantechAPI
    200     {
    201         /**//// <summary>
    202         /// 最大设备个数
    203         /// </summary>
    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         /**//// <summary>
    211         /// 获取设备列表。
    212         /// </summary>
    213         /// <param name="deviceList">返回的设备列表清单数组</param>
    214         /// <param name="maxEntries">最大的设备数</param>
    215         /// <param name="outEntries">返回的设备数</param>
    216         /// <returns>返回值为 0 则表示执行成功,否则执行失败。</returns>
    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         /**//// <summary>
    224         /// 获取设备列表个数,我就基本用不到它,姑且留着。
    225         /// </summary>
    226         /// <param name="numOfDevices"></param>
    227         /// <returns></returns>
    228         [DllImport("adsapi32.dll", CharSet = CharSet.Ansi)]
    229         public static extern int DRV_DeviceGetNumOfList(ref short numOfDevices);
    230
    231         /**//// <summary>
    232         /// 打开设备。
    233         /// </summary>
    234         /// <param name="deviceNum">设备号</param>
    235         /// <param name="driverHandle">设备句柄</param>
    236         /// <returns></returns>
    237         [DllImport("adsapi32.dll", CharSet = CharSet.Ansi)]
    238         public static extern int DRV_DeviceOpen(uint deviceNum, ref int driverHandle);
    239
    240         /**//// <summary>
    241         /// 向端口写数字信号。
    242         /// </summary>
    243         /// <param name="DriverHandle"></param>
    244         /// <param name="lpDioWritePortByte"></param>
    245         /// <returns></returns>
    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         /**//// <summary>
    255         /// 获取主机上安装的设备列表。
    256         /// </summary>
    257         /// <returns>返回错误代码,如果无错误,则返回0。</returns>
    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
     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         /**//// <summary>
    299         /// 数字信号按端口输出。
    300         /// </summary>
    301         /// <param name="deviceHandle"></param>
    302         /// <param name="port"></param>
    303         /// <param name="value"></param>
    304         /// <param name="mask"></param>
    305         /// <returns></returns>
    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         /**//// <summary>
    321         /// 从指定的端口获取数据。
    322         /// </summary>
    323         /// <param name="deviceHandle"></param>
    324         /// <param name="port"></param>
    325         /// <param name="value"></param>
    326         /// <returns></returns>
    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     }
      

  3.   

    在Java中该怎么写这些接口呢?
      

  4.   


    我应该没进错版吧,dot net版你问java啊。
    预留接口就是留串口和并口了,不过并口现在很少用了吧,似乎c#也写不动,留个串口收发就好了,不过这个还是需要你留下配置文件让用户自定义配置的,毕竟协议不一样,不可能写的那么通用,串口通信,参考吧。
      

  5.   

    就一个说的靠谱的...SerialPort都出来的,都怎么想的...
      

  6.   

    向zzxap致敬!!!太无私了,感激啊!!提供的这个类太好太好了