在CP210x C#中控制GPIO

时间:2017-05-11 11:55:49

标签: c# .net dll usb gpio

我需要使用Silicon Labs提供的CP210xManufacturing.dllCP210xRuntime.dll来控制CP210x器件的GPIO引脚。 我设法打开和关闭设备并获取部件号。 (就我而言,是CP2105)。

我想我已经成功读取了闩锁,但我不确定。 我也调用了写锁存功能,没有返回错误,也没有任何引脚切换。

根据提供的实用程序(CP21xxCustomizationUtility.exe),它显示两个端口都处于GPIO模式。

这是我的代码:     使用系统;     使用System.Runtime.InteropServices;

namespace CP210x
{
   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);
      }
   }
}

和我的类中调用DLL方法的函数:

private static void ResetTelit()
{
   Int32 numOfDevices = 0;
   Int32 retVal = CP210x.CP210x.GetNumDevices(ref numOfDevices);
   IntPtr handle = IntPtr.Zero;
   Byte[] prtNum = new Byte[1];
   UInt16[] latch = new UInt16[8];
   UInt16 mask = 0x01;
   if (numOfDevices > 0)
   {
      retVal = CP210x.CP210x.Open(0, ref handle);
      retVal = CP210x.CP210x.GetPartNumber(handle, prtNum);
      if (prtNum[0] == 5)
      {
         retVal = CP210x.CP210x.ReadLatch(handle, latch);  

         for (Int32 idx = 0; idx < 16; idx++)
         {
            retVal = CP210x.CP210x.WriteLatch(handle, (UInt16)(mask << idx), 0x01);
         }               
      }
      retVal = CP210x.CP210x.Close(handle);
   }
}

我确实知道问题可能出在DLL包装器中,但我无法弄明白。

请参阅Windows Data Types。这有助于DLL包装器。

请参阅CP21xxCustomizationUtility。这包含DLL。

谷歌“硅实验室an169”获得USBXpress®程序员指南。

所以问题是这里有什么问题?如何切换GPIO引脚?

0 个答案:

没有答案