|
发表于 2015-3-1 15:22:25
|
显示全部楼层
- using System;
- using System.IO;
- using System.IO.Ports;
- using System.Text;
- using System.Threading;
- namespace Termie
- {
- public sealed class CommPort
- {
- public delegate void EventHandler(string param);
- private SerialPort _serialPort;
- private Thread _readThread;
- private volatile bool _keepReading;
- private static readonly CommPort instance;
- public CommPort.EventHandler StatusChanged;
- public CommPort.EventHandler DataReceived;
- public static CommPort Instance
- {
- get
- {
- return CommPort.instance;
- }
- }
- public bool IsOpen
- {
- get
- {
- return this._serialPort.IsOpen;
- }
- }
- static CommPort()
- {
- CommPort.instance = new CommPort();
- }
- private CommPort()
- {
- this._serialPort = new SerialPort();
- this._readThread = null;
- this._keepReading = false;
- }
- private void StartReading()
- {
- if (!this._keepReading)
- {
- this._keepReading = true;
- this._readThread = new Thread(new ThreadStart(this.ReadPort));
- this._readThread.Start();
- }
- }
- private void StopReading()
- {
- if (this._keepReading)
- {
- this._keepReading = false;
- this._readThread.Join();
- this._readThread = null;
- }
- }
- private void ReadPort()
- {
- while (this._keepReading)
- {
- if (this._serialPort.IsOpen)
- {
- byte[] array = new byte[this._serialPort.ReadBufferSize + 1];
- try
- {
- int count = this._serialPort.Read(array, 0, this._serialPort.ReadBufferSize);
- string @string = Encoding.GetEncoding("Gb2312").GetString(array, 0, count);
- this.DataReceived(@string);
- }
- catch (TimeoutException)
- {
- }
- }
- else
- {
- TimeSpan timeout = new TimeSpan(0, 0, 0, 0, 50);
- Thread.Sleep(timeout);
- }
- }
- }
- public void Open()
- {
- this.Close();
- try
- {
- this._serialPort.PortName = Settings.Port.PortName;
- this._serialPort.BaudRate = Settings.Port.BaudRate;
- this._serialPort.Parity = Settings.Port.Parity;
- this._serialPort.DataBits = Settings.Port.DataBits;
- this._serialPort.StopBits = Settings.Port.StopBits;
- this._serialPort.Handshake = Settings.Port.Handshake;
- this._serialPort.Encoding = Encoding.GetEncoding("Gb2312");
- this._serialPort.ReadTimeout = 50;
- this._serialPort.WriteTimeout = 50;
- this._serialPort.Open();
- this.StartReading();
- }
- catch (IOException)
- {
- this.StatusChanged(string.Format("{0} does not exist", Settings.Port.PortName));
- }
- catch (UnauthorizedAccessException)
- {
- this.StatusChanged(string.Format("{0} already in use", Settings.Port.PortName));
- }
- catch (Exception ex)
- {
- this.StatusChanged(string.Format("{0}", ex.ToString()));
- }
- if (this._serialPort.IsOpen)
- {
- string text = this._serialPort.Parity.ToString().Substring(0, 1);
- string text2 = this._serialPort.Handshake.ToString();
- if (this._serialPort.Handshake == Handshake.None)
- {
- text2 = "no handshake";
- }
- this.StatusChanged(string.Format("{0}: {1} bps, {2}{3}{4}, {5}", new object[]
- {
- this._serialPort.PortName,
- this._serialPort.BaudRate,
- this._serialPort.DataBits,
- text,
- (int)this._serialPort.StopBits,
- text2
- }));
- }
- else
- {
- this.StatusChanged(string.Format("{0} already in use", Settings.Port.PortName));
- }
- }
- public void Close()
- {
- this.StopReading();
- this._serialPort.Close();
- this.StatusChanged("connection closed");
- }
- public string[] GetAvailablePorts()
- {
- return SerialPort.GetPortNames();
- }
- public int Send(string data)
- {
- int result;
- if (this.IsOpen)
- {
- try
- {
- this._serialPort.Write(data);
- result = data.Length;
- return result;
- }
- catch (Exception ex)
- {
- this._serialPort.Close();
- this.StatusChanged(ex.Message);
- result = 0;
- return result;
- }
- }
- result = 0;
- return result;
- }
- public bool ShowCTS()
- {
- bool result;
- try
- {
- result = this._serialPort.CtsHolding;
- }
- catch (Exception ex)
- {
- this.StatusChanged(ex.Message);
- result = false;
- }
- return result;
- }
- public bool ShowDSR()
- {
- bool result;
- try
- {
- result = this._serialPort.DsrHolding;
- }
- catch (Exception ex)
- {
- this.StatusChanged(ex.Message);
- result = false;
- }
- return result;
- }
- public bool ShowRLSD()
- {
- bool result;
- try
- {
- result = this._serialPort.CDHolding;
- }
- catch (Exception ex)
- {
- this.StatusChanged(ex.Message);
- result = false;
- }
- return result;
- }
- public bool ShowRING()
- {
- bool result;
- try
- {
- result = false;
- }
- catch (Exception ex)
- {
- this.StatusChanged(ex.Message);
- result = false;
- }
- return result;
- }
- public void SelDTR(bool stat)
- {
- try
- {
- this._serialPort.DtrEnable = stat;
- }
- catch (Exception ex)
- {
- this.StatusChanged(ex.Message);
- this._serialPort.DtrEnable = false;
- }
- }
- public void SelRTS(bool stat)
- {
- try
- {
- this._serialPort.RtsEnable = stat;
- }
- catch (Exception ex)
- {
- this.StatusChanged(ex.Message);
- this._serialPort.RtsEnable = false;
- }
- }
- public void SelBREAK(bool stat)
- {
- try
- {
- this._serialPort.BreakState = stat;
- }
- catch (Exception ex)
- {
- this.StatusChanged(ex.Message);
- this._serialPort.BreakState = false;
- }
- }
- }
- }
复制代码 |
|