Advertisement
Guest User

as

a guest
Jan 30th, 2015
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.72 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15. using System.IO.Ports;
  16. using System.Windows.Threading;
  17.  
  18. namespace Termometr
  19. {
  20.     /// <summary>
  21.     /// Interaction logic for MainWindow.xaml
  22.     /// </summary>
  23.     public partial class MainWindow : Window
  24.     {
  25.         static string temperatura = ""; // deklaracja zmiennej temperatury
  26.         double temp1;
  27.         //static string wilgotnosc = ""; // deklaracja zmiennej wilgotności
  28.         static public bool parz = true; // deklaracja zmiennej boolean
  29.         static public bool _continue = false;
  30.         static SerialPort serialPort1 = new SerialPort("COM10",9600);
  31.         public Window1 winWykres;
  32.  
  33.        
  34.  
  35.  
  36.         public MainWindow()
  37.         {
  38.             InitializeComponent();
  39.             But_Bt_Stop.IsEnabled = false;
  40.             ProgBar1.Value = 25;
  41.             serialPort1.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
  42.             serialPort1.ReadTimeout = 50;
  43.             //serialPort1.WriteBufferSize
  44.            
  45.             Temp1.Content = temperatura + "C";
  46.             //Hum1.Content = wilgotnosc + "%";
  47.             DispatcherTimer timer = new DispatcherTimer();
  48.             timer.Interval = TimeSpan.FromSeconds(2);
  49.             timer.Tick += timer_Tick;
  50.             timer.Start();
  51.            
  52.             ///
  53.             this.BtWYkres.Visibility = Visibility.Hidden;
  54.             this.BtWYkres.Visibility = Visibility.Collapsed;
  55.            
  56.            
  57.         }
  58.  
  59.         void timer_Tick(object sender, EventArgs e)
  60.         {
  61.            
  62.             //Hum1.Content = wilgotnosc + "%";
  63.             Temp1.Content = temperatura + "C";
  64.             ProgBar1.Value = temp1;
  65.             if (serialPort1.IsOpen)
  66.                 serialPort1.DiscardInBuffer();
  67.         }
  68.  
  69.         protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
  70.         {
  71.             //do my stuff before closing
  72.             if (!(serialPort1 == null))
  73.                 if (serialPort1.IsOpen)
  74.                     serialPort1.Close();
  75.             base.OnClosing(e);
  76.         }
  77.  
  78.  
  79.         private void But_Bt_Start_Click(object sender, RoutedEventArgs e)
  80.         {
  81.             try
  82.             {
  83.                 if (!serialPort1.IsOpen) // Jeżeli port nie jest otwarty, otwieramy go
  84.                 {
  85.                     But_Bt_Start.IsEnabled = false;
  86.                     But_Bt_Stop.IsEnabled = true;
  87.                     serialPort1.PortName = this.PORT_COM.Text; //(string)PORT_COM.Text;
  88.                     serialPort1.Open(); // otwarcie portu
  89.                     serialPort1.DiscardInBuffer();
  90.                     MessageBox.Show("Otwarto Port");
  91.                    
  92.                 }
  93.             }
  94.             catch(Exception)
  95.             {
  96.                 MessageBox.Show("Błąd otwarcia portu");
  97.                 serialPort1.Close();
  98.                 But_Bt_Start.IsEnabled = true;
  99.                 But_Bt_Stop.IsEnabled = false;
  100.             }
  101.            
  102.         }
  103.  
  104.         private void But_Bt_Stop_Click(object sender, RoutedEventArgs e)
  105.         {
  106.             serialPort1.Close(); // zamkniecie portu
  107.  
  108.             But_Bt_Start.IsEnabled = true;
  109.             But_Bt_Stop.IsEnabled = false;
  110.         }
  111.        
  112.         private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
  113.         {
  114.             SerialPort sp = (SerialPort)sender;
  115.                 try
  116.                 {
  117.                     temperatura = sp.ReadLine();
  118.                     temp1 = Double.Parse(temperatura.Replace(".",","));
  119.                     serialPort1.DiscardInBuffer();
  120.                 }
  121.                 catch(Exception)
  122.                 {
  123.  
  124.                 }
  125.         }
  126.  
  127.         private void Button_Click(object sender, RoutedEventArgs e)
  128.         {
  129.             winWykres = new Window1();
  130.             winWykres.Show();
  131.             //BtWYkres.IsEnabled = false;
  132.         }
  133.  
  134.         private void Button_Click_1(object sender, RoutedEventArgs e)
  135.         {
  136.             //SerialPort sp = new SerialPort();
  137.             try
  138.             {
  139.                 if (serialPort1.IsOpen)
  140.                     serialPort1.WriteLine(this.tempZadan.Text.Replace(",", "."));
  141.                 else
  142.                     MessageBox.Show("Otwórz Port");
  143.             }
  144.             catch
  145.             {
  146.                 MessageBox.Show("Blad");
  147.             }
  148.            
  149.  
  150.         }
  151.    
  152.     }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement