コード例 #1
0
 private async void ScrollFunction(char ch, float addValue)
 {
     clicFlag = true;
     try
     {
         //serialWrite(ch + trackBar.Value.ToString() + 'z');
         CommonPoint[ch] = (float)(CommonPoint[ch] + addValue);
         serialPort.Write(CommonPoint);
         EditorUpdate();
         await Task.Run(async() => {
             if (clicFlag)
             {
                 Thread.Sleep(300);
             }
             while (clicFlag)
             {
                 await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                 {
                     try{
                         CommonPoint[ch] += addValue;
                         serialPort.Write(CommonPoint);
                         EditorUpdate();
                     }
                     catch (MaxValueException e) { CommonConsoleWrite(e.Message, Colors.Red); CommonPoint[ch] -= addValue; }
                     catch (MinValueException e) { CommonConsoleWrite(e.Message, Colors.Red); CommonPoint[ch] -= addValue; }
                     catch (Exception e) { CommonConsoleWrite(e.Message, Colors.Red); }
                 });
                 Thread.Sleep(50);
             }
         });
     }
     catch (MaxValueException e) { CommonConsoleWrite(e.Message, Colors.Red); CommonPoint[ch] -= addValue; }
     catch (MinValueException e) { CommonConsoleWrite(e.Message, Colors.Red); CommonPoint[ch] -= addValue; }
     catch (Exception e) { CommonConsoleWrite(e.Message, Colors.Red); }
 }
コード例 #2
0
        private async void ConectButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (!serialPort.IsOpen)
                {
                    ConectButton.IsEnabled = false;
                    serialPort.PortName    = ((string)comboSelectPort.SelectedItem);

                    await Task.Run(() =>
                    {
                        try
                        {
                            //  serialPort.BaudRate = programConfig.Speed;
                            serialPort.Open();
                            Point homePoint = new Point(0, 45, 87, 0, 240, 0, 500);
                            CommonPoint     = homePoint;
                            serialPort.Write(homePoint);
                        }
                        catch (UnauthorizedAccessException)
                        {
                            ConsoleWrite("COM порт занят.", Colors.Red);
                        }
                        catch (Exception soe)
                        {
                            ConsoleWrite(soe.ToString(), Colors.Red);
                        }
                        finally
                        {
                            if (serialPort.IsOpen)
                            {
                                ConsoleWrite("подключено к " + serialPort.PortName, Colors.Green);
                            }
                        }
                    });

                    send = new Task(() => { });
                    send.Start();
                    Execution.Start();
                }
                else
                {
                    Point homePoint = new Point(0, 0, 8, 0, 245, 0);
                    CommonPoint = homePoint;
                    serialPort.Write(homePoint);

                    serialPort.Close();
                    ConsoleWrite("Отключено от " + serialPort.PortName, Colors.Green);
                }
            }
            catch (IOException)
            {
                ConsoleWrite("Повторное подключение");
                ConectButton_Click(new object(), new RoutedEventArgs());
            }
            //catch (ArgumentNullException)
            //{
            //    DialogResult dialogResult = MessageBox.Show("Не выбран COM-порт.\nПовторить поиск?",
            //                                                "Ошибка!",
            //                                                MessageBoxButtons.OKCancel,
            //                                                MessageBoxIcon.Error);
            //    if (dialogResult == DialogResult.OK)
            //    {
            //        comboBox.Items.Clear();
            //        int portCount = 0;
            //        foreach (string portName in System.IO.Ports.SerialPort.GetPortNames())
            //        {
            //            comboBox.Items.Add(portName);
            //            portCount++;
            //        }
            //        if (portCount >= 2) comboBox.SelectedIndex = 2;
            //        comboHomeMode.SelectedIndex = 1;

            //        connectButton_Click_1(sender, e);
            //    }
            //}
            catch (Exception ce)
            {
                ConsoleWrite(ce.ToString(), Colors.Red);
            }
            finally
            {
                ConectButton.IsEnabled = true;
            }
        }