Esempio n. 1
0
 private void Form2_Load(object sender, EventArgs e)
 {
     button1.PerformClick();
     try {
         server.sendCommand(new SerialCommand("list;", server.command_list));
     } catch (Exception ex)
     {
     }
 }
Esempio n. 2
0
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;

            runninSearch = true;
            int    progress;
            string comName = (string)e.Argument;

            server = new SensorServer(comName);
            Console.WriteLine(comName);


            _serialPort = new SerialPort(comName);

            _serialPort.RtsEnable = true;

            _serialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);

            _serialPort.WriteTimeout = 200;


            e.Result = "";
            try
            {
                _serialPort.Open();

                _serialPort.DiscardInBuffer();
                _serialPort.DiscardOutBuffer();
            } catch (Exception ee)
            {
                e.Result = "Port Nereaguje";
                return;
            }

            worker.ReportProgress(10);

            if (!_serialPort.IsOpen)
            {
                e.Result = "Port Nereaguje";
                return;
            }
            server.comPort = _serialPort;
            worker.ReportProgress(20);


            Thread.Sleep(100);
            server.flushInputBuffer();

            Stopwatch stopWatch = new Stopwatch();

            stopWatch.Start();

            worker.ReportProgress(30);

            server.sendCommand(new SerialCommand("hi", server.command_hi));
            //server.sendCommand("list;");

            worker.ReportProgress(40);
            progress = 40;


            Console.WriteLine("Probe");
            while (true)
            {
                if (!_serialPort.IsOpen)
                {
                    Console.WriteLine("CLOSED");
                    break;
                }
                if (server.deviceFound())
                {
                    Console.WriteLine("FOUND");
                    break;
                }
                if (stopWatch.ElapsedMilliseconds > 0 && (stopWatch.ElapsedMilliseconds % 100 == 0))
                {
                    worker.ReportProgress(40 + (int)(55 / ((serialTimeOut / stopWatch.ElapsedMilliseconds))));
                }
                if ((stopWatch.ElapsedMilliseconds >= serialTimeOut))
                {
                    Console.WriteLine("TIMEOUT");
                    break;
                }
                if (server.deviceWriteFailed())
                {
                    Console.WriteLine("WRITE TIMEOUT");
                    break;
                }
            }

            e.Result = comName;
            if (server.deviceFound())
            {
                Console.WriteLine("Najdene");
            }
            else
            {
                Console.WriteLine("TIMEOUT");
                _serialPort.Close();
            }

            stopWatch.Stop();

            worker.ReportProgress(100);
        }