Beispiel #1
0
        private void ToggleButton_SerialPortIn_Unchecked(object sender, RoutedEventArgs e)
        {
            ToggleButton button = (sender as ToggleButton);

            try
            {
                if (SerialPortModbus.IsOpen)
                {
                    SerialPortModbus.Close();
                    if (!SerialPortModbus.IsOpen)
                    {
                        label_PortState_Modbus.Content = "Порт закрыт";
                        button.Content = "Открыть";
                    }
                }
                else
                {
                    button.IsChecked = false;
                    throw new Exception($"Доступ к порту '{SerialPortModbus.PortName.ToString()}' закрыт");
                }
            }
            catch (Exception exception)
            {
                label_PortState_Modbus.Content = exception.Message;
                button.IsChecked = false;
            }
        }
Beispiel #2
0
        private void SerialDataIntBUSResponse()
        {
            int byteRecieved = SerialPortIntBUS.BytesToRead;

            byte[] responseBytes = new byte[byteRecieved];
            if (responseBytes.Length == 0)
            {
                return;
            }
            SerialPortIntBUS.Read(responseBytes, 0, byteRecieved);
            SerialPortIntBUS.DiscardInBuffer();

            string message = BitConverter.ToString(responseBytes).Replace('-', ' ');

            message = AddTime(message);
            AddConsoleText(message + "\r", textBox_ConsoleIntBUS, Brushes.LimeGreen);

            List <byte> listBytes = new List <byte>();

            listBytes.AddRange(responseBytes);
            listBytes = listBytes.Skip(2).ToList();//TODO переделать
            listBytes = listBytes.Take(listBytes.Count() - 2).ToList();
            listBytes.AddRange(ModbusUtility.CalculateCrc(listBytes.ToArray()));
            responseBytes = listBytes.ToArray();

            message = BitConverter.ToString(responseBytes).Replace('-', ' ');
            message = AddTime(message);
            if (SerialPortModbus != null)
            {
                if (SerialPortModbus.IsOpen)
                {
                    SerialPortModbus.Write(responseBytes, 0, responseBytes.Length);
                }
            }
        }
Beispiel #3
0
        private void ToggleButton_SerialPortIn_Checked(object sender, RoutedEventArgs e)
        {
            ToggleButton button = (sender as ToggleButton);

            try
            {
                if (!SerialPortModbus.IsOpen)
                {
                    SerialPortModbus.Open();
                    if (SerialPortModbus.IsOpen)
                    {
                        label_PortState_Modbus.Content = "Порт открыт";
                        button.Content = "Закрыть";
                    }
                }
                else
                {
                    button.IsChecked = true;
                    throw new Exception($"Доступ к порту '{SerialPortModbus.PortName.ToString()}' закрыт");
                }
            }
            catch (Exception exception)
            {
                button.IsChecked = true;
                label_PortState_Modbus.Content = exception.Message;
            }


            //ToggleButton button = (sender as ToggleButton);
            //try
            //{
            //    if (SerialPortIn.IsOpen)
            //    {
            //        SerialPortIn.Close();
            //        if (!SerialPortIn.IsOpen)
            //        {
            //            label_PortState_In.Content = "Порт закрыт";
            //            button.Content = "Открыть";
            //        }
            //    }
            //    else
            //    {
            //        button.IsChecked = true;
            //        throw new Exception($"Доступ к порту '{SerialPortIn.PortName.ToString()}' закрыт");
            //    }
            //}
            //catch (Exception exception)
            //{
            //    label_PortState_In.Content = exception.Message;
            //    button.IsChecked = true;
            //}
        }