作者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