www.pudn.com > oscilloscope.rar > MainWindow.xaml.cs, change:2014-06-30,size:3973b


using System; 
using System.Collections.Generic; 
using System.Globalization; 
using System.IO; 
using System.IO.Ports; 
using System.Linq; 
using System.Text; 
using System.Reflection; 
using System.Threading; 
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.Windows.Threading; 
using Microsoft.Research.DynamicDataDisplay; 
using Microsoft.Research.DynamicDataDisplay.DataSources; 
using Microsoft.Research.DynamicDataDisplay.PointMarkers; 
 
 
namespace TestChart 
{ 
    /// <summary> 
    /// MainWindow.xaml 的交互逻辑 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
        ObservableDataSource<Point> sourceVoltage = new ObservableDataSource<Point>(); 
 
        static SerialPort k60Port = new SerialPort("COM7", 9600, Parity.None, 8); 
 
        private LineGraph lineVoltage = new LineGraph(); 
 
        bool isNewGraph = true; 
        bool isAutoMoveTimeAxis = false; 
 
        static int[] package = new int[11]; 
 
        private DispatcherTimer timer = new DispatcherTimer(); 
 
        double x = 0; 
 
        public MainWindow() 
        { 
            InitializeComponent(); 
        } 
 
        private void Window_Loaded(object sender, RoutedEventArgs e) 
        { 
            timer.Interval = TimeSpan.FromMilliseconds(1); 
            timer.Tick += GetValueAndDraw; 
            plotter.Viewport.FitToView(); 
        } 
 
        private void startButton_Click(object sender, RoutedEventArgs e) 
        { 
            ////Open Serial port 
            k60Port.Open(); 
            Thread.Sleep(50); 
 
            if (isNewGraph) 
            { 
 
                lineVoltage = plotter.AddLineGraph(sourceVoltage, Colors.Black, 2, "angle"); 
                isNewGraph = false; 
            } 
            timer.IsEnabled = true; 
            //clearAllLines.IsEnabled = false; 
        } 
 
        private void GetValueAndDraw(object sender, EventArgs e) 
        { 
                double voltage; 
                K60Datas(out voltage); 
 
                Point pVoltage = new Point(x, voltage); 
 
                sourceVoltage.AppendAsync(Dispatcher, pVoltage); 
 
                x += 0.00001; 
                if ((int)(x * 1000) % 1 == 0 && isAutoMoveTimeAxis) 
                { 
                    plotter.Viewport.Visible = new Rect(x - 20, -5, 20, 10); 
                } 
        } 
 
        private void K60Datas(out double voltage) 
        { 
            int[] packageToDecode = new int[2]; 
            while (k60Port.ReadByte() != 0x55) ; 
            for (int i = 0; i < 2; i++) 
            { 
             packageToDecode[i] = k60Port.ReadByte(); 
            } 
            voltage = (ushort)((packageToDecode[0] << 8 | packageToDecode[1])) / 65536.0; 
            //if (voltage > 0.85) 
            //    voltage = 1; 
            //else 
            //    voltage = 0; 
        } 
 
        private void stopButton_Click(object sender, RoutedEventArgs e) 
        { 
            timer.IsEnabled = false; 
            clearAllLines.IsEnabled = true; 
            k60Port.Close(); 
        } 
 
        private void fitToViewButton_Click(object sender, RoutedEventArgs e) 
        { 
            plotter.Viewport.FitToView(); 
            isAutoMoveTimeAxis = false; 
        } 
 
        private void clearAllLinesButton_Click(object sender, RoutedEventArgs e) 
        { 
            x = 0; 
            isNewGraph = true; 
            plotter.Children.Remove(lineVoltage); 
            sourceVoltage = new ObservableDataSource<Point>(); 
        } 
 
        private void autoMoveTimeAxisButton_Click(object sender, RoutedEventArgs e) 
        { 
            isAutoMoveTimeAxis = true; 
        } 
    } 
}