Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- using System.IO.Ports;
- using System.Windows.Threading;
- namespace Termometr
- {
- /// <summary>
- /// Interaction logic for MainWindow.xaml
- /// </summary>
- public partial class MainWindow : Window
- {
- static string temperatura = ""; // deklaracja zmiennej temperatury
- double temp1;
- //static string wilgotnosc = ""; // deklaracja zmiennej wilgotności
- static public bool parz = true; // deklaracja zmiennej boolean
- static public bool _continue = false;
- static SerialPort serialPort1 = new SerialPort("COM10",9600);
- public Window1 winWykres;
- public MainWindow()
- {
- InitializeComponent();
- But_Bt_Stop.IsEnabled = false;
- ProgBar1.Value = 25;
- serialPort1.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
- serialPort1.ReadTimeout = 50;
- //serialPort1.WriteBufferSize
- Temp1.Content = temperatura + "C";
- //Hum1.Content = wilgotnosc + "%";
- DispatcherTimer timer = new DispatcherTimer();
- timer.Interval = TimeSpan.FromSeconds(2);
- timer.Tick += timer_Tick;
- timer.Start();
- ///
- this.BtWYkres.Visibility = Visibility.Hidden;
- this.BtWYkres.Visibility = Visibility.Collapsed;
- }
- void timer_Tick(object sender, EventArgs e)
- {
- //Hum1.Content = wilgotnosc + "%";
- Temp1.Content = temperatura + "C";
- ProgBar1.Value = temp1;
- if (serialPort1.IsOpen)
- serialPort1.DiscardInBuffer();
- }
- protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
- {
- //do my stuff before closing
- if (!(serialPort1 == null))
- if (serialPort1.IsOpen)
- serialPort1.Close();
- base.OnClosing(e);
- }
- private void But_Bt_Start_Click(object sender, RoutedEventArgs e)
- {
- try
- {
- if (!serialPort1.IsOpen) // Jeżeli port nie jest otwarty, otwieramy go
- {
- But_Bt_Start.IsEnabled = false;
- But_Bt_Stop.IsEnabled = true;
- serialPort1.PortName = this.PORT_COM.Text; //(string)PORT_COM.Text;
- serialPort1.Open(); // otwarcie portu
- serialPort1.DiscardInBuffer();
- MessageBox.Show("Otwarto Port");
- }
- }
- catch(Exception)
- {
- MessageBox.Show("Błąd otwarcia portu");
- serialPort1.Close();
- But_Bt_Start.IsEnabled = true;
- But_Bt_Stop.IsEnabled = false;
- }
- }
- private void But_Bt_Stop_Click(object sender, RoutedEventArgs e)
- {
- serialPort1.Close(); // zamkniecie portu
- But_Bt_Start.IsEnabled = true;
- But_Bt_Stop.IsEnabled = false;
- }
- private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
- {
- SerialPort sp = (SerialPort)sender;
- try
- {
- temperatura = sp.ReadLine();
- temp1 = Double.Parse(temperatura.Replace(".",","));
- serialPort1.DiscardInBuffer();
- }
- catch(Exception)
- {
- }
- }
- private void Button_Click(object sender, RoutedEventArgs e)
- {
- winWykres = new Window1();
- winWykres.Show();
- //BtWYkres.IsEnabled = false;
- }
- private void Button_Click_1(object sender, RoutedEventArgs e)
- {
- //SerialPort sp = new SerialPort();
- try
- {
- if (serialPort1.IsOpen)
- serialPort1.WriteLine(this.tempZadan.Text.Replace(",", "."));
- else
- MessageBox.Show("Otwórz Port");
- }
- catch
- {
- MessageBox.Show("Blad");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement