作者fiend1212 (依旧是帅气小禹)
看板C_Sharp
标题[问题] 关闭GPS後却还读的到讯息
时间Tue Jul 27 22:53:23 2010
小弟最近想读取Windows Mobile GPS的经纬度资料
可是都只能从SerialPort读取到
$GPGGA,,,,,,0,,,,,,,,*60
且在读到GPGGA之前还会读到一些GPGSV的讯息
手机里的外部GPS设定是设定COM4 BaudRate 4800和SerialPort设定是一致的
然後如果把GPS关闭情况也一样耶...也不会出现读不到或空白之类情形
以下是我的程式码
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;
namespace GPS
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
try
{
serialPort1.Open();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "SerialPort开启错误");
}
Thread.Sleep(1000);
GPS_thread = new Thread(new ThreadStart(Get_GPS));
GPS_thread.Start();
}
private void Get_GPS()
{
Thread.Sleep(1000);
while ((temp = serialPort1.ReadLine()) != null &&
serialPort1.IsOpen)
{
temp2 += temp + "\n";
if (textBox1.InvokeRequired)
textBox1.Invoke(new textBox1Callback(Set_textBox1), new
object[] { });
else
textBox1.Text = temp2;
string[] check = temp.Split(',');
if (check[0] == "$GPGGA")
{
flag = 0;
if (textBox2.InvokeRequired)
textBox2.Invoke(new textBox1Callback(Set_textBox1),
new object[] { });
else
textBox2.Text = data;
if (check[2] != "" && check[4] != "")
{
serialPort1.Close();
serialPort1.Dispose();
break;
}
else
{
temp2 = "";
flag = 1;
}
}
Thread.Sleep(1000);
}
}
private void Set_textBox1()
{
if(flag == 1)
textBox1.Text = temp2;
else
textBox2.Text = data;
}
private void close_Click(object sender, EventArgs e)
{
serialPort1.Close();
GPS_thread.Join();
GPS_thread.Abort();
serialPort1.Dispose();
}
}
}
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 219.71.8.243
1F:→ liaommx:很简单的情况,因为GPS关闭不代表程式关闭读取serial port 07/27 22:57
2F:→ liaommx:所以你应该要关闭的是serial port,避免他继续读取资料 07/27 22:58
3F:→ fiend1212:可是我是把程式关闭後把GPS关掉再重开耶... 07/28 00:04
4F:→ fiend1212:程式关闭前有先按close那个Button把Port关掉 07/28 00:04