Пример #1
0
        public static int PollModuleForResponse(int addr, ref byte[] bfrRecv)
        {
            var m = new I2CManager();

            bfrRecv[0] = 0xCC;
            int iCnt = 0;
            int fd   = GetFDFromAddress(addr);

            Console.WriteLine(string.Format("PollModuleForResponse : File descriptor : {0}", fd));
            while ((bfrRecv[0] == 0xCC) && iCnt < 80) // 80x50ms = 4s timeout
            {
                int retVal = m.I2CReadBlock(fd, ref bfrRecv);
                //int retVal = m.I2CReadBlockAddr(addr, ref bfrRecv);

                PrintBuffer(bfrRecv, "PollModuleForResponse");

                if (bfrRecv[0] == 0xCC)
                {
                    // No data, waiting 50ms to retry.
                    System.Threading.Thread.Sleep(50);
                }

                iCnt++;
            }

            if (bfrRecv[0] == 0xCC)
            {
                return(0);
            }
            else
            {
                return(-1);
            }
        }
Пример #2
0
        /// <summary>
        /// Polls a module for response.
        /// </summary>
        /// <param name="addr">Address of the module.</param>
        /// <param name="bfrRecv">Buffer with received data after method returns.</param>
        /// <returns>Status of communication. True if response is valid, false otherwise.</returns>
        protected virtual bool PollModuleForResponse(int addr, ref byte[] bfrRecv)
        {
            var m = new I2CManager();

            bfrRecv[0] = 0xCC;
            int iCnt = 0;

            System.Threading.Thread.Sleep(10); // Needed on RPi3 because it was too fast
            int fd = GetFDFromAddress(addr);

            while ((bfrRecv[0] == 0xCC) && iCnt < 80) // 80x50ms = 4s timeout
            {
                int retVal = m.I2CReadBlock(fd, ref bfrRecv);

                if (bfrRecv[0] == 0xCC)
                {
                    // No data, waiting 50ms to retry.
                    System.Threading.Thread.Sleep(50);
                }

                iCnt++;
            }

            LogBuffer(bfrRecv, "I2C data recv");

            return(bfrRecv[0] != 0xCC);
        }
Пример #3
0
        public static int GetFDFromAddress(int address)
        {
            var m = new I2CManager();

            if (address < 0x08 || address > 0x77)
            {
                return(0);
            }

            if (Addresses[address] == 0)
            {
                Addresses[address] = m.I2CSetup(address, 1);
            }

            return(Addresses[address]);
        }
Пример #4
0
        /// <summary>
        /// Sends a request to a module and then polls the module to get the response.
        /// </summary>
        /// <param name="addr">Module address.</param>
        /// <param name="buffer">I2C data to be sent, I2C data received after method returns.</param>
        /// <returns>Status of communication. True if response is valid, false otherwise.</returns>
        protected virtual bool RequestToModuleFromBuffer(int addr, ref byte[] buffer)
        {
            var m = new I2CManager();

            CRC16.PutCRC(ref buffer);

            LogBuffer(buffer, "I2C data sent");

            int fd = GetFDFromAddress(addr);
            int l  = m.I2CWriteBlock(fd, ref buffer);

            for (int i = 0; i < ICCConstants.PACKETSIZE; i++)
            {
                buffer[i] = 0;
            }

            return(PollModuleForResponse(addr, ref buffer));
        }
Пример #5
0
        /// <summary>
        /// Sends a request to a module and gets the response.
        /// </summary>
        /// <param name="addr">Address of the module.</param>
        /// <param name="data">Data to send.</param>
        /// <param name="group">Group of command to send.</param>
        /// <param name="command">Command to send.</param>
        /// <param name="datas">Response data.</param>
        /// <returns>Status of response. True if response acknowledges correctly group and command, false otherwise.</returns>
        public virtual bool RequestToModule(int addr, IBICCPData data, byte group, byte command, params byte[] datas)
        {
            var m = new I2CManager();

            int fd = GetFDFromAddress(addr);

            byte[] buffer = new byte[ICCConstants.PACKETSIZE];
            buffer[0] = group;
            buffer[1] = command;
            for (int i = 2; i < ICCConstants.PACKETSIZE; i++)
            {
                buffer[i] = 0;
            }

            for (int i = 0; i < datas.Length; i++)
            {
                if (i < ICCConstants.DATASIZE)
                {
                    buffer[i + 2] = datas[i];
                }
            }

            bool b = RequestToModuleFromBuffer(addr, ref buffer);

            if (!b)
            {
                return(false);
            }
            else if (buffer[0] != group || buffer[1] != command)
            {
                return(false);
            }
            else
            {
                for (int i = 0; i < ICCConstants.DATASIZE; i++)
                {
                    data.Data[i] = buffer[i + 2];
                }
                return(true);
            }
        }
Пример #6
0
        public static int RequestToModuleFromBuffer(int addr, ref byte[] buffer)
        {
            var m = new I2CManager();

            CRC16.PutCRC(ref buffer);
            int fd = GetFDFromAddress(addr);

            Console.WriteLine(string.Format("RequestToModuleFromBuffer : File descriptor : {0}", fd));
            int l = m.I2CWriteBlock(fd, ref buffer);

            //int l = m.I2CWriteBlockAddr(addr, ref buffer);

            PrintBuffer(buffer, "RequestToModuleFromBuffer");

            for (int i = 0; i < ICCConstants.PACKETSIZE; i++)
            {
                buffer[i] = 0;
            }

            return(PollModuleForResponse(addr, ref buffer));
        }
Пример #7
0
        static void Main(string[] args)
        {
            Modules   = new Dictionary <int, Module>();
            Addresses = new int[0x78];

            byte[] b = new byte[0x78];
            var    m = new I2CManager();
            int    i = m.I2CScan(1, ref b);

            Console.WriteLine(string.Format("{0} modules detected.", i));
            for (int l = 0; l < 0x78; l++)
            {
                if (b[l] != 0)
                {
                    Console.WriteLine(string.Format("Module detected at address {0}.", l));
                }
            }

            System.Threading.Thread.Sleep(2000);

            var answer = new BICCPData();

//            if (_BICCPManager.RequestToModule(addr, answer, ICCConstants.BICCP_GRP_CONF, ICCConstants.BICCP_CMD_CONF_VERSION) != 0)


            //int fd = GetFDFromAddress(0x08);
            //Console.WriteLine(string.Format("File descriptor : {0}", fd));

            byte[] buffer = new byte[ICCConstants.PACKETSIZE];
            buffer[0] = ICCConstants.BICCP_GRP_CONF;
            buffer[1] = ICCConstants.BICCP_CMD_CONF_VERSION;
            for (int l = 2; l < ICCConstants.PACKETSIZE; l++)
            {
                buffer[l] = 0;
            }

            //for (int l = 0; l < datas.Length; i++)
            //{
            //    if (i < ICCConstants.DATASIZE)
            //        buffer[l + 2] = datas[l];
            //}

            PrintBuffer(buffer, "Before call");

            int j = RequestToModuleFromBuffer(0x08, ref buffer);

            if (j == 0)
            {
                Console.WriteLine("RequestToModuleFromBuffer returned 0.");
            }
            else if (buffer[0] != ICCConstants.BICCP_GRP_CONF || buffer[1] != ICCConstants.BICCP_CMD_CONF_VERSION)
            {
                Console.WriteLine("RequestToModuleFromBuffer returned wrong group or command.");
            }
            else
            {
                Console.WriteLine("Correct return.");
                //for (int i = 0; i < ICCConstants.DATASIZE; i++)
                //    data.Data[i] = buffer[i + 2];
                //return -1;
            }


            buffer    = new byte[ICCConstants.PACKETSIZE];
            buffer[0] = ICCConstants.BICCP_GRP_CONF;
            buffer[1] = ICCConstants.BICCP_CMD_CONF_IDENT;
            for (int l = 2; l < ICCConstants.PACKETSIZE; l++)
            {
                buffer[l] = 0;
            }

            //for (int l = 0; l < datas.Length; i++)
            //{
            //    if (i < ICCConstants.DATASIZE)
            //        buffer[l + 2] = datas[l];
            //}

            PrintBuffer(buffer, "Before call");

            j = RequestToModuleFromBuffer(0x08, ref buffer);
            if (j == 0)
            {
                Console.WriteLine("RequestToModuleFromBuffer returned 0.");
            }
            else if (buffer[0] != ICCConstants.BICCP_GRP_CONF || buffer[1] != ICCConstants.BICCP_CMD_CONF_IDENT)
            {
                Console.WriteLine("RequestToModuleFromBuffer returned wrong group or command.");
            }
            else
            {
                Console.WriteLine("Correct return.");
                //for (int i = 0; i < ICCConstants.DATASIZE; i++)
                //    data.Data[i] = buffer[i + 2];
                //return -1;
            }
        }
Пример #8
0
        /// <summary>
        /// Scans the I2C bus.
        /// </summary>
        /// <param name="list">Array of bytes. Will contain status of module presence for each address (0 = no module, 1 = module found). It is mandaatory that the array has a size of 120 (0x78).</param>
        /// <returns>Quantity of modules found.</returns>
        public virtual int BICCP_ScanBus(ref byte[] list)
        {
            var m = new I2CManager();

            return(m.I2CScan(1, ref list));
        }