Beispiel #1
0
        //private void RunScriptFile_Click(object sender, EventArgs e)
        //{

        //}

        private void RunI2CScript_Click(object sender, EventArgs e)
        {
            OpenFileDialog openXmlFileDialog = new OpenFileDialog();

            openXmlFileDialog.Title            = "Select I2C Script file";
            openXmlFileDialog.Multiselect      = false;
            openXmlFileDialog.InitialDirectory = Application.StartupPath;//@"C:\lexar1\csharp\CS2010\"; // @"E:\LEXAR1\tests\";
            //openXmlFileDialog.Filter = "xml files (*.xml)|*.xml | hex files (*.hex)|*.hex";
            openXmlFileDialog.Filter = "csv files |*.csv";
            openXmlFileDialog.ShowDialog();
            string[] fileNames = openXmlFileDialog.FileNames;
            string   fileName;
            byte     i2cAddr = 0;
            byte     i2cReg  = 0;

            byte[] i2cData = null;
            int    j       = 0;

            if (fileNames.Length > 0)
            {
                string ipstr    = ipAddrTextBox.Text;
                string gpibAddr = "inst30";

                string       resp;
                VXI11Class   remote_inst = new VXI11Class(gpibAddr, ipstr);
                StreamReader reader      = new StreamReader(fileNames[0]);
                while (!reader.EndOfStream)
                {
                    string   line = reader.ReadLine();
                    string[] ary  = line.Split(new char[] { ',' });
                    if (ary.Length >= 3) // min is i2cAddr, i2cReg, data
                    {
                        i2cAddr = Convert.ToByte(ary[0], 16);
                        i2cReg  = Convert.ToByte(ary[1], 16);
                        j       = ary.Length - 2;
                        i2cData = new byte[j];
                        for (int k = 0; k < j; k++)
                        {
                            i2cData[k] = Convert.ToByte(ary[2 + k], 16);
                        }
                        // ready to write
                        try
                        {
                            Console.WriteLine(Convert.ToByte(i2cData.Length));
                            remote_inst.writeI2c(i2cAddr, i2cReg, Convert.ToByte(i2cData.Length), i2cData);
                            i2cRespTextBox.AppendText(line + "\r\n");
                        }
                        catch (VisaException ex)
                        {
                            resp = ex.ErrorCode.ToString();
                            i2cRespTextBox.AppendText(resp + "\r\n");
                        }
                    }
                }
            }
        }
Beispiel #2
0
        private void i2cWriteButton_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 write: " + numOfBytes.ToString());



            inputStr = i2cComboBox.Text;
            if (inputStr == string.Empty)
            {
                return;
            }
            byte[] data = new byte[numOfBytes];

            if (inputStr.Contains(@"/"))
            {
                strAry = inputStr.Split(new char[] { '/' }); // first element is ignored due to / char

                data = new byte[strAry.Length - 1];
                for (int j = 1; j < strAry.Length; j++)
                {
                    data[j - 1] = Convert.ToByte(strAry[j], 16);// or byte.Parse(strAry[j], System.Globalization.NumberStyles.HexNumber);
                }
            }

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

            // check if there is any differences between number of bytes versus i2c data to write (i2cComboBox)
            if (numOfBytes != strAry.Length)
            {
                numOfBytes = (byte)(strAry.Length - 1);
            }
            if (numOfBytes > 255)
            {
                i2cRespTextBox.AppendText(numOfBytes.ToString() + " bytes is greater than max 255 allowed" + "\r\n");
                remote_inst.close();
                return;
            }
            try
            {
                remote_inst.writeI2c(i2cAddr, regAddr, Convert.ToByte(numOfBytes), data);
            }
            catch (Exception ex)
            {
                resp = ex.Message;
                i2cRespTextBox.AppendText(resp + "\r\n");
            }
            i2cRespTextBox.AppendText(inputStr + "\r\n");
            remote_inst.close();
        }