Exemple #1
0
        /// <summary>
        /// 关闭设备
        /// </summary>
        /// <returns></returns>
        public bool closeDevice()
        {
            int temp = VCI_GYI2C.GYI2C_Close(deviceType, deviceInd);

            if (temp == 1)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Exemple #2
0
        /// <summary>
        /// 打开设备
        /// </summary>
        /// <returns></returns>
        public bool openDevice()
        {
            int temp = VCI_GYI2C.GYI2C_Open(deviceType, deviceInd, 115200);

            if (temp == 1)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Exemple #3
0
        private int writeBytes(byte SlaveAddr, int writeSize, int startAddress, int ClkValue, byte[] writeBuffer)
        {
            int writeBufferLength = writeBuffer.Length;
            int writeTimes        = writeBufferLength / writeSize;

            int writeMod = writeBufferLength % writeSize;

            if (writeMod > 0)
            {
                writeTimes = writeTimes + 1;
            }

            int writeCount = 0;

            for (int i = 0; i < writeTimes; i++)
            {
                GYI2C_DATA_INFO pDataInfo = new GYI2C_DATA_INFO();
                pDataInfo.IoData     = IoData;
                pDataInfo.IoSel      = IoSel;
                pDataInfo.SlaveAddr  = SlaveAddr;
                pDataInfo.Databuffer = new byte[520];
                pDataInfo.Reserved   = new byte[4] {
                    0, 0, 0, 0
                };
                pDataInfo.Databuffer[0] = (byte)(i * writeSize + startAddress);

                int writeLength = (writeSize < writeBufferLength - i * writeSize) ? writeSize : writeBufferLength - i * writeSize;
                for (int j = 0; j < writeLength; j++)
                {
                    pDataInfo.Databuffer[j + 1] = writeBuffer[i * writeSize + j];
                }
                pDataInfo.ReadNum   = 0;
                pDataInfo.DlyMsRead = 10 * (writeSize + 20) / ClkValue;    //留一点余量
                pDataInfo.WriteNum  = writeLength + 1;
                int len = VCI_GYI2C.GYI2C_Write(deviceType, deviceInd, ref pDataInfo);
                writeCount = writeCount + len;
                if (len != 1)
                {
                    break;
                }
            }

            return(writeCount);
        }
Exemple #4
0
        private int readBytes(byte SlaveAddr, int readSize, int startAddress, int ClkValue, ref byte[] readBuffer)
        {
            GYI2C_DATA_INFO pDataInfo = new GYI2C_DATA_INFO();

            pDataInfo.IoData     = IoData;
            pDataInfo.IoSel      = IoSel;
            pDataInfo.SlaveAddr  = SlaveAddr;
            pDataInfo.Databuffer = new byte[520];

            pDataInfo.Databuffer[0] = (byte)startAddress;
            pDataInfo.ReadNum       = readSize;
            pDataInfo.DlyMsRead     = 10 / ClkValue * (pDataInfo.ReadNum + 20); //留一点余量
            pDataInfo.WriteNum      = 1;

            int len = VCI_GYI2C.GYI2C_Read(deviceType, deviceInd, ref pDataInfo);

            if (len > 0)
            {
                Array.Copy(pDataInfo.Databuffer, readBuffer, len);
            }
            return(len);
        }
Exemple #5
0
        ///<summary>
        /// 写入数据
        /// </summary>
        /// <param name="slaveAdd">器件地址</param>
        /// <param name="startWriteAdd"></param>
        /// <param name="channel"></param>
        /// <param name="writeLen"></param>
        /// <param name="writeBuffer"></param>
        /// <returns></returns>
        public operationResult writeDevice(byte slaveAdd, byte startWriteAdd, byte channel, int writeLen, byte[] writeBuffer)
        {
            operationResult oR       = new operationResult();
            int             ClkValue = GYI2C_GetClk(deviceType, deviceInd);
            int             ChValue  = channel;
            int             temp     = VCI_GYI2C.GYI2C_SetChannel(deviceType, deviceInd, ChValue);

            if (temp == 0)
            {
                oR.result = false;
                oR.msg    = "fail to set channel" + ChValue.ToString();
            }
            else if (temp == -1)
            {
                oR.result = false;
                oR.msg    = "device is not open";
            }
            else
            {
                //temp = 1;
                int writedLen = writeBytes(slaveAdd, writeBlock, startWriteAdd, ClkValue, writeBuffer);
                if (writedLen == writeBuffer.Length)
                {
                    oR.result = true;
                    oR.msg    = "write success";
                }
                else if (writeLen < 0)
                {
                    oR.result = false;
                    oR.msg    = "device is not open!";
                }
                else
                {
                    oR.result = false;
                    oR.msg    = "write fail";
                }
            }
            return(oR);
        }
Exemple #6
0
        /// <summary>
        /// 读取数据
        /// </summary>
        /// <param name="slaveAdd">器件地址</param>
        /// <param name="startReadAdd">起始位置</param>
        /// <param name="channel">通道</param>
        /// <param name="readLen">读取长度</param>
        /// <returns></returns>
        public operationResult readDevice(byte slaveAdd, byte startReadAdd, byte channel, byte readLen)
        {
            operationResult oR       = new operationResult();
            int             ClkValue = GYI2C_GetClk(deviceType, deviceInd);
            int             ChValue  = channel;
            int             temp     = VCI_GYI2C.GYI2C_SetChannel(deviceType, deviceInd, ChValue);

            if (temp == 0)
            {
                oR.result = false;
                oR.msg    = "fail to set channel" + ChValue.ToString() + " !";
            }
            else if (temp == -1)
            {
                oR.result = false;
                oR.msg    = "device is not open!";
            }
            else
            {
                //temp = 1;
                byte[] readBuffer       = new byte[readLen];
                int    readBufferLength = readBytes(slaveAdd, readLen, startReadAdd, ClkValue, ref readBuffer);
                if (readBufferLength == readLen)
                {
                    oR.result     = true;
                    oR.msg        = "read success";
                    oR.bufferData = readBuffer;
                }
                else
                {
                    oR.result = false;
                    oR.msg    = "read fail";
                }
            }
            return(oR);
        }