reader.cs
上传用户:tupian
上传日期:2009-05-04
资源大小:93k
文件大小:3k
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Threading;
- using System.IO.Ports;
- namespace GpsTracer
- {
- public class reader
- {
- static private System.IO.Ports.SerialPort m_serialPort1;
- static private Thread m_th=null;
- static private bool m_run=false;
- static public String m_readed="";
- static private String m_port;
- static private bool m_demo;
- public delegate void DataReceivedEventHandler(string data);
- public event DataReceivedEventHandler dataReceived;
- public reader(String port)
- {
- m_port = port;
- }
- public void start(bool demo)
- {
- m_demo = demo;
-
- if (m_th == null)
- {
- if(!demo && m_serialPort1==null)
- {
- m_serialPort1 = new System.IO.Ports.SerialPort(m_port);
- }
- m_th = new Thread(methodTh);
- }
- m_run = true;
- m_th.Start();
- }
- public void stop()
- {
- m_run = false;
- Thread.Sleep(500);
- if (!m_demo)
- {
- m_serialPort1.Close();
- }
- if (m_th != null)
- {
- m_th.Abort();
- m_th = null;
- }
- }
-
- private void methodTh()
- {
- if (!m_demo)
- {
- m_serialPort1.Open();
- }
- byte[] buffer= new byte[100];
- while (m_run)
- {
- Thread.Sleep(1000);
- if (m_demo)
- {
- dataReceived(randomString());
- }
- else
- {
- m_readed = m_serialPort1.ReadLine();
- if (m_readed.Length > 0)
- {
- dataReceived(m_readed);
- }
- }
- }
- }
- private String randomString()
- {
- Random r = new Random();
- String rSecond1 = (int)(r.NextDouble() * 10 - 1) + "" + (int)(r.NextDouble() * 10 - 1) + "" + (int)(r.NextDouble() * 10 - 1) + "" + (int)(r.NextDouble() * 10 - 1);
- String rSecond2 = (int)(r.NextDouble() * 10 - 1) + "" + (int)(r.NextDouble() * 10 - 1) + "" + (int)(r.NextDouble() * 10 - 1) + "" + (int)(r.NextDouble() * 10 - 1);
- String rPrime1 = (int)(r.NextDouble() * 1 - 1) + "";
- String rPrime2 = (int)(r.NextDouble() * 1 - 1) + "";
-
- return "$GPRMC,170111,A,434"+ rPrime1 + "." + rSecond1+",N,0111" + rPrime2 + "." + rSecond2+",W,000.0,360.0,060199,017.5,W*73";
- }
- }
- }