Beispiel #1
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();

            if (!setChannel(channel))
            {
                oR.result = false;
                oR.msg    = EEPROM.msgDeviceChannelSetFail;
                return(oR);
            }

            CP2112_DLL.HID_SMBUS_STATUS ret = Write3(slaveAdd, (ushort)writeLen, startWriteAdd, writeBuffer);

            if (CP2112_DLL.HID_SMBUS_STATUS.HID_SMBUS_SUCCESS ==
                ret)
            {
                oR.result = true;
                oR.msg    = EEPROM.msgWriteSuccess;
            }
            else if (CP2112_DLL.HID_SMBUS_STATUS.HID_SMBUS_WRITE_COMAPARE_FAIL == ret)
            {
                oR.result = false;
                oR.msg    = EEPROM.msgWriteCheckFail;
            }
            else
            {
                oR.result = false;
                oR.msg    = EEPROM.msgWriteFail;
            }
            return(oR);
        }
Beispiel #2
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);
        }
Beispiel #3
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             writeTimes = writeLen / writeBlock;
            int             writeMod   = writeLen % writeBlock;

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

            for (int i = 0; i < writeTimes; i++)
            {
                int    writeLength      = (writeBlock < (writeLen - i * writeBlock)) ? writeBlock : (writeLen - i * writeBlock);
                byte[] writeBufferBlock = new byte[writeLength];
                for (int j = 0; j < writeLength; j++)
                {
                    writeBufferBlock[j] = writeBuffer[i * writeBlock + j];
                }


                operationResult oRtemp = sendData(slaveAdd, (byte)(startWriteAdd + i * writeBlock), channel, (byte)writeLength, sp, writeBufferBlock);
                if (!oRtemp.result)
                {
                    writeResult = false;
                    oR.msg      = oRtemp.msg;
                    break;
                }
            }
            oR.result = writeResult;
            if (writeResult)
            {
                oR.msg = EEPROM.msgWriteSuccess;
            }


            return(oR);
        }
Beispiel #4
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();

            if (!setChannel(channel))
            {
                oR.result = false;
                oR.msg    = EEPROM.msgDeviceChannelSetFail;
                return(oR);
            }
            int readLength = 0;

            if (readLen == 0)
            {
                readLength = 256;
            }
            else
            {
                readLength = readLen;
            }

            byte[] readBuffer = new byte[readLength];

            if (CP2112_DLL.HID_SMBUS_STATUS.HID_SMBUS_SUCCESS ==
                Read3(slaveAdd, (ushort)readLength, startReadAdd, ref readBuffer))
            {
                oR.result     = true;
                oR.msg        = EEPROM.msgReadSuccess;
                oR.bufferData = readBuffer;
            }
            else
            {
                oR.result = false;
                oR.msg    = EEPROM.msgReadFail;
            }
            return(oR);
        }
Beispiel #5
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);
        }
Beispiel #6
0
        private operationResult sendData(byte slaveAdd, byte startReadAdd, byte channel, byte readLen, SerialPort sp, byte[] writeBuffer = null)
        {
            operationResult oR     = new operationResult();
            commandFrame    cFRead = new commandFrame();

            if (writeBuffer == null)
            {
                cFRead.cmd_type = read;
            }
            else
            {
                cFRead.cmd_type = write;
            }

            cFRead.cmd_slaves = slaveAdd;
            cFRead.addr       = startReadAdd;
            cFRead.cmd_bus    = channel;
            cFRead.length     = readLen;
            cFRead.param      = writeBuffer;

            if (!sp.IsOpen)
            {
                oR.result = false;
                oR.msg    = EEPROM.msgDeviceNotOpen;
                return(oR);
            }
            byte[] writeData = cFRead.convertToByteArray();
            sp.Write(writeData, 0, writeData.Length);

            int countTime = 0;

            while (sp.BytesToRead == 0)
            {
                Thread.Sleep(10);
                countTime++;
                if (countTime > 10)
                {
                    oR.result = false;
                    oR.msg    = EEPROM.msgDeviceTimeOut;
                    return(oR);
                }
            }
            if (cFRead.cmd_type == read)
            {
                Thread.Sleep(1000);
            }
            else
            {
                Thread.Sleep(timeOut);
            }

            byte[] recData = new byte[sp.BytesToRead];
            sp.Read(recData, 0, recData.Length);

            byte[] operationHead = new byte[8];
            Array.Copy(recData, operationHead, 8);

            if (Util.arrayCompare(operationHead, cmd_error))
            {
                //命令格式错误
                oR.result = false;
                oR.msg    = EEPROM.msgCMDError;
                return(oR);
            }

            if (Util.arrayCompare(operationHead, cmd_read_fail))
            {
                //读取失败
                oR.result = false;
                oR.msg    = EEPROM.msgReadFail;
                return(oR);
            }

            if (Util.arrayCompare(operationHead, cmd_write_fail))
            {
                //写入失败
                oR.result = false;
                oR.msg    = EEPROM.msgWriteFail;
                return(oR);
            }

            if (Util.arrayCompare(operationHead, cmd_write_success))
            {
                //写入成功
                oR.result = true;
                oR.msg    = EEPROM.msgWriteSuccess;
                return(oR);
            }

            if (cFRead.cmd_type == read)
            {
                oR.result = true;
                oR.msg    = EEPROM.msgReadSuccess;
                int    dataLength = readLen == 0 ? 256 : readLen;
                byte[] readData   = new byte[dataLength];
                Array.Copy(recData, 8, readData, 0, dataLength);
                oR.bufferData = readData;
            }

            return(oR);
        }