您的位置:首页 > 编程语言 > C#

C# 控制cp2103芯片 GPIO口

2018-04-09 09:58 453 查看
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace CP2103
{
    class Program
    {
        static void Main(string[] args)
        {
            Int32 numOfDevices = 0;
            Int32 retVal = CP210x.GetNumDevices(ref numOfDevices);
            IntPtr handle = IntPtr.Zero;
            Byte[] prtNum = new Byte[1];
            UInt16[] latch = new UInt16[4];
            UInt16 mask = 0x01;
            if (numOfDevices > 0)
            {
                retVal = CP210x.Open(0, ref handle);
                retVal = CP210x.GetPartNumber(handle, prtNum);
                if (prtNum[0] == 3)     //cp21"03"
                {
                   
                    //设置GPIO为101
                    retVal = CP210x.WriteLatch(handle, (UInt16)(mask << 0), (UInt16)(0x01 << 0));
                    retVal = CP210x.WriteLatch(handle, (UInt16)(mask << 1), (UInt16)(0x00 << 1));
                    retVal = CP210x.WriteLatch(handle, (UInt16)(mask << 2), (UInt16)(0x01 << 2));

                }
                retVal = CP210x.ReadLatch(handle, latch);
                retVal = CP210x.Close(handle);
            }
            
                Console.WriteLine(latch[0]);
            
            Console.WriteLine(retVal.ToString());

        }
        
    }
    public class CP210x
    {
        [DllImport("CP210xManufacturing.dll")]
        private static extern Int32 CP210x_GetNumDevices(ref Int32 numOfDevices);
        public static Int32 GetNumDevices(ref Int32 numOfDevices)
        {
            return CP210x_GetNumDevices(ref numOfDevices);
        }

        [DllImport("CP210xManufacturing.dll")]
        private static extern Int32 CP210x_Open(Int32 deviceNum, ref IntPtr handle);
        public static Int32 Open(Int32 deviceNum, ref IntPtr handle)
        {
            return CP210x_Open(deviceNum, ref handle);
        }

        [DllImport("CP210xManufacturing.dll")]
        private static extern Int32 CP210x_Close(IntPtr handle);
        public static Int32 Close(IntPtr handle)
        {
            return CP210x_Close(handle);
        }

        [DllImport("CP210xManufacturing.dll")]
        private static extern Int32 CP210x_GetPartNumber(IntPtr handle, Byte[] lpbPartNum);
        public static Int32 GetPartNumber(IntPtr handle, Byte[] lpbPartNum)
        {
            return CP210x_GetPartNumber(handle, lpbPartNum);
        }

        [DllImport("CP210xRuntime.dll")]
        private static extern Int32 CP210xRT_WriteLatch(IntPtr handle, UInt16 mask, UInt16 latch);
        public static Int32 WriteLatch(IntPtr handle, UInt16 mask, UInt16 latch)
        {
            return CP210xRT_WriteLatch(handle, mask, latch);
        }

        [DllImport("CP210xRuntime.dll")]
        private static extern Int32 CP210xRT_ReadLatch(IntPtr handle, UInt16[] lpLatch);
        public static Int32 ReadLatch(IntPtr handle, UInt16[] lpLatch)
        {
            return CP210xRT_ReadLatch(handle, lpLatch);
        }
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: