作业帮 > 综合 > 作业

我用c#做一个串口接收GPS的的程序,但是要如何完整接收GPS发过来的数据呢?

来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/05/10 07:35:53
我用c#做一个串口接收GPS的的程序,但是要如何完整接收GPS发过来的数据呢?
我用一个jieshou_Click按钮,单击一下,就从串口读入大量的数据,可是停不下来,等一下,程序就未响应了,
要如何解决.
private void jieshou_Click(object sender, EventArgs e)
{
ReadData();
}
private void ReadData()
{
while(true)
{
recb = mycom1.Read(117);
msg.AppendText("\r\n接收到数据包:" + recb);
}
}
现在我想用了serialPort1_DataReceived事件,把GPS里的一个字符一个字符读取下来,就是没用过serialPort这个类,
不知道有没有人熟悉这个类的,或是编过类似的程序的大侠,给个例子或代码参考下.
用SerialPort类的时候,在接收数据的serialPort1_DataReceived事件里要用委托.
示例:
private void serialPort1_DataReceived(object sender,SerialDataReceivedEventArgs e)
{
string tmpstr = "";
for (int i = 0; i < serialPort1.BytesToRead; i++)
{
tmpstr += Convert.ToString(serialPort1.ReadByte(),16)+ " ";
}
tmpstr= tmpstr.ToUpper();
safeAddtrText(tmpstr);
}
//由于本人比较懒,委托就从网上直接Copy了.
public delegate void _SafeAddtrTextCall(string text);
private void safeAddtrText(string text)
{
if (this.InvokeRequired)
{
_SafeAddtrTextCall call =
delegate(string s)
{
txtBoxRecive.Text += s;
};
this.Invoke(call,text);
}
else
{
txtBoxRecive.Text += text;
}
}