Example #1
0
        public bool Disconnect(PortConfiguration window)
        {
            bool isDisconnectOK = false;

            IsPortOpen = false;
            readThread.Join();

            if (port.TestDisconnect())
            {
                isDisconnectOK = true;

                dataReceived.Append("End of transmission\n");

                port.serialPort.Close();
                logger.Info("Port closed");

                window.Dispatcher.BeginInvoke(new Action(() =>
                {
                    window.txtBox_receivedMessages.Text = dataReceived.ToString();
                }));

                return(isDisconnectOK);
            }
            else
            {
                logger.Error("Problem with disconnection");
                IsPortOpen = true;
                return(isDisconnectOK);
            }
        }
Example #2
0
 public void SendData(string data, PortConfiguration window)
 {
     if (IsPortOpen)
     {
         port.serialPort.Write(data);
         dataSend.Append($"{data}\n");
         window.Dispatcher.BeginInvoke(new Action(() =>
         {
             window.txtBox_sendMessages.Text = dataSend.ToString();
         }));
     }
 }
Example #3
0
        public bool Connect(PortConfiguration window)
        {
            bool isConfigurationOK = port.CreatePort();

            if (isConfigurationOK == true)
            {
                try
                {
                    port.serialPort.Open();
                    logger.Info("Port Open");

                    if (port.TestConnection() == true)
                    {
                        IsPortOpen = true;
                        dataReceived.Append("Start of transmission\n");

                        window.Dispatcher.BeginInvoke(new Action(() =>
                        {
                            window.txtBox_receivedMessages.Text = dataReceived.ToString();
                        }));

                        logger.Info("Connected");
                        readThread = new Thread(() => Read());
                        readThread.Start();
                    }
                }
                catch (Exception exception)
                {
                    IsPortOpen = false;
                    port.serialPort.Close();
                    logger.Error("Problem with connection!");
                    logger.Error(exception.Message);
                    MessageBox.Show("You have chosen the wrong port", "Port problem", MessageBoxButton.OK, MessageBoxImage.Warning);
                }
            }
            return(IsPortOpen);
        }