using System; using System.Collections.Generic; using System.Configuration; using System.Globalization; using System.IO; using System.IO.Ports; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading; using System.Windows.Forms; namespace FlyDockTool { public class SerialPortOption { /// /// 让此类创建一个串口操作的单例类 /// public static SerialPortOption Instance; //申明一个EcgDrawing对象,复制Null public static int BufferBytesLength = 0; private Thread _thSerialPortDataRead; //串口数据读取线程 public SerialPort DataReadSerialPort; public DateTime SerialTime; private static readonly object LockHelper = new object(); public EventHandler dataOutPutShow; public static SerialPortOption CreateInstance() { if (Instance == null) lock (LockHelper) { if (Instance == null) Instance = new SerialPortOption(); } return Instance; } /// /// 初始化串口 /// public void IniserialPortOption() { //数据读取串口 初始化 //CcgDataReadSerialPort = new SerialPort(ConfigHelper.EcgDataComPort, 1500000, Parity.None, 8, StopBits.One) // string CurrentDevice = ConfigurationManager.AppSettings["CurrentDevice"];//获取设备信息URL string comName= ConfigurationManager.AppSettings[apiOptions.CurrentDevice].Split(',')[3]; int baudRate= int.Parse(ConfigurationManager.AppSettings[apiOptions.CurrentDevice].Split(',')[4]); string parity1= ConfigurationManager.AppSettings[apiOptions.CurrentDevice].Split(',')[5]; Parity parity = Parity.None; if (parity1.ToUpper()== "NONE") parity= Parity.None; if (parity1.ToUpper() == "EVEN") parity = Parity.Even; int databits= int.Parse(ConfigurationManager.AppSettings[apiOptions.CurrentDevice].Split(',')[6]); StopBits SB = StopBits.One; string sb1 = ConfigurationManager.AppSettings[apiOptions.CurrentDevice].Split(',')[7]; if (sb1== "1") SB = StopBits.One; if (sb1.ToUpper() == "NONE") SB = StopBits.None; if (sb1.ToUpper() == "2") SB = StopBits.Two; DataReadSerialPort = new SerialPort(comName, baudRate, parity, databits, SB) { RtsEnable = false, ReadBufferSize = 4096000, ReadTimeout = 3000, ReceivedBytesThreshold = 1 }; SerialTime = DateTime.Now; } /// /// 下发AD启动指令 /// public void writeStartAD_Command() { byte[] EcgStart = { 0xBB, 0x44, 0xAA, 0x55, 0xCC, 0x01, 0x00, 0x00, 0xCB, 0x02 }; if (!DataReadSerialPort.IsOpen) DataReadSerialPort.Open(); DataReadSerialPort.Write(EcgStart, 0, EcgStart.Length); } /// /// 下发AD停止命令 /// public void writeStopAD_Command() { byte[] EcgEnd = { 0xBB, 0x44, 0xAA, 0x55, 0x02, 0x00, 0x00, 0xCC, 0x02 }; if (!DataReadSerialPort.IsOpen) { DataReadSerialPort.Open(); } DataReadSerialPort.Write(EcgEnd, 0, EcgEnd.Length); } /// /// 终止串口数据读取的线程 /// public void StopDataReadThread() { _thSerialPortDataRead.Abort(); } /// /// 开启串口数据读取的线程 /// public void StartDataReadThread() { _thSerialPortDataRead = new Thread(DataRead) { IsBackground = true, Priority = ThreadPriority.Highest }; _thSerialPortDataRead.Start(); } /// /// 读取串口数据 /// private void DataRead() { while (true) { try { if (!DataReadSerialPort.IsOpen) { DataReadSerialPort.Open(); } BufferBytesLength = DataReadSerialPort.BytesToRead; if (BufferBytesLength > 0) { var buffer = new byte[BufferBytesLength]; DataReadSerialPort.Read(buffer, 0, BufferBytesLength); string encodingStr = "UTF-8"; try { encodingStr = ConfigurationManager.AppSettings[apiOptions.CurrentDevice].Split(',')[8]; } catch { } string ReDataStr = Encoding.GetEncoding(encodingStr).GetString(buffer); dataOutPutShow(ReDataStr, null); } } catch { } Thread.Sleep(1000);//睡眠1秒 } } } }