advantech PCI-1727 的 C# 接口






using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace PCIDRV
{
    public partial class Adsapi
    {
        [DllImport("adsapi32.dll", CharSet = CharSet.Ansi)]
        public static extern int DRV_DeviceGetNumOfList(ref Int16 comm);
        [DllImport("adsapi32.dll", CharSet = CharSet.Ansi)]
        static extern int DRV_DeviceGetList(ref byte d, Int16 MaxEntries, ref Int16 OutEntries);
        [DllImport("adsapi32.dll", CharSet = CharSet.Ansi)]
        static extern int DRV_SelectDevice(IntPtr hCaller, bool GetModule, ref Int32 DeviceNum, ref byte Description);
        [DllImport("adsapi32.dll", CharSet = CharSet.Ansi)]
        public static extern int DRV_DeviceOpen(Int32 DeviceNum, ref Int32 DriverHandle);
        [DllImport("adsapi32.dll", CharSet = CharSet.Ansi)]
        public static extern int DRV_DeviceClose(ref Int32 DriverHandle);
        [DllImport("adsapi32.dll", CharSet = CharSet.Ansi)]
        unsafe static extern int DRV_DeviceGetProperty(Int32 DriverHandle, Int32 nPropertyID, byte* pBuffer, ref Int32 pLength);
        [DllImport("adsapi32.dll", CharSet = CharSet.Ansi)]
        public static extern int DRV_AOConfig(Int32 DriverHandle, ref AOConfig OConfig);
        [DllImport("adsapi32.dll", CharSet = CharSet.Ansi)]
        public static extern int DRV_AOBinaryOut(Int32 DriverHandle, ref AOBinaryOut Out);
        [DllImport("adsapi32.dll", CharSet = CharSet.Ansi)]
        unsafe static extern void DRV_GetErrorMessage(Int32 ErrorCode, ref byte ErrorMsg);
        [DllImport("adsapi32.dll", CharSet = CharSet.Ansi)]
        unsafe static extern int DRV_AOScale(Int32 DriverHandle, ref AOScale Aos);
        [DllImport("adsapi32.dll", CharSet = CharSet.Ansi)]
        unsafe static extern int DRV_DeviceGetFeatures(Int32 DriverHandle, ref PT_DeviceGetFeatures lpDevFeatures);
        [DllImport("adsapi32.dll", CharSet = CharSet.Ansi)]
        static extern int DRV_AOVoltageOut(Int32 DriverHandle, AOVoltageOut Ao);
        [DllImport("adsapi32.dll", CharSet = CharSet.Ansi)]
        static extern int DRV_AOCurrentOut(Int32 DriverHandle, AOVoltageOut Ao);
        [DllImport("adsapi32.dll", CharSet = CharSet.Ansi)]
        static extern int DRV_WriteSyncAO(Int32 DriverHandle);
        [DllImport("adsapi32.dll", CharSet = CharSet.Ansi)]
        static extern int DRV_EnableSyncAO(Int32 DriverHandle, ref Int32 Enable);
        [DllImport("adsapi32.dll", CharSet = CharSet.Ansi)]
        unsafe static extern int AdxDioReadDiPorts(Int32 DriverHandle, UInt32 dwPortStart, UInt32 dwPortCoun, byte* pBuffer);

        [DllImport("adsapi32.dll", CharSet = CharSet.Ansi)]
        unsafe static extern int AdxDioWriteDoPorts(Int32 DriverHandle, UInt32 dwPortStart, UInt32 dwPortCoun, byte* pBuffer);
        [DllImport("adsapi32.dll", CharSet = CharSet.Ansi)]
        unsafe static extern int AdxDioGetCurrentDoPortsState(Int32 DriverHandle, UInt32 dwPortStart, UInt32 dwPortCoun, byte* pBuffer);

 

        unsafe public static int DRV_DeviceGetFeatures(Int32 DriverHandle, out DevFeatures df)
        {
            byte[] buff = new byte[1000];
            DEVFEATURES d = new DEVFEATURES();
            PT_DeviceGetFeatures Gf = new PT_DeviceGetFeatures();

            int r;
            fixed (byte* p = buff)
            {
                Gf.buffer = (IntPtr)p;
                Gf.size = 1000;
                r = DRV_DeviceGetFeatures(DriverHandle, ref Gf);
                DEVFEATURES* K = (DEVFEATURES*)p;
                d = *K;
                df = new DevFeatures(d);
            }
            return r;
        }

        unsafe public static int DRV_AOScale(Int32 DriverHandle, ushort channel, float outvalue, out ushort DataBin)
        {
            AOScale a = new AOScale();
            ushort B;
            ushort* p;
            int r;
            a.chan = channel;
            a.OutputValue = outvalue;
            p = &B;
            a.pt = (IntPtr)p;
            r = DRV_AOScale(DriverHandle, ref a);
            DataBin = B;
            return r;
        }

        public unsafe static string DRV_GetErrorMessage(Int32 ErrorCode)
        {
            byte[] buff = new byte[200];
            string f;
            fixed (byte* p = buff)
            {
                DRV_GetErrorMessage(ErrorCode, ref  buff[0]);
                f = Marshal.PtrToStringAnsi((IntPtr)(p));
            }
            return f;

        }
        unsafe public static int GetDevicePropertyID(Int32 DriverHandle, ref Int32 BoardI, PropertyID Property)
        {
            int r;
            int iBoardID = BoardI;
            int l = 4;
            byte* p = (byte*)&iBoardID;
            r = DRV_DeviceGetProperty(DriverHandle, (Int32)(Property), p, ref l);
            BoardI = iBoardID;

            return r;
        }


        unsafe public static int DRV_DeviceGetList(out DevList[] h)
        {
            byte[] buff = new byte[1000];
            DevList[] s;

            Int16 OutLines = 0;
            Int16 Max = 1000;
            int r;
            fixed (byte* p = buff)
            {
                r = DRV_DeviceGetList(ref  buff[0], Max, ref OutLines);
                s = new DevList[OutLines];
                for (int i = 0; i < OutLines; i++)
                {
                    DevList x = new DevList();
                    byte* su = (byte*)p + i * 56;
                    x.dwDeviceNum = Marshal.ReadInt32((IntPtr)(su));
                    su += 4;
                    x.DeviceName = Marshal.PtrToStringAnsi((IntPtr)(su));
                    su += 50;
                    x.nNumOfSubDevices = Marshal.ReadInt16((IntPtr)(su));
                    s[i] = x;
                }

            }
            h = s;
            return r;
        }
        unsafe static public int DRV_SelectDevice(IntPtr hCaller, bool GetModule, ref   Int32 DeviceNum, out string Description)
        {

            byte[] dec = new byte[200];

            int r;
            fixed (byte* p = dec)
            {
                r = Adsapi.DRV_SelectDevice(hCaller, false, ref DeviceNum, ref dec[0]);
                if (r == 0)
                    Description = Marshal.PtrToStringAnsi((IntPtr)(p));
                else
                    Description = null;
            }

            return r;
        }

 

    }

 


}

posted @ 2008-06-01 21:21  km  阅读(1902)  评论(0编辑  收藏  举报