Newer
Older
Correlator / Correlator / View / MainWindow.xaml.cs
using System;
using System.Diagnostics;
using System.IO.Ports;
using System.Threading.Tasks;
using System.Windows.Threading;
using Correlator.Util;

namespace Correlator.View
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow
    {
        private readonly DispatcherTimer _timer = new DispatcherTimer
        {
            Interval = new TimeSpan(0, 0, 1)
        };

        private int _counterOneTime = 5;

        private readonly DispatcherTimer _checkSerialPortTimer = new DispatcherTimer
        {
            Interval = new TimeSpan(0, 0, 7)
        };

        private Task<HomePageProWindow> GetHomePageProWindow()
        {
            return Task.FromResult(new HomePageProWindow());
        }

        public MainWindow()
        {
            InitializeComponent();

            _checkSerialPortTimer.Start();
            _checkSerialPortTimer.Tick += CheckSerialPortState;

            //倒计时显示大屏Logo
            _timer.Start();
            _timer.Tick += async delegate
            {
                if (_counterOneTime > 0)
                {
                    _counterOneTime--;
                }
                else
                {
                    _timer.Stop();
                    var homePageProWindow = await GetHomePageProWindow();
                    homePageProWindow.Show();
                    Close();
                }
            };
        }

        /// <summary>
        /// 检查串口状态
        /// </summary>
        private void CheckSerialPortState(object sender, EventArgs eventArgs)
        {
            var ports = SerialPort.GetPortNames();
            foreach (var portName in ports)
            {
                if (portName != "COM3" || SerialPortManager.Get.Sp.IsOpen) continue;
                SerialPortManager.Get.Sp.PortName = portName; //串口名称
                SerialPortManager.Get.Sp.BaudRate = int.Parse("230400"); //波特率57600和230400
                SerialPortManager.Get.Sp.DataBits = int.Parse("8"); //数据位
                SerialPortManager.Get.Sp.StopBits = (StopBits)int.Parse("1"); //停止位
                SerialPortManager.Get.Sp.Open(); //打开串口
                Debug.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " MainWindow.xaml => 打开串口");

                break;
            }

            if (SerialPortManager.Get.Sp.IsOpen)
            {
                //实时显示信号电量
                if (!FlowStatus.RedSensorIsEnable && !FlowStatus.BlueSensorIsEnable)
                {
                    _devId = (byte)(_devId == 0x02 ? 0x01 : 0x02);
                    SerialPortManager.Get.ShowSignal(_devId);
                }
                else
                {
                    if (!FlowStatus.RedSensorIsEnable)
                    {
                        SerialPortManager.Get.ShowSignal(0x01);
                    }

                    if (!FlowStatus.BlueSensorIsEnable)
                    {
                        SerialPortManager.Get.ShowSignal(0x02);
                    }
                }
            }
        }

        private byte _devId = 0x02;
    }
}