using SA_LTT.Base; using System; using System.IO.Ports; using System.Linq; using System.Threading; namespace SA_LTT.Module { public class ModuleBase : XmlManager { public string filePath = @"C:\Equipment\"; public string fileName = "ModuleBase.xml"; protected SerialPort serialPort; private XmlManager _xmlManager; private object _thisLock; private bool _lockCheck; private string _receivedData; private string _portName; private int _baudRate; private int _dataBits; private Parity _parity; private StopBits _stopBits; private Handshake _handshake; private int _readTimeout; private int _writeTimeout; private double _receiveWaitTime; private string _terminator; public string PortName { get { return _portName; } set { _portName = value; serialPort.PortName = _portName; } } public int BaudRate { get { return _baudRate; } set { _baudRate = value; serialPort.BaudRate = _baudRate; } } public int DataBits { get { return _dataBits; } set { _dataBits = value; serialPort.DataBits = _dataBits; } } public Parity Parity { get { return _parity; } set { _parity = value; serialPort.Parity = _parity; } } public StopBits StopBits { get { return _stopBits; } set { _stopBits = value; serialPort.StopBits = _stopBits; } } public Handshake Handshake { get { return _handshake; } set { _handshake = value; serialPort.Handshake = _handshake; } } public int ReadTimeout { get { return _readTimeout; } set { _readTimeout = value; serialPort.ReadTimeout = _readTimeout; } } public int WriteTimeout { get { return _writeTimeout; } set { _writeTimeout = value; serialPort.WriteTimeout = _writeTimeout; } } public double ReceiveWaitTime { get { return _receiveWaitTime; } set { _receiveWaitTime = value; } } public string Terminator { get { return _terminator; } set { _terminator = value; } } public ModuleBase(string filePath = @"C:\Equipment\", string fileName = "ModuleBase.xml") { this.filePath = filePath; this.fileName = fileName; _thisLock = new object(); serialPort = new SerialPort(); PortName = "COM1"; BaudRate = 9600; DataBits = 8; Parity = Parity.None; StopBits = StopBits.One; Handshake = Handshake.None; ReadTimeout = 500; WriteTimeout = 500; } public static string[] GetExistPortNames() { return SerialPort.GetPortNames(); } public bool Open() { try { serialPort.Open(); return true; } catch (Exception e) { EquipmentLogManager.Instance.WriteExceptionLog(e.StackTrace); return false; } } public bool Close() { try { serialPort.Close(); return true; } catch (Exception e) { EquipmentLogManager.Instance.WriteExceptionLog(e.StackTrace); return false; } } protected string WriteRead(string command) { string receivedData = string.Empty; lock (_thisLock) { while (_lockCheck) Monitor.Wait(_thisLock); try { _lockCheck = true; _receivedData = string.Empty; if (serialPort.BytesToRead > 0) serialPort.ReadExisting(); serialPort.BaseStream.Flush(); serialPort.Write(command); DateTime check = DateTime.Now; while (true) { if ((DateTime.Now - check).TotalMilliseconds > ReceiveWaitTime) { check = DateTime.Now; break; } } while (true) { if (serialPort.BytesToRead > 0) { receivedData = serialPort.ReadExisting(); if (CheckTerminator(receivedData)) { if (Terminator == null) { if (serialPort.BytesToRead > 0) continue; } receivedData = _receivedData; break; } } if ((DateTime.Now - check).TotalMilliseconds > serialPort.ReadTimeout) { throw new TimeoutException($"{serialPort.PortName} Port Receieve Timeout"); } } return receivedData; } catch (Exception e) { EquipmentLogManager.Instance.WriteExceptionLog(e.StackTrace + "\r\n" + e.Message); return receivedData; } finally { _lockCheck = false; Monitor.Pulse(_thisLock); } } } protected void Write(string command) { lock (_thisLock) { while (_lockCheck) Monitor.Wait(_thisLock); try { _lockCheck = true; serialPort.Write(command); } catch (Exception e) { EquipmentLogManager.Instance.WriteExceptionLog(e.StackTrace); } finally { _lockCheck = false; Monitor.Pulse(_thisLock); } } } protected string WriteRead(byte[] command) { string receivedData = string.Empty; lock (_thisLock) { while (_lockCheck) Monitor.Wait(_thisLock); try { _lockCheck = true; _receivedData = string.Empty; if (serialPort.BytesToRead > 0) serialPort.ReadExisting(); serialPort.BaseStream.Flush(); serialPort.Write(command, 0, command.Length); DateTime check = DateTime.Now; while (true) { if ((DateTime.Now - check).TotalMilliseconds > ReceiveWaitTime) { check = DateTime.Now; break; } } while (true) { if (serialPort.BytesToRead > 0) { receivedData = serialPort.ReadExisting(); if (CheckTerminator(receivedData)) { double aa = (DateTime.Now - check).TotalMilliseconds; receivedData = _receivedData; break; } } if ((DateTime.Now - check).TotalMilliseconds > serialPort.ReadTimeout) { throw new TimeoutException($"{serialPort.PortName} Port Receieve Timeout"); } } return receivedData; } catch (Exception e) { EquipmentLogManager.Instance.WriteExceptionLog(e.StackTrace); return receivedData; } finally { _lockCheck = false; Monitor.Pulse(_thisLock); } } } protected void Write(byte[] command) { lock (_thisLock) { while (_lockCheck) Monitor.Wait(_thisLock); try { _lockCheck = true; serialPort.Write(command, 0, command.Length); } catch (Exception e) { EquipmentLogManager.Instance.WriteExceptionLog(e.StackTrace); } finally { _lockCheck = false; Monitor.Pulse(_thisLock); } } } protected string Read(int timeout) { string receivedData = string.Empty; DateTime check = DateTime.Now; lock (_thisLock) { while (_lockCheck) Monitor.Wait(_thisLock); try { _lockCheck = true; while (true) { if (serialPort.BytesToRead > 0) { receivedData = serialPort.ReadExisting(); if (CheckTerminator(receivedData)) { receivedData = _receivedData; break; } } if ((DateTime.Now - check).TotalMilliseconds > timeout) { throw new TimeoutException($"{serialPort.PortName} Port Receieve Timeout"); } } } catch (Exception e) { EquipmentLogManager.Instance.WriteExceptionLog(e.StackTrace); return receivedData; } finally { _lockCheck = false; Monitor.Pulse(_thisLock); } } return receivedData; } private bool CheckTerminator(string receivedData) { if (Terminator == null) { _receivedData += receivedData; return true; } else { if (receivedData.Contains(Terminator)) { int terminatorLocation = receivedData.IndexOf(Terminator); if (receivedData.Length == terminatorLocation + Terminator.Length) { _receivedData += receivedData; } else { _receivedData += receivedData.Remove(terminatorLocation + Terminator.Length); } return true; } else { _receivedData += receivedData; return false; } } } } }