reader.cs
上传用户:tupian
上传日期:2009-05-04
资源大小:93k
文件大小:3k
源码类别:

GPS编程

开发平台:

C#

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Threading;
  5. using System.IO.Ports;
  6. namespace GpsTracer
  7. {
  8.     public class reader 
  9.     {
  10.         static private System.IO.Ports.SerialPort m_serialPort1;
  11.         static private Thread m_th=null;
  12.         static private bool m_run=false;
  13.         static public String m_readed="";
  14.         static private String m_port;
  15.         static private bool m_demo;
  16.         public delegate void DataReceivedEventHandler(string data);
  17.         public event DataReceivedEventHandler dataReceived;
  18.         public reader(String port)
  19.         {
  20.          m_port = port;
  21.         }
  22.         public void start(bool demo)
  23.         {
  24.             m_demo = demo;
  25.             
  26.             if (m_th == null)
  27.             {
  28.                 if(!demo && m_serialPort1==null)
  29.                 {
  30.                     m_serialPort1 = new System.IO.Ports.SerialPort(m_port);
  31.                 }
  32.                 m_th = new Thread(methodTh);
  33.             }
  34.             m_run = true;
  35.             m_th.Start();
  36.         }
  37.         public void stop()
  38.         {
  39.             m_run = false;
  40.             Thread.Sleep(500);
  41.             if (!m_demo)
  42.             {
  43.                 m_serialPort1.Close();
  44.             }
  45.             if (m_th != null)
  46.             {
  47.                 m_th.Abort();
  48.                 m_th = null;
  49.             }
  50.         }
  51.         
  52.         private void methodTh()
  53.         {
  54.             if (!m_demo)
  55.             {
  56.                 m_serialPort1.Open();
  57.             }
  58.             byte[] buffer= new byte[100];
  59.             while (m_run)
  60.             {
  61.                 Thread.Sleep(1000);
  62.                 if (m_demo)
  63.                 {
  64.                     dataReceived(randomString());
  65.                 }
  66.                 else
  67.                 {
  68.                     m_readed = m_serialPort1.ReadLine();
  69.                     if (m_readed.Length > 0)
  70.                     {
  71.                         dataReceived(m_readed);
  72.                     }
  73.                 }
  74.             }
  75.         }
  76.         private String randomString()
  77.         { 
  78.             Random r = new Random();
  79.             String rSecond1 = (int)(r.NextDouble() * 10 - 1) + "" + (int)(r.NextDouble() * 10 - 1) + "" + (int)(r.NextDouble() * 10 - 1) + "" + (int)(r.NextDouble() * 10 - 1);
  80.             String rSecond2 = (int)(r.NextDouble() * 10 - 1) + "" + (int)(r.NextDouble() * 10 - 1) + "" + (int)(r.NextDouble() * 10 - 1) + "" + (int)(r.NextDouble() * 10 - 1);
  81.             String rPrime1 = (int)(r.NextDouble() * 1 - 1) + "";
  82.             String rPrime2 = (int)(r.NextDouble() * 1 - 1) + "";
  83.             
  84.             return "$GPRMC,170111,A,434"+ rPrime1 + "." + rSecond1+",N,0111" + rPrime2 + "." + rSecond2+",W,000.0,360.0,060199,017.5,W*73";
  85.          }
  86.     }
  87. }