예제 #1
0
 private void button_stop_listen_Click(object sender, RoutedEventArgs e)
 {
     comboBox_uart_ports.IsEnabled = true;
     button_stop_listen.IsEnabled  = false;
     button_listen.IsEnabled       = true;
     button_send_command.IsEnabled = false;
     Uart.ClosePort();
     Uart.RemoveReciveEventHandler(GetDataFromComPort);
 }
예제 #2
0
        private void button_send_command_Click(object sender, RoutedEventArgs e)
        {
            if (checkBox_hex_command.IsChecked == true)
            {
                var bytes = textBox_command.Text.Split(' ').Select(_ => int.Parse(_, NumberStyles.HexNumber));

                int[]  int_array  = bytes.ToArray();
                byte[] byte_array = new byte[int_array.Length];
                for (int i = 0; i < int_array.Length; i++)
                {
                    byte_array[i] = (byte)int_array[i];
                }
                Uart.Write(byte_array, 0, byte_array.Length);
            }
            else
            {
                byte[] bytes = Encoding.Unicode.GetBytes(textBox_command.Text);
                Uart.Write(bytes, 0, bytes.Length);
            }
        }
예제 #3
0
 private void GetDataFromComPort(object sender, SerialDataReceivedEventArgs e)
 {
     try {
         SerialPort sp = (SerialPort)sender;
         //Thread.Sleep(100);
         //string data = sp.ReadExisting();
         //listBox_COM.Invoke(new Action(() => listBox_COM.Items.Add(data)));
         byte[] listenerData = Uart.Read();
         string strMessage   = "";
         strMessage = listenerData.Aggregate("", (current, c) => current + (char)c);
         string hexMsg = "";
         for (int i = 0; i < listenerData.Length; i++)
         {
             _uartData.Add(listenerData[i]);
             if (listenerData[i] < 0X10)
             {
                 hexMsg += "0";
             }
             hexMsg   += listenerData[i].ToString("X") + " ";
             _lastByte = listenerData[i];
         }
         this.Dispatcher.Invoke(new Action(() => {
             text_box_main_1.AppendText(strMessage);
             text_box_main_2.AppendText(strMessage);
             text_box_main_3.AppendText(hexMsg);
             if (CurrentAppState.CurrentAppConfig.LogsPathsHexIsListen)
             {
                 CurrentAppState.LogsHexStream.Write(hexMsg);
                 CurrentAppState.LogsHexStream.Flush();
             }
             if (CurrentAppState.CurrentAppConfig.LogsPathsAsciiIsListen)
             {
                 CurrentAppState.LogsAsciiStream.Write(strMessage);
                 CurrentAppState.LogsAsciiStream.Flush();
             }
         }));
     } catch (Exception ex) {
         //var a = ex as EndOfStreamException;
         MessageBox.Show("Исключение в потоке получения данных UART: " + ex.Message + "(" + ex.Source + ")");
     }
 }