在CP210x C#中控制GPIO [英] Controlling GPIO in CP210x C#

查看:54
本文介绍了在CP210x C#中控制GPIO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用Silicon Labs提供的 CP210xManufacturing.dll CP210xRuntime.dll 来控制CP210x器件的GPIO引脚.我设法打开和关闭设备并获取部件号.(在我的情况下为CP2105).

I need to control the GPIO pins of a CP210x device using CP210xManufacturing.dll and CP210xRuntime.dll provided by Silicon Labs. I manage to open and close the device and to get the part number. (In my case a CP2105).

我认为我已成功读取了闩锁,但不确定.我还调用了写闩锁功能,没有错误返回,也没有任何引脚切换.

I think I successfully read the latch, but I'm not sure. I also call the write latch function, and no error is returned and neither do any pins toggle.

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

According to the provided utility (CP21xxCustomizationUtility.exe) it shows that both ports are in GPIO mode.

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

Here is my code: using System; using 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方法的函数:

and the function in my class calling the DLL methods:

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包装器中,但我无法弄清楚.

I do realise that the issue might be in the DLL wrapper, but I can't figure it out.

请参考 Windows数据类型.这有助于DLL包装.

Refer to Windows Data Types. This assists in the DLL wrapper.

请参考 CP21xxCustomizationUtility .其中包含DLL.

Refer to CP21xxCustomizationUtility. This contains the DLLs.

Google"silicon labs an169"以获取《USBXpress®程序员指南》.

Google "silicon labs an169" to get the USBXpress® Programmer’s Guide.

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

So the question is what is wrong here? How do I toggle the GPIO pins?

推荐答案

问题的答案是代码很好.该代码有效.问题是集成电路.该代码在CP2103上可以正常工作,但在CP2105上却不能很好地工作.看起来CP2105的设置不同.

The answer to the question is that the code is fine. The code works. The issue is the IC. The code works fine with the CP2103, but not so good with the CP2105. It seems like the CP2105 is setup differently.

这篇关于在CP210x C#中控制GPIO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆