GPS接收机(五)基于C#的GPS软件上位机

前面已经搭建了一个可以接受GPS数据的简易接收机,接下来将要编写一个上位机处理接收到的数据,并进行处理显示。

效果示意图

接收文本数据,并进行表格显示
在这里插入图片描述
接受文本数据,并进行图表显示
在这里插入图片描述

前台代码如下

    <Grid>
        <TextBox x:Name="Tip" HorizontalAlignment="Left" Height="24" Margin="8,176,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="40" BorderBrush="{x:Null}" Background="{x:Null}" FontSize="16" IsReadOnly="True" Foreground="Black" HorizontalContentAlignment="Center" Text="提示"/>
        <ComboBox x:Name="Port_Select" RenderTransformOrigin="1.436,2.058" BorderBrush="Black" Foreground="Black" FontFamily="Microsoft YaHei" Background="{x:Null}" Height="32" VerticalAlignment="Top" HorizontalAlignment="Left" Width="76" MouseEnter="PortSelect_MouseEnter" Margin="75,30,0,0" FontSize="13.333" VerticalContentAlignment="Center"/>
        <Button x:Name="Port_Open" Content="打开串口" Margin="75,170,0,0" RenderTransformOrigin="-0.833,1.211" Click="PortOpen_Click" Background="{x:Null}" BorderBrush="Black" FontFamily="Microsoft YaHei" FontSize="16" HorizontalAlignment="Left" Width="76" Height="32" VerticalAlignment="Top"/>
        <TextBox x:Name="textBox" HorizontalAlignment="Left" Height="300" Margin="186,36,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="396"/>
        <GroupBox Header="接收数据" Height="326" HorizontalAlignment="Left" Margin="186,10,0,0" x:Name="GroupBox" VerticalAlignment="Top" Width="396"/>
        <ComboBox x:Name="comboBox" HorizontalAlignment="Left" Margin="75,103,0,0" VerticalAlignment="Top" Width="76" Background="{x:Null}" Height="32" FontSize="13.333" VerticalContentAlignment="Center" >
            <ComboBoxItem Content="9600"/>
            <ComboBoxItem Content="14400"/>
            <ComboBoxItem Content="19200"/>
            <ComboBoxItem Content="38400"/>
            <ComboBoxItem Content="57600"/>
            <ComboBoxItem Content="115200"/>
        </ComboBox>
        <TextBlock x:Name="textBlock" HorizontalAlignment="Left" Margin="8,36,0,0" VerticalAlignment="Top" RenderTransformOrigin="-0.673,-3.125" Height="24" Width="40" Text="端口" FontSize="16" LineHeight="8" TextWrapping="Wrap" TextAlignment="Center"/>
        <TextBlock x:Name="textBlock1" HorizontalAlignment="Left" Margin="8,108,0,0" TextWrapping="Wrap" Text="波特率" VerticalAlignment="Top" Width="60" Height="28" FontSize="16"/>
        <Button x:Name="clr" Content="clear" HorizontalAlignment="Left" Margin="8,240,0,0" VerticalAlignment="Top" Width="50" Click="Clr_Click" Height="28" FontSize="16" Background="{x:Null}"/>
        <Button x:Name="chart" Content="chart" HorizontalAlignment="Left" Margin="100,240,0,0" VerticalAlignment="Top" Width="50" Click="Chart_Click" Height="28" Background="{x:Null}" FontSize="16"/>
        <Button x:Name="table" Content="table" HorizontalAlignment="Left" Margin="100,300,0,0" VerticalAlignment="Top" Width="50" RenderTransformOrigin="0.037,-0.94" Height="28" Background="{x:Null}" FontSize="16" Click="Table_Click"/>
        <Button x:Name="save" Content="save" HorizontalAlignment="Left" Margin="8,300,0,0" VerticalAlignment="Top" Width="50" Height="28" Background="{x:Null}" FontSize="16" Click="Save_Click"/>
    </Grid>

后台代码如下

public  partial class MainWindow : Window
    {
        string getstring = "";
        string putoutstring = "";
        //声明串口
        SerialPort ComPort = new SerialPort();
        public delegate void Display(byte[] buffer);
        public Display Dis_UI;
        public   MainWindow()
        {     
            InitializeComponent();
            ComPort.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);
            Dis_UI = new Display(display);
        }
        //接收数据事件
        void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            int count = ComPort.BytesToRead;
            byte[] buffer = new byte[count];
            ComPort.Read(buffer, 0, count);
            //多线程操作UI
            this.Dispatcher.Invoke(Dis_UI, buffer);
        }
        public  void display(byte[] buf)
        {
            for (int i = 0; i < buf.Length; i++)
                {
                    getstring += Char.ConvertFromUtf32(buf[i]);
                }
            
            if  (getstring .Contains(@"$GNVTG") )
                {
                    App.wxid.Clear();
                    App.wxxzb.Clear();
                    string pattern = @"\$BDGSA,(?<GPS>[\s\S]+)\$BDGSV";
                    Match mc = Regex .Match(getstring, pattern);
                    string tempStr = mc.Groups["GPS"].ToString ();
                    string[] Gpsarray = Regex.Split(tempStr, "\r\n", RegexOptions.IgnoreCase);
                    foreach (string istring in Gpsarray)
                    {
                        try
                        {
                            if (istring.Contains(@"$GPGSV"))
                            {
                                string[] sArray = Regex.Split(istring, ",", RegexOptions.IgnoreCase);
                                try
                                {
                                    App.wxid.Add(sArray[4]);
                                    App.wxxzb.Add(Convert.ToDouble(sArray[7]));
                                }
                                catch
                                {
                                }
                            }
                        }
                        catch
                        {
                        }
                    }
                    textBox.Text =getstring+ "\n" + System.DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") + "\n";
                    putoutstring = getstring + "\n" + System.DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") + "\n";
                    getstring = "";
                }
        }
        //串口集
        public string[] portNames { get; set; }
        //打开标志
        bool openFlag = false;
        //得到当前可用串口
        private void GetPort()
        {
            portNames = SerialPort.GetPortNames();         //得到可用串口
            if (portNames.Length > 0)
            {
                Tip.Text = "检测到" + portNames.Length + "个可用串口";
            }
            else
            {
                Tip.Text = "未检测到串口";
                Port_Select.ItemsSource = null;
                return;
            }
            Port_Select.ItemsSource = portNames;           //添加可用串口
            Port_Select.SelectedValue = portNames[0];      //默认选中
        }
        //打开事件
        private void PortOpen_Click(object sender, RoutedEventArgs e)
        {
            OpenPort();
        }
        //鼠标进入事件
        private void PortSelect_MouseEnter(object sender, MouseEventArgs e)
        {
            if (openFlag == false)
            {
                GetPort();
            }
        }
        //打开串口方法
        private void OpenPort()
        {
            if (Port_Select.SelectedValue == null)
            {
                GetPort();
            }
            if (openFlag == false)
            {
                try
                {
                    //设置
                    ComPort.PortName = Port_Select.SelectedValue.ToString();
                    ComPort.BaudRate = Convert.ToInt32(comboBox.SelectionBoxItem.ToString ());
                    ComPort.Parity = Parity.None;
                    ComPort.StopBits = StopBits.One;
                    ComPort.DataBits = 8;
                    ComPort.Handshake = Handshake.None;
                    // ComPort.RtsEnable = true;
                    ComPort.Open();
                    Tip.Text = "打开成功";
                    openFlag = true;
                    Port_Open.Content = "关闭串口";
                }
                catch
                {
                    Tip.Text = "打开失败";
                }
            }
            else
            {
                try
                {
                    ComPort.DiscardInBuffer();
                    ComPort.DiscardOutBuffer();
                    ComPort.Close();
                    openFlag = false;
                    Port_Open.Content = "打开串口";
                    Tip.Text = "关闭成功";
                }
                catch
                {
                    Tip.Text = "关闭失败";
                }
            }
        }
        //清除显示文本
        private void Clr_Click(object sender, RoutedEventArgs e)
        {
            this.textBox .Text ="";
        }
        //打开图表窗口
        private void Chart_Click(object sender, RoutedEventArgs e)
        {
            chartwindow Chartwindow = new chartwindow();
            Chartwindow.Show();
        }
        //打开表格窗口
        private void Table_Click(object sender, RoutedEventArgs e)
        {
            table Table = new ck.table();
            Table.Show();
        }
        //保存数据文本
        private void Save_Click(object sender, RoutedEventArgs e)
        {
            SaveFileDialog saveDlg = new SaveFileDialog();
            saveDlg.Filter = "文本(*.txt)|*.txt;|Excle(*.xls)|*.xls";
            if (saveDlg.ShowDialog() == true)
            {
                using (FileStream fs = new FileStream(saveDlg.FileName, FileMode.Create))
                {
                    using (StreamWriter sw = new StreamWriter(fs, Encoding.Default))
                    {
                        sw.WriteLine(putoutstring);
                    }
                }
            }
        }
    }
相关推荐
全套源代码,请咨询 ny_mrzhang@hotmail.com QQ:97302751 以前的协议是这样的。。。车载设备发往网络中心<br>位置数据-80<br>[功能] 车载设备向网络中心发送当前位置信息<br>[去向] 网络中心<br>[数据入库] 网络中心收到数据后将数据保存到动态位置数据表中 (DynData)<br>[格式] 29 29 80 len1 len2 d3d2d1d0 yymmddhhmmss jjjj wwww ssff st lichen1 lichen2 lichen3 st1st2st3 st4 v1v2v3v4v5v6v7v8 <br>len1 len2: 包长<br>d3d2d1d0: 伪IP,终端ID号<br>yymmddhhmmss:年月日时分秒<br>日期表示范围:年份,后两位 ;月份,1-12;日期,1-31<br>时间表示范围:小时,00——23;分钟,00——59;秒,00——59<br>时、分、秒均采用压缩BCD编码。<br>[例如]: 10点23分15秒表示为:<br> 10H,23H,15H<br>jjjj:经度<br>经度表示范围:000度00.000分——179度59.999分;<br>采用压缩BCD编码,但最高位是符号位。“正”表示“东经”,“负”表示“西经”;<br>“分”的单位是:千分之一分。<br>[例如]: 西经130度45.608分表示为:(注意:1度=60分)<br> 13H,04H,56H,08H<br>wwww:纬度<br>纬度表示范围:00度00.000分——89度59.999分;<br>采用压缩BCD编码,但最高位是符号位。“正”表示“北纬”,“负”表示“南纬”;<br>“分”的单位是:千分之一分。<br>[例如]: 南纬30度37.901分表示为:<br> 3H,03H,79H,01H<br>ssff:速度方向<br>速度表示方法:<br>表示范围:0——9999公里/小时<br>采用压缩BCD编码。<br>[例如]: 120公里/小时表示为:<br> 01H,20H<br>方位表示方法:<br>表示范围:000——359度<br>采用压缩BCD编码,正北为0度,顺时针方向计数。<br>单位为:度。<br>[例如]: 154度表示为:<br> 01H,54H<br>st:定位,天线,电源状态<br>定位状态由单字节组成,各位含义如下:<br> D7 D6 D5 D4 D3 D2 D1 D0<br><br>D7 定位标志<br>0 GPS未定位<br>1 GPS已定位<br><br>D6 D5 <br>1 1 GPS正常<br>1 0 GPS天线短路<br>0 1 GPS天线开路<br>0 0 GPS天线故障<br><br>D4 D3 电源状态<br>1 1 正常<br>1 0 主电源掉电<br>0 1 主电源过高或过低<br> <br><br>D2 D1 D0 保留<br> <br> <br> <br><br><br>lichen1 lichen2 lichen3:里程数<br> HEX 格式:0——0XFFFFFF (0——16777215)米<br>st1st2st3 st4:车辆状态<br><br><br><br><br>现在协议格式:*HQ,000,S12,130305,60# 上传短信<br>*HQ,000,S4,121212,0F,FF# 速度显示<br>*HQ,000,S4,121212,9F,FF# 断油断电
©️2020 CSDN 皮肤主题: 精致技术 设计师:CSDN官方博客 返回首页