Beispiel #1
0
 public static byte[] I2CRead(byte i2caddr, uint length)
 {
     lock (locker)
     {
         int    rtval;
         byte[] dbuffer = new byte[length];
         rtval = MCP2221.M_Mcp2221_I2cRead(mHandle, length, i2caddr, SEVENBITADDR, dbuffer);
         if (rtval != I2C_NO_ERROR)
         {
             MCP2221.M_Mcp2221_I2cCancelCurrentTransfer(mHandle);
         }
         return(dbuffer);
     }
 }
Beispiel #2
0
 public static int I2CWriteAddr(byte i2caddr, byte regaddr)
 {
     lock (locker)
     {
         int    rtval;
         byte[] dbuffer = new byte[1];
         dbuffer[0] = regaddr;
         rtval      = MCP2221.M_Mcp2221_I2cWrite(mHandle, (uint)dbuffer.Length, i2caddr, SEVENBITADDR, dbuffer);
         if (rtval != I2C_NO_ERROR)
         {
             MCP2221.M_Mcp2221_I2cCancelCurrentTransfer(mHandle);
         }
         return(rtval);
     }
 }
Beispiel #3
0
        private static int OpenbyIndex()
        {
            int  rtval;
            uint nDev = 0;

            DeviceInfo.LibraryVersion = MCP2221.M_Mcp2221_GetLibraryVersion();
            rtval = MCP2221.M_Mcp2221_GetConnectedDevices(vid, pid, ref nDev);
            if (rtval == MCP2221.M_E_NO_ERR)
            {
                mHandle = MCP2221.M_Mcp2221_OpenByIndex(vid, pid, nDev - 1);
                rtval   = MCP2221.M_Mcp2221_GetLastError();
                if (rtval == MCP2221.M_E_NO_ERR)
                {
                    DeviceInfo.Manufacture = MCP2221.M_Mcp2221_GetManufacturerDescriptor(mHandle);
                    DeviceInfo.Product     = MCP2221.M_Mcp2221_GetProductDescriptor(mHandle);
                    DeviceInfo.Serial      = MCP2221.M_Mcp2221_GetSerialNumberDescriptor(mHandle);
                    DeviceInfo.FactorySN   = MCP2221.M_Mcp2221_GetFactorySerialNumber(mHandle);
                    DeviceInfo.HardwareVer = MCP2221.M_Mcp2221_GetHardwareRevision(mHandle);
                    DeviceInfo.FirmwareRev = MCP2221.M_Mcp2221_GetFirmwareRevision(mHandle);
                    rtval = MCP2221.M_Mcp2221_GetLastError();
                    if (rtval == MCP2221.M_E_NO_ERR)
                    {
                        int rval = MCP2221.M_Mcp2221_SetSpeed(mHandle, i2cspeed);
                        int loop = 100;
                        while (rval != MCP2221.M_E_NO_ERR)
                        {
                            MCP2221.M_Mcp2221_I2cCancelCurrentTransfer(mHandle);
                            MCP2221.M_Mcp2221_Reset(mHandle);
                            MCP2221.M_Mcp2221_GetConnectedDevices(vid, pid, ref nDev);
                            mHandle = MCP2221.M_Mcp2221_OpenByIndex(vid, pid, nDev - 1);
                            rval    = MCP2221.M_Mcp2221_SetSpeed(mHandle, i2cspeed);
                            loop--;
                            if (loop == 0)
                            {
                                break;
                            }
                        }
                    }
                }
            }
            return(rtval);
        }