最近学了很多,也是时候自己做点东西来复习并且巩固下之前的知识了。学以致用最重要。最近写了个小软件,用来处理处理从窗口发送过来的GPS信息。GPS是与STM32相连接任何STM32把GPS信息发到电脑的串口上。任何我写了个C#的程序通过串口接收到信息,并对信息进行处理得到我们需要的经纬度,并通过经纬度查找到具体的地址。可以表达的不太清晰,想来几张图来个大家看看具体的效果。任何在讲讲具体的程序。
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SerialPort sp = new SerialPort();
//private void button1_Click(object sender, EventArgs e)
//{
// //sp.Close();
// sp.PortName = "COM8";
// sp.BaudRate = 38400;
// sp.Open();
//}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
sp.PortName = "COM8"; //端口
sp.BaudRate = 38400; //波特率
sp.Open(); //打开串口
bool stop = false; //标志位
[page]
string longitude = "0";//经度
string latitude = "0";//纬度
while (!stop)
{
try
{
string message = sp.ReadLine();
message = message.Substring(0, message.Length - sp.NewLine.Length);
if (message.StartsWith("$GPGGA"))
{
//模拟定位
//message = "$GPGGA,012440.00,2232.458380,N,11356.91024,E,1,05,2.7,40.2,M,0.5,M,,*6F";
//message = "$GPGGA,012440.00,2232.301654,N,11402.058366,E,1,05,2.7,40.2,M,0.5,M,,*6F";
char[] separator = { ',' };
string[] data = message.Split(separator);
if (data[6].Equals("1"))
{
string s;
s = data[2];
int idx = s.IndexOf(".");
string d1 = s.Substring(0, idx - 2);
string d2 = s.Substring(idx - 2);
double degree = Double.Parse(d1) + Double.Parse(d2) / 60;
latitude = Convert.ToString(Convert.ToDouble(s) / 100);//纬度
string j;
j = data[4];
int idxj = j.IndexOf(".");
string d1j = j.Substring(0, idxj - 2);
string d2j = j.Substring(idxj - 2);
double degreej = Double.Parse(d1j) + Double.Parse(d2j) / 60;
longitude = Convert.ToString(Convert.ToDouble(j)/100);//经度
textBox1.Text = longitude;
textBox2.Text = latitude;
stop = true;
}
else
{
sp.Close();
sp.PortName = "COM8";
sp.BaudRate = 38400;
sp.Open();
// button2_Click(button2,EventArgs e);
}
}
else if (message.StartsWith("$GPGGA"))
{
char[] separator = { ',' };
string[] data = message.Split(separator);
}
}
catch (TimeoutException) { }
}
sp.Close();
// sp = null;
//textBox4.Text = adrss();2
MessageBox.Show("GPS已停止,请等待地址信息的查询!");
textBox4.Text = GPSadrss.adrss(latitude, longitude);
}
class GPSadrss
{
public static string adrss(string latitude, string longitude)
{
WebClient client = new WebClient();//webclient客户端对象
//string url = "http://maps.google.com/maps/api/geocode/xml?latlng=29.82121,121.55973&language=zh-CN&sensor=false";//请求地址
string url = "http://maps.google.com/maps/api/geocode/xml?latlng="+latitude+","+longitude
+"&language=zh-CN&sensor=false";//请求地址
client.Encoding = Encoding.UTF8;//编码格式
string responseTest = client.DownloadString(url);//下载xml响应数据
XmlDocument doc = new XmlDocument();//创建XML文档对象
if (!string.IsNullOrEmpty(responseTest))
{
doc.LoadXml(responseTest);//加载xml字符串
//获取状态信息
string xpath = @"GeocodeResponse/status";
XmlNode node = doc.SelectSingleNode(xpath);
string status = node.InnerText.ToString();
if (status == "OK")
{
//获取地址信息
xpath = @"GeocodeResponse/result/formatted_address";
node = doc.SelectSingleNode(xpath);
string address = node.InnerText.ToString();
string[]ad= address.Split();
return ad[0];
}
return "false";
}
return "false";
}