title: Controlling GPIO in CP210x C#
body: <p>I need to control the GPIO pins of a CP210x device using <code>CP210xManufacturing.dll</code> and <code>CP210xRuntime.dll</code> provided by Silicon Labs.
  I manage to open and close the device and to get the part number.  (In my case a CP2105).</p>
  
  <p>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.</p>
  
  <p>According to the provided utility (CP21xxCustomizationUtility.exe) it shows that both ports are in GPIO mode.</p>
  
  <p>Here is my code:
      using System;
      using System.Runtime.InteropServices;</p>
  
  <pre><code>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);
        }
     }
  }
  </code></pre>
  
  <p>and the function in my class calling the DLL methods:</p>
  
  <pre><code>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 &gt; 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 &lt; 16; idx++)
           {
              retVal = CP210x.CP210x.WriteLatch(handle, (UInt16)(mask &lt;&lt; idx), 0x01);
           }               
        }
        retVal = CP210x.CP210x.Close(handle);
     }
  }
  </code></pre>
  
  <p>I do realise that the issue might be in the DLL wrapper, but I can't figure it out.</p>
  
  <p>Refer to <a href="https://msdn.microsoft.com/en-us/library/aa383751(v=vs.85).aspx" rel="nofollow noreferrer">Windows Data Types</a>.  This assists in the DLL wrapper.</p>
  
  <p>Refer to <a href="http://www.silabs.com/Support%20Documents/Software/AN721SW.zip" rel="nofollow noreferrer">CP21xxCustomizationUtility</a>.  This contains the DLLs.</p>
  
  <p>Google "silicon labs an169" to get the USBXpress® Programmer’s Guide.</p>
  
  <p>So the question is what is wrong here?  How do I toggle the GPIO pins?</p>