解析GPS模拟器发来的串口数据
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Text;
7 using System.Windows.Forms;
8
9
10 using System.IO.Ports;
11
12
13 namespace ParseSerialData
14 {
15 public partial class Form1 : Form
16 {
17 private delegate void dg(string str);
18 /// <summary>
19 /// datagridview的数据源
20 /// </summary>
21 DataTable dt = new DataTable();
22
23 public Form1()
24 {
25 InitializeComponent();
26 AddColumn();
27 }
28
29 private void AddColumn()
30 {
31 string[] strColumn = { "UTC时间", "纬度", "经度", "GPS状态", "卫星数", "平面精度因子", "天线高度", "海平面分离度", "差分校正时延(秒)", "参考站识别", "校验和" };
32 foreach (string item in strColumn)
33 {
34 DataColumn dc = new DataColumn(item);
35 dt.Columns.Add(dc);
36 }
37 dataGridView1.DataSource = dt;
38 }
39
40 private void Form1_Load(object sender, EventArgs e)
41 {
42 string[] Ports = SerialPort.GetPortNames();
43 foreach (string item in Ports)
44 {
45 comboBoxPort.Items.Add(item);
46 }
47 comboBoxPort.SelectedIndex = 0;
48 SetPortProperty();
49 }
50
51 private void comboBoxPort_SelectedIndexChanged(object sender, EventArgs e)
52 {
53 SetPortProperty();
54 }
55
56 private void SetPortProperty()
57 {
58 serialPort1.PortName = comboBoxPort.SelectedItem.ToString();
59 serialPort1.Parity = Parity.Odd;
60 serialPort1.StopBits = StopBits.Two;
61 }
62
63 private void btnOpenPort_Click(object sender, EventArgs e)
64 {
65 if (serialPort1.PortName == "")
66 {
67 MessageBox.Show("请选择一个端口", "提示信息");
68 }
69 else if (!serialPort1.IsOpen)
70 {
71 serialPort1.Open();
72 comboBoxPort.Enabled = false;
73 }
74 }
75
76 private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
77 {
78 string strMessage = serialPort1.ReadLine();
79 string subMsg = strMessage.Substring(0, 6);
80 if (subMsg == "$GPGGA")
81 {
82 string[] temp = ParseGPSMsg(strMessage);
83 DataRow dr = dt.NewRow();
84 for (int i = 0; i < 11; i++)
85 {
86
87 dr[i] = temp[i];
88
89 }
90 dt.Rows.Add(dr);
91
92 }
93 dg dg1 = new dg(AppendText);
94 this.Invoke (dg1, new object[] {strMessage});
95
96 }
97
98 /// <summary>
99 /// 解析分割字符串
100 /// </summary>
101 /// <param name="message"></param>
102 /// <returns></returns>
103 private string[] ParseGPSMsg(string message)
104 {
105 string[] temp = message.Split(new char[] { ',' });
106 string[] arrMsg = { temp[1],temp [2]+temp [3],temp [4]+ temp [5]
107 ,temp [6],temp [7],temp [8],temp [9] +temp [10]
108 ,temp [11]+temp [12],temp [13],temp [14].Substring (0,4)
109 ,temp [14].Substring (4,3)};
110 //GPSPosition gpsPos = new GPSPosition(arrMsg);
111
112 return arrMsg;
113 }
114
115 private void comboBoxPort_DropDown(object sender, EventArgs e)
116 {
117 comboBoxPort.Items.Clear();
118 string[] Ports = SerialPort.GetPortNames();
119 foreach (string item in Ports)
120 {
121 comboBoxPort.Items.Add(item);
122 }
123 comboBoxPort.SelectedIndex = 0;
124 }
125 private void AppendText(string Msg)
126 {
127 textBox1.Text = textBox1.Text + Msg;
128 dataGridView1.DataSource = dt;
129 dataGridView1.Refresh();
130 }
131
132 private void btnClosePort_Click(object sender, EventArgs e)
133 {
134 serialPort1.Close();
135 comboBoxPort.Enabled = true;
136 }
137 }
138 }
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Text;
7 using System.Windows.Forms;
8
9
10 using System.IO.Ports;
11
12
13 namespace ParseSerialData
14 {
15 public partial class Form1 : Form
16 {
17 private delegate void dg(string str);
18 /// <summary>
19 /// datagridview的数据源
20 /// </summary>
21 DataTable dt = new DataTable();
22
23 public Form1()
24 {
25 InitializeComponent();
26 AddColumn();
27 }
28
29 private void AddColumn()
30 {
31 string[] strColumn = { "UTC时间", "纬度", "经度", "GPS状态", "卫星数", "平面精度因子", "天线高度", "海平面分离度", "差分校正时延(秒)", "参考站识别", "校验和" };
32 foreach (string item in strColumn)
33 {
34 DataColumn dc = new DataColumn(item);
35 dt.Columns.Add(dc);
36 }
37 dataGridView1.DataSource = dt;
38 }
39
40 private void Form1_Load(object sender, EventArgs e)
41 {
42 string[] Ports = SerialPort.GetPortNames();
43 foreach (string item in Ports)
44 {
45 comboBoxPort.Items.Add(item);
46 }
47 comboBoxPort.SelectedIndex = 0;
48 SetPortProperty();
49 }
50
51 private void comboBoxPort_SelectedIndexChanged(object sender, EventArgs e)
52 {
53 SetPortProperty();
54 }
55
56 private void SetPortProperty()
57 {
58 serialPort1.PortName = comboBoxPort.SelectedItem.ToString();
59 serialPort1.Parity = Parity.Odd;
60 serialPort1.StopBits = StopBits.Two;
61 }
62
63 private void btnOpenPort_Click(object sender, EventArgs e)
64 {
65 if (serialPort1.PortName == "")
66 {
67 MessageBox.Show("请选择一个端口", "提示信息");
68 }
69 else if (!serialPort1.IsOpen)
70 {
71 serialPort1.Open();
72 comboBoxPort.Enabled = false;
73 }
74 }
75
76 private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
77 {
78 string strMessage = serialPort1.ReadLine();
79 string subMsg = strMessage.Substring(0, 6);
80 if (subMsg == "$GPGGA")
81 {
82 string[] temp = ParseGPSMsg(strMessage);
83 DataRow dr = dt.NewRow();
84 for (int i = 0; i < 11; i++)
85 {
86
87 dr[i] = temp[i];
88
89 }
90 dt.Rows.Add(dr);
91
92 }
93 dg dg1 = new dg(AppendText);
94 this.Invoke (dg1, new object[] {strMessage});
95
96 }
97
98 /// <summary>
99 /// 解析分割字符串
100 /// </summary>
101 /// <param name="message"></param>
102 /// <returns></returns>
103 private string[] ParseGPSMsg(string message)
104 {
105 string[] temp = message.Split(new char[] { ',' });
106 string[] arrMsg = { temp[1],temp [2]+temp [3],temp [4]+ temp [5]
107 ,temp [6],temp [7],temp [8],temp [9] +temp [10]
108 ,temp [11]+temp [12],temp [13],temp [14].Substring (0,4)
109 ,temp [14].Substring (4,3)};
110 //GPSPosition gpsPos = new GPSPosition(arrMsg);
111
112 return arrMsg;
113 }
114
115 private void comboBoxPort_DropDown(object sender, EventArgs e)
116 {
117 comboBoxPort.Items.Clear();
118 string[] Ports = SerialPort.GetPortNames();
119 foreach (string item in Ports)
120 {
121 comboBoxPort.Items.Add(item);
122 }
123 comboBoxPort.SelectedIndex = 0;
124 }
125 private void AppendText(string Msg)
126 {
127 textBox1.Text = textBox1.Text + Msg;
128 dataGridView1.DataSource = dt;
129 dataGridView1.Refresh();
130 }
131
132 private void btnClosePort_Click(object sender, EventArgs e)
133 {
134 serialPort1.Close();
135 comboBoxPort.Enabled = true;
136 }
137 }
138 }