Ejemplo n.º 1
0
        public bool Write(List <int> outputData)
        {
            st = device.Connect(1);
            RshInitPort p = new RshInitPort();

            p.operationType = RshInitPort.OperationTypeBit.Write;
            for (uint i = 0; i < 3; i++)
            {
                var bits = Convert.ToString(outputData[Convert.ToInt32(i)], 2);
                while (bits.Length < 8)
                {
                    bits = bits.Insert(0, "0");
                }
                bits = InverseString(bits);
                var byteToSave = Convert.ToByte(bits, 2);
                p.portAddress = i;
                p.portValue   = byteToSave;
                st            = device.Init(p);
                if (st != RSH_API.SUCCESS)
                {
                    return(false);
                }
            }
            return(true);
        }
Ejemplo n.º 2
0
        public List <int> Read()
        {
            st = device.Connect(2);
            List <int>  InputData = new List <int>();
            RshInitPort p         = new RshInitPort();

            p.operationType = RshInitPort.OperationTypeBit.Read;
            for (uint i = 0; i < 3; i++)
            {
                p.portAddress = i;
                st            = device.Init(p);
                if (st != RSH_API.SUCCESS)
                {
                    return new List <int>()
                           {
                               0, 0, 0, 0
                           }
                }
                ;
                InputData.Add(Convert.ToInt32(p.portValue));
            }
            p.portAddress = 4;
            st            = device.Init(p);
            if (st != RSH_API.SUCCESS)
            {
                return new List <int>()
                       {
                           0, 0, 0, 0
                       }
            }
            ;
            InputData.Add(Convert.ToInt32(p.portValue));
            return(InputData);
        }
Ejemplo n.º 3
0
        public string SayGoodBye(RSH_API statusCode)
        {
            string errorMessage;

            Device.RshGetErrorDescription(statusCode, out errorMessage, RSH_LANGUAGE.RUSSIAN);
            string ret = statusCode.ToString() + ": " + errorMessage;

            pr(ret);
            return(ret);
        }
Ejemplo n.º 4
0
        public List <int> Read()
        {
            st = device.Connect(2);
            List <int>  InputData = new List <int>();
            RshInitPort p         = new RshInitPort();

            p.operationType = RshInitPort.OperationTypeBit.Read;
            for (uint i = 0; i < 3; i++)
            {
                p.portAddress = i;
                st            = device.Init(p);
                if (st != RSH_API.SUCCESS)
                {
                    return new List <int>()
                           {
                               0, 0, 0, 0
                           }
                }
                ;

                var bits = Convert.ToString(p.portValue, 2);
                while (bits.Length < 8)
                {
                    bits = bits.Insert(0, "0");
                }
                bits = InverseString(bits);
                var byteToSave = Convert.ToByte(bits, 2);
                InputData.Add(byteToSave);
            }

            p.portAddress = 4;
            st            = device.Init(p);
            if (st != RSH_API.SUCCESS)
            {
                return new List <int>()
                       {
                           0, 0, 0, 0
                       }
            }
            ;

            var bits2 = Convert.ToString(p.portValue, 2);

            while (bits2.Length < 8)
            {
                bits2 = bits2.Insert(0, "0");
            }
            bits2 = InverseString(bits2);
            var byteToSave2 = Convert.ToByte(bits2, 2);

            InputData.Add(byteToSave2);


            return(InputData);
        }
Ejemplo n.º 5
0
        public bool Connect()
        {
            st = device.OperationStatus;
            //Коннектимся к первой плате
            st = device.Connect(1);
            if (st != RSH_API.SUCCESS)
            {
                return(false);
            }

            st = device.Get(RSH_GET.DEVICE_PORT_INFO, ref bpi);
            for (int i = 0; i < bpi.confs.Length; i++)
            {
                RshInitPort port = new RshInitPort();
                port.operationType = RshInitPort.OperationTypeBit.Write;
                port.portAddress   = bpi.confs[i].address;
                port.portValue     = 0x80;
                st = device.Init(port); //У первой платы все на вывод
            }

            //Сбрасываем все порты в 0, но т.к. у нас инверсия то в ff
            RshInitPort p = new RshInitPort();

            p.operationType = RshInitPort.OperationTypeBit.Write;
            p.portAddress   = 0;
            p.portValue     = 0xff;
            st            = device.Init(p);
            p.portAddress = 1;
            p.portValue   = 0xff;
            st            = device.Init(p);
            p.portAddress = 2;
            p.portValue   = 0xff;
            st            = device.Init(p);
            //-------------------

            //Коннектимся ко второй плате
            st = device.Connect(2);
            if (st != RSH_API.SUCCESS)
            {
                return(false);
            }

            st = device.Get(RSH_GET.DEVICE_PORT_INFO, ref bpi);
            for (int i = 0; i < bpi.confs.Length; i++)
            {
                RshInitPort port = new RshInitPort();
                port.operationType = RshInitPort.OperationTypeBit.Write;
                port.portAddress   = bpi.confs[i].address;
                port.portValue     = 0x9B;
                st = device.Init(port); //У второй платы все на ввод
            }
            return(true);
        }
Ejemplo n.º 6
0
        public bool Write(List <byte> outputData)
        {
            st = device.Connect(1);
            RshInitPort p = new RshInitPort();

            p.operationType = RshInitPort.OperationTypeBit.Write;
            for (int i = 0; i < 3; i++)
            {
                var byteToSave = Inverse(outputData[i]);
                p.portAddress = Convert.ToUInt16(i);
                p.portValue   = byteToSave;
                st            = device.Init(p);
                if (st != RSH_API.SUCCESS)
                {
                    return(false);
                }
            }
            return(true);
        }
Ejemplo n.º 7
0
 private int SayGoodBye(RSH_API statusCode)
 {
     device.Stop();
     device = null;
     string errorMessage;
     Device.RshGetErrorDescription(statusCode, out errorMessage, RSH_LANGUAGE.RUSSIAN);
     Logging.WriteToLog(" !! Error " + "Connection.SayGoodBye :: " + errorMessage + "\n" + statusCode.ToString());
     //MessageBox.Show(errorMessage + "\n" + statusCode.ToString(), "Error", MessageBoxButtons.OK);
     throw new IOException(errorMessage + "\n" + statusCode.ToString());
     return (int)statusCode;
 }
Ejemplo n.º 8
0
        private void ParseString(string InStr)
        {
            String[] words      = InStr.Split(' ');
            double[] dataBuffer = new double[IBSIZE * 4];
            st = device.GetData(dataBuffer); // Заполним буфер данными.
            if (st != RSH_API.SUCCESS)
            {
                toolStripStatusLabel1.Text = "Error when collecting data from device, status= " + st;
                //return;
            }

            val1         = CalculateMean(dataBuffer, 0);
            val2         = CalculateMean(dataBuffer, 1);
            val3         = CalculateMean(dataBuffer, 2);
            val4         = CalculateMean(dataBuffer, 3);
            label11.Text = val1.ToString();
            label10.Text = val2.ToString();
            label9.Text  = val3.ToString();
            label8.Text  = val4.ToString();
            try
            {
                if (words[0] == "m")
                {
                    ChangeOperationMode(MODE_MAIN);
                    //try
                    //{
                    //    value1 = Int32.Parse(words[1]);
                    //    value2 = Int32.Parse(words[2]);
                    //    value3 = Int32.Parse(words[3]);
                    //    value4 = Int32.Parse(words[4]);
                    //    value5 = Int32.Parse(words[5]);
                    //    value6 = Int32.Parse(words[6]);
                    //}
                    //catch
                    //{

                    //}

                    time = (DateTime.Now - StartExperiment).Ticks / 10000000.0;


                    //val1 = (value1 - 8388608) * 5000.0 / 16777216.0;
                    //val2 = (value2 - 8388608) * 5.0 / 16777216.0;
                    //val3 = (value3 - 8388608) * 5.0 / 16777216.0;
                    //val4 = (value4 - 8388608) * 5000.0 / 16777216.0;

                    if ((Math.Abs(val2) <= 1.0e-5) || (Math.Abs(val4) <= 1.0e-5))
                    {
                        val5 = 0;
                    }
                    else
                    {
                        val5 = 8880.0 * val1 / (val2 * val4);
                    }

                    //label2.Text = value1.ToString();
                    //label3.Text = value2.ToString();
                    //label4.Text = value3.ToString();
                    //label5.Text = value4.ToString();
                    //label6.Text = value5.ToString();
                    //label7.Text = value6.ToString();

                    //label11.Text = val1.ToString();
                    //label10.Text = val2.ToString();
                    //label9.Text = val3.ToString();
                    //label8.Text = val4.ToString();

                    label16.Text = val5.ToString();

                    OutputFile = new StreamWriter(FileName, true);
                    OutputFile.WriteLine(String.Format("{0} {1} {2} {3} {4} {5} {6} {7} ", time, value1, value2, value3, value4, value5, value6, val5));
                    OutputFile.Close();


                    if ((NPoints < MaxPoints) && (((TakeEachPointNow++) == TakeEachPoint)))
                    {
                        if (val5 < YMIN)
                        {
                            YMIN = val5;
                        }
                        if (val5 > YMAX)
                        {
                            YMAX = val5;
                        }
                        NPoints++;
                        Array.Resize(ref XX, NPoints);
                        Array.Resize(ref YY, NPoints);
                        XX[NPoints - 1]     = time;
                        YY[NPoints - 1]     = val5;
                        NPlot1.DataSource   = YY;
                        NPlot1.AbscissaData = XX;
                        plotSurface2D1.Refresh();       // added new points
                        TakeEachPointNow = 0;
                    }
                    if (NPoints == MaxPoints)
                    {
                        for (int i = 0; i < NPoints / 2; i++)
                        {
                            XX[i] = XX[2 * i];
                            YY[i] = YY[2 * i];
                        }
                        NPoints         /= 2;
                        TakeEachPoint   *= 2;
                        TakeEachPointNow = 0;
                        Array.Resize(ref XX, NPoints);
                        Array.Resize(ref YY, NPoints);
                    }
                    if ((time > plotSurface2D1.XAxis1.WorldMax) && AutoRescale)
                    {
                        PlotRescale();
                    }
                    // DEBUG


                    //                   toolStripStatusLabel1.Text = SerialWriteBuffer[0].ToString();
                }

                if (words[0] == "s")
                {
                    ChangeOperationMode(SCAN_CELL);
                    voltage = Int32.Parse(words[1]);

                    //value6 = Int32.Parse(words[1]);
                    //value3 = Int32.Parse(words[2]);
                    //if (value3 == 16777215) return;
                    //if (value3 == 0) return;

                    time = (DateTime.Now - StartExperiment).Ticks / 10000000.0;


                    //val1 = value6 * 10.0 / 1024;
                    //val3 = (value3 - 8388608) * 5.0 / 16777216.0;

                    //                val5 = 100.0 * val1 / (val2 * val4);

                    label2.Text = "-";
                    label3.Text = "-";
                    //label4.Text = value3.ToString();
                    label5.Text = "-";
                    //label6.Text = value6.ToString();
                    label7.Text = "-";

                    //label11.Text = val1.ToString();
                    label11.Text = voltage.ToString();
                    label10.Text = "-";
                    //label9.Text = val3.ToString();
                    label9.Text = val2.ToString();
                    label8.Text = "-";

                    label16.Text = "-";

                    OutputFile = new StreamWriter(FileName, true);
                    OutputFile.WriteLine(String.Format("{0} {1} {2} ", /*value6, value3*/ time, voltage, val2));
                    OutputFile.Close();


                    if (NPoints <= MaxPoints)
                    {
                        if (val3 < YMIN)
                        {
                            YMIN = val2;
                        }
                        if (val3 > YMAX)
                        {
                            YMAX = val2;
                        }
                        NPoints++;
                        Array.Resize(ref XX, NPoints);
                        Array.Resize(ref YY, NPoints);
                        XX[NPoints - 1]     = time;
                        YY[NPoints - 1]     = val2;
                        NPlot1.DataSource   = YY;
                        NPlot1.AbscissaData = XX;
                        plotSurface2D1.Refresh();  // added new points
                    }
                    if (time > plotSurface2D1.XAxis1.WorldMax)
                    {
                        PlotRescale();
                    }
                }
            }
            catch (System.FormatException)
            {
            }
            SerialControl();
        }
Ejemplo n.º 9
0
        private void GetData(string mode = "")
        {
            double[] dataBuffer = new double[4];
            if (mode == "mean")
            {
                dataBuffer = new double[IBSIZE * 4];
            }
            st = device.GetData(dataBuffer); // Заполним буфер данными.
            if (st != RSH_API.SUCCESS)
            {
                toolStripStatusLabel1.Text = "Error when collecting data from device, status= " + st;
                return;
            }

            val1         = CalculateMean(dataBuffer, 0);
            val2         = CalculateMean(dataBuffer, 1);
            val3         = CalculateMean(dataBuffer, 2);
            val4         = CalculateMean(dataBuffer, 3);
            label11.Text = val1.ToString();
            label10.Text = val2.ToString();
            label9.Text  = val3.ToString();
            label8.Text  = val4.ToString();
            time         = (DateTime.Now - StartExperiment).Ticks / 10000000.0;
            if ((Math.Abs(val2) <= 1.0e-5) || (Math.Abs(val4) <= 1.0e-5))
            {
                val5 = 0;
            }
            else
            {
                val5 = 8880.0 * val1 / (val2 * val4);
            }
            label16.Text = val5.ToString();

            OutputFile = new StreamWriter(FileName, true);
            OutputFile.WriteLine(String.Format("{0} {1} {2} {3} {4} {5}", time, val1, val2, val3, val4, val5));
            OutputFile.Close();

            if ((NPoints < MaxPoints) && (((TakeEachPointNow++) == TakeEachPoint)))
            {
                if (val5 < YMIN)
                {
                    YMIN = val5;
                }
                if (val5 > YMAX)
                {
                    YMAX = val5;
                }
                NPoints++;
                Array.Resize(ref XX, NPoints);
                Array.Resize(ref YY, NPoints);
                XX[NPoints - 1]     = time;
                YY[NPoints - 1]     = val5;
                NPlot1.DataSource   = YY;
                NPlot1.AbscissaData = XX;
                plotSurface2D1.Refresh();   // added new points
                TakeEachPointNow = 0;
            }
            if (NPoints == MaxPoints)
            {
                for (int i = 0; i < NPoints / 2; i++)
                {
                    XX[i] = XX[2 * i];
                    YY[i] = YY[2 * i];
                }
                NPoints         /= 2;
                TakeEachPoint   *= 2;
                TakeEachPointNow = 0;
                Array.Resize(ref XX, NPoints);
                Array.Resize(ref YY, NPoints);
            }
            if ((time > plotSurface2D1.XAxis1.WorldMax) && AutoRescale)
            {
                PlotRescale();
            }
            // DEBUG
            //_serialPort.Write(SerialWriteBuffer, 0, 1);
        }
Ejemplo n.º 10
0
        public void ADC_init()
        {
            //=================== ИНФОРМАЦИЯ О ЗАГРУЖЕННОЙ БИБЛИОТЕКЕ ======================;
            string libVersion, libName, libCoreVersion, libCoreName;

            st = device.Get(RSH_GET.LIBRARY_VERSION_STR, out libVersion);
            if (st != RSH_API.SUCCESS)
            {
                toolStripStatusLabel1.Text  = DateTime.Now.ToString();
                toolStripStatusLabel1.Text += ": Error while loading RSh lib; status= ";
                toolStripStatusLabel1.Text += st;
                return;
            }
            st = device.Get(RSH_GET.CORELIB_VERSION_STR, out libCoreVersion);
            if (st != RSH_API.SUCCESS)
            {
                toolStripStatusLabel1.Text  = DateTime.Now.ToString();
                toolStripStatusLabel1.Text += ": Error while loading RSh lib core lib; status= ";
                toolStripStatusLabel1.Text += st;
                return;
            }
            st = device.Get(RSH_GET.CORELIB_FILENAME, out libCoreName);
            if (st != RSH_API.SUCCESS)
            {
                toolStripStatusLabel1.Text  = DateTime.Now.ToString();
                toolStripStatusLabel1.Text += ": Error while loading RSh core lib; status= ";
                toolStripStatusLabel1.Text += st;
                return;
            }
            st = device.Get(RSH_GET.LIBRARY_FILENAME, out libName);
            if (st != RSH_API.SUCCESS)
            {
                toolStripStatusLabel1.Text  = DateTime.Now.ToString();
                toolStripStatusLabel1.Text += ": Error while loading RSh lib; status= ";
                toolStripStatusLabel1.Text += st;
                return;
            }
            toolStripStatusLabel1.Text = "Successfully loaded libraries and drivers; ";
            //===================== ПРОВЕРКА СОВМЕСТИМОСТИ =================================;
            uint caps = (uint)RSH_CAPS.SOFT_PGATHERING_IS_AVAILABLE;

            //Проверим, поддерживает ли устройство функцию сбора данных в непрерывном режиме.
            st = device.Get(RSH_GET.DEVICE_IS_CAPABLE, ref caps);
            if (st != RSH_API.SUCCESS)
            {
                toolStripStatusLabel1.Text = DateTime.Now.ToString() + "Device is not capable; status= " + st;
                return;
            }
            //========================== ИНИЦИАЛИЗАЦИЯ =====================================;
            //Подключаемся к устройству. Нумерация начинается с 1.
            st = device.Connect(1);
            if (st != RSH_API.SUCCESS)
            {
                toolStripStatusLabel1.Text = DateTime.Now.ToString() + "Error when connecting to device; status= " + st;
                return;
            }
            //Структура для инициализации параметров работы устройства.
            RshInitDMA p = new RshInitDMA();

            //Запуск устройства программный.
            p.startType = (uint)RshInitDMA.StartTypeBit.Program;
            //Режим непрерывного сбора данных.
            p.dmaMode = (uint)RshInitDMA.DmaModeBit.Persistent;
            //Размер внутреннего блока данных, по готовности которого произойдёт прерывание.
            p.bufferSize = IBSIZE;
            //Частота дискретизации.
            p.frequency = SAMPLE_FREQ;
            //Сделаем все 4 канала активными;
            for (int i = 0; i <= 3; i++)
            {
                p.channels[i].control = (uint)RshChannel.ControlBit.Used;
                //Зададим коэффициент усиления для i-го канала.
                p.channels[i].gain = 1;
            }
            //Инициализация устройства (передача выбранных параметров сбора данных)
            //После инициализации неправильные значения в структуре будут откорректированы.
            st = device.Init(p);
            if (st != RSH_API.SUCCESS)
            {
                toolStripStatusLabel1.Text = DateTime.Now.ToString() + "Error when initializing device; status= " + st;
                return;
            }
            toolStripStatusLabel1.Text += "Successfully initialized device; ";

            //Время ожидания(в миллисекундах) до наступления прерывания. Прерывание произойдет при полном заполнении буфера.
            uint waitTime = 100000;

            st = device.Start();
            if (st != RSH_API.SUCCESS)
            {
                toolStripStatusLabel1.Text = DateTime.Now.ToString() + "Error when starting data collection; status= " + st;
                return;
            }

            st = device.Get(RSH_GET.WAIT_BUFFER_READY_EVENT, ref waitTime);
            if (st != RSH_API.SUCCESS)
            {
                toolStripStatusLabel1.Text = DateTime.Now.ToString() + "Error with interruption time; status= " + st;
                return;
            }
            toolStripStatusLabel1.Text += "Successfully started data collection";
        }