我收集了一些GPS数据在t.txt文档中,现在要将这里面的数据通过虚拟串口发给一个PC版的导航软件。
但我这发送有点问题,点一下就发送一部分数据,再点一下又发送一部分
请问下如何才能让程序自动一步一步发啊~~?请赐教
代码如下:
using System;
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;
using System.IO;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
SerialPort _serialPort;
string filename = @"E:\t.txt";
StreamWriter sw = null;
bool _continue = true;
public Form1()
{
InitializeComponent();
}
private void InitPort()
{
StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
// Create a new SerialPort object with default settings.
_serialPort = new SerialPort();
// Allow the user to set the appropriate properties.
_serialPort.PortName = "COM5";//SetPortName(_serialPort.PortName);
_serialPort.BaudRate = 4800;// SetPortBaudRate(_serialPort.BaudRate);
_serialPort.Parity = Parity.None;// SetPortParity(_serialPort.Parity);
_serialPort.DataBits = 8;//SetPortDataBits(_serialPort.DataBits);
_serialPort.StopBits = StopBits.One;// SetPortStopBits(_serialPort.StopBits);
_serialPort.Handshake = Handshake.RequestToSend;// SetPortHandshake(_serialPort.Handshake);
_serialPort.RtsEnable = true;
// Set the read/write timeouts
_serialPort.ReadTimeout = 500;
_serialPort.WriteTimeout = 500;
_serialPort.Open();
}
public void Read()
{
while (_continue)
{
try
{
//string message = _serialPort.ReadLine();
string message = _serialPort.ReadLine();
sw.Write(message);
//Console.WriteLine(message);
}
catch (TimeoutException) { }
}
sw.Close();
}
private void Form1_Load(object sender, EventArgs e)
{
InitPort();
}
private void btnRead_Click(object sender, EventArgs e)
{
_continue = true;
sw = new StreamWriter(filename, true);
Thread readThread = new Thread(Read);
readThread.Start();
}
private void btnStop_Click(object sender, EventArgs e)
{
_continue = false;
}
private void btnWrite_Click(object sender, EventArgs e)
{
StreamReader sr = new StreamReader(filename);
while (sr.Peek() != -1)
{
string message = sr.ReadLine();
//try
//{
_serialPort.WriteLine(message);
//}
//catch (TimeoutException) { }
}
sr.Close();
}
}
}