Пример #1
0
        private int _Mcp2221Disconnect()
        {
            int result;

            if (ipHandler == IntPtr.Zero)
            {
                return(0);
            }

            if (as_atAdapterType != AdapterSelector.AdapterType.AS_AT_MCP2221)
            {
                return(-1);
            }

            result = Mcp2221Api.Mcp2221CloseApi(ipHandler);
            if (result < 0)
            {
                return(-1);
            }

            ipHandler        = IntPtr.Zero;
            as_atAdapterType = AdapterSelector.AdapterType.AS_AT_DUMMY;

            return(0);
        }
Пример #2
0
        public int UpdateMcp2221AdapterApi()
        {
            ushort[] deviceIndex        = new ushort[16];
            string[] deviceSerialNumber = new string[16];
            string   tmp;
            int      i, count;

            lMcp2221Adapter.Clear();

            // Find all the attached devices
            count = Mcp2221Api.Mcp2221FindDevicesExtApi(16, deviceIndex, 16, deviceSerialNumber);

            if (count == 0)
            {
                lMcp2221Adapter.Add("Cannot find any adapter!!");
                return(0);
            }

            for (i = 0; i < count; i++)
            {
                tmp = deviceIndex[i].ToString() + "  : " + deviceSerialNumber[i].ToString();
                lMcp2221Adapter.Add(tmp);
            }

            return(0);
        }
Пример #3
0
        private int _Mcp2221Connect(int port)
        {
            int result;

            if ((port < 0) || (port > 16))
            {
                return(-1);
            }

            if (ipHandler != IntPtr.Zero)
            {
                goto Exit;
            }

            ipHandler = Mcp2221Api.Mcp2221OpenByIndexApi(Convert.ToUInt32(port));
            if (ipHandler == IntPtr.Zero)
            {
                MessageBox.Show("Please check I2C adapter connect!!");
                goto Error;
            }

            // Set the bitrate
            result = Mcp2221Api.Mcp2221SetBitrateApi(ipHandler, Convert.ToUInt32(iBitrate) * 1000);
            if (result < 0)
            {
                goto Error;
            }

Exit:
            return(0);

Error:
            return(-1);
        }
Пример #4
0
        private int _Mcp2221Write(byte devAddr, byte regAddr, byte length, byte[] data)
        {
            byte[] buf = new byte[length + 1];

            buf[0] = regAddr;
            Array.Copy(data, 0, buf, 1, length);

            Mcp2221Api.Mcp2221I2cWriteApi(ipHandler, Convert.ToUInt32(length + 1), devAddr, 1, buf);

            return(0);
        }
Пример #5
0
        private int _AardvarkRead16(byte devAddr, byte[] regAddr, byte length, byte[] data)
        {
            byte[] reg = { regAddr[0], regAddr[1] };
            int    result;

            result = Mcp2221Api.Mcp2221I2cWriteApi(ipHandler, 2, devAddr, 1, reg);
            if (result < 0)
            {
                return(-1);
            }

            result = Mcp2221Api.Mcp2221I2cReadApi(ipHandler, length, devAddr, 1, data);
            if (result < 0)
            {
                return(-1);
            }

            return(length);
        }
Пример #6
0
        private int _Mcp2221Read(byte devAddr, byte regAddr, byte length, byte[] data)
        {
            byte[] reg = { regAddr };
            int    result;

            result = Mcp2221Api.Mcp2221I2cWriteApi(ipHandler, 1, devAddr, 1, reg);
            if (result < 0)
            {
                return(result);
            }

            result = Mcp2221Api.Mcp2221I2cReadApi(ipHandler, length, devAddr, 1, data);
            if (result < 0)
            {
                return(-3);
            }

            return(length);
        }
Пример #7
0
        public int ReadRawApi(byte devAddr, byte length, byte[] data)
        {
            int result;

            switch (as_atAdapterType)
            {
            case AdapterSelector.AdapterType.AS_AT_DUMMY:
                break;

            case AdapterSelector.AdapterType.AS_AT_AARDVARK:
                if (iHandler <= 0)
                {
                    if (ConnectApi() < 0)
                    {
                        MessageBox.Show("I2cMasterConnectApi() fail!!");
                    }
                    return(-1);
                }
                return(AardvarkApi.aa_i2c_read(iHandler, devAddr, AardvarkI2cFlags.AA_I2C_NO_FLAGS, length, data));

            case AdapterSelector.AdapterType.AS_AT_MCP2221:
                if (ipHandler == IntPtr.Zero)
                {
                    if (ConnectApi() < 0)
                    {
                        MessageBox.Show("I2cMasterConnectApi() fail!!");
                    }
                    return(-1);
                }
                result = Mcp2221Api.Mcp2221I2cReadApi(ipHandler, length, devAddr, 1, data);
                if (result < 0)
                {
                    return(-3);
                }

                return(length);

            default:
                break;
            }

            return(0);
        }
Пример #8
0
        public int SetTimeoutApi(UInt16 timeout)
        {
            switch (as_atAdapterType)
            {
            case AdapterSelector.AdapterType.AS_AT_DUMMY:
                break;

            case AdapterSelector.AdapterType.AS_AT_AARDVARK:
                if (iHandler <= 0)
                {
                    if (ConnectApi() < 0)
                    {
                        MessageBox.Show("I2cMasterConnectApi() fail!!");
                    }
                    return(-1);
                }
                return(AardvarkApi.aa_i2c_bus_timeout(iHandler, timeout));

            case AdapterSelector.AdapterType.AS_AT_MCP2221:
                if (timeout > 0xFF)
                {
                    return(-1);
                }

                if (ipHandler == IntPtr.Zero)
                {
                    if (ConnectApi() < 0)
                    {
                        MessageBox.Show("I2cMasterConnectApi() fail!!");
                    }
                    return(-1);
                }

                return(Mcp2221Api.Mcp2221SetAdvancedCommParamsApi(ipHandler, Convert.ToByte(timeout), 1));

            default:
                break;
            }

            return(-1);
        }
Пример #9
0
        public int WriteRawApi(byte devAddr, byte length, byte[] data)
        {
            switch (as_atAdapterType)
            {
            case AdapterSelector.AdapterType.AS_AT_DUMMY:
                break;

            case AdapterSelector.AdapterType.AS_AT_AARDVARK:
                if (iHandler <= 0)
                {
                    if (ConnectApi() < 0)
                    {
                        MessageBox.Show("I2cMasterConnectApi() fail!!");
                    }
                    return(-1);
                }
                AardvarkApi.aa_i2c_write(iHandler, devAddr, AardvarkI2cFlags.AA_I2C_NO_FLAGS, length, data);
                AardvarkApi.aa_sleep_ms(100);
                return(0);

            case AdapterSelector.AdapterType.AS_AT_MCP2221:
                if (ipHandler == IntPtr.Zero)
                {
                    if (ConnectApi() < 0)
                    {
                        MessageBox.Show("I2cMasterConnectApi() fail!!");
                    }
                    return(-1);
                }
                Mcp2221Api.Mcp2221I2cWriteApi(ipHandler, Convert.ToUInt32(length), devAddr, 1, data);
                return(0);

            default:
                break;
            }

            return(0);
        }