Пример #1
0
        private void i2cQueryButton_Click(object sender, EventArgs e)
        {
            byte   i2cAddr    = 0;
            byte   regAddr    = 0;
            byte   numOfBytes = 0;
            string inputStr   = i2cAddrTextBox.Text;

            string[] strAry = null;

            string ipstr    = ipAddrTextBox.Text;
            string gpibAddr = "inst30";

            string     resp;
            VXI11Class remote_inst = new VXI11Class(gpibAddr, ipstr);

            if (inputStr == string.Empty)
            {
                return;
            }

            if (inputStr.Contains(@"/"))
            {
                strAry  = inputStr.Split(new char[] { '/' });
                i2cAddr = Convert.ToByte(strAry[strAry.Length - 1], 16);
            }
            else
            {
                i2cAddr = Convert.ToByte(inputStr);
            }

            Console.WriteLine("i2c addr: " + i2cAddr.ToString());
            //
            inputStr = i2cRegisterTextBox.Text;
            if (inputStr == string.Empty)
            {
                return;
            }
            if (inputStr.Contains(@"/"))
            {
                strAry  = inputStr.Split(new char[] { '/' });
                regAddr = Convert.ToByte(strAry[strAry.Length - 1], 16);
            }
            else
            {
                regAddr = Convert.ToByte(inputStr);
            }

            Console.WriteLine("i2c register: " + regAddr.ToString());

            inputStr = i2cNumOfBytesTextBox.Text;

            if (inputStr == string.Empty)
            {
                return;
            }
            if (inputStr.Contains(@"/"))
            {
                strAry     = inputStr.Split(new char[] { '/' });
                numOfBytes = Convert.ToByte(strAry[strAry.Length - 1], 16);
            }
            else
            {
                numOfBytes = Convert.ToByte(inputStr);
            }

            Console.WriteLine("number of bytes I2C read: " + numOfBytes.ToString());


            byte[] data = new byte[numOfBytes];

            i2cRespTextBox.AppendText("i2c read->addr: " + i2cAddr.ToString() + ", reg: " + regAddr.ToString() + "\r\n");

            if (numOfBytes > 255)
            {
                i2cRespTextBox.AppendText(numOfBytes.ToString() + " bytes is greater than max 255 allowed" + "\r\n");
                remote_inst.close();
                return;
            }
            try
            {
                remote_inst.readI2c(i2cAddr, regAddr, Convert.ToByte(numOfBytes), ref data);
            }
            catch (Exception ex)
            {
                resp = ex.Message;
                i2cRespTextBox.AppendText(resp + "\r\n");
                remote_inst.close();
                return;
            }

            for (int j = 0; j < numOfBytes; j++)
            {
                resp = j.ToString() + ": " + String.Format("0x{0:X}", data[j]);
                i2cRespTextBox.AppendText(resp + "\r\n");
            }

            i2cRespTextBox.AppendText("\r\n");

            remote_inst.close();
            i2cComboBox.Items.Add(inputStr);
        }