using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Threading; using System.IO.Ports; namespace KHSCALE_TP { public class SerialBase { protected object m_lock = new object(); protected float m_value = 0; protected Thread m_t1 = null; protected bool m_bThreadRun = true; protected int m_recvCount = 0; protected DateTime m_dtRecv = DateTime.Now; protected string m_portName = "COM1"; protected int m_baudRate = 9600; protected int m_parity = 0; protected int m_dataBits = 8; protected float m_stopBits = 1; public string GetComSetting() { return m_portName + "," + m_baudRate.ToString() + "," + m_parity.ToString() + "," + m_dataBits.ToString() + "," + m_stopBits.ToString(); } // 통신 프레임 사이즈가 고정일 경우 그 사이즈를 지정. 정해진 사이즈가 없으면 -1 리턴하고, 구분자로 프레임 구분하도록 protected virtual int GetFrameSize() { return -1; } // 통신 프레임 간의 구분자가 있을 경우 그 구분자를 지정 protected virtual char GetFrameDelimiter() { return '\n'; } protected virtual bool Parse(List buffRecv) { return false; } public float GetValue() { lock (m_lock) { return m_value; } } protected void SetValue(float val) { lock (m_lock) { m_value = val; m_dtRecv = DateTime.Now; } } public float GetRecvCount() { lock (m_lock) { return m_recvCount; } } public DateTime GetUpdTime() { lock (m_lock) { return m_dtRecv; } } // portName: COM1, COM2, .... // baudRate: 9600, 19200, ... // parity: 0=None, 1=Odd, 2=Even // dataBits: 7, 8, ... // stopBits: 1, 1.5, 2 public void Init(string portName, int baudRate, int parity, int dataBits, float stopBits) { m_portName = portName; m_baudRate = baudRate; m_parity = parity; m_dataBits = dataBits; m_stopBits = stopBits; m_t1 = new Thread(new ThreadStart(SerialRun)); m_t1.Start(); } public void Close() { m_bThreadRun = false; if (m_t1 != null) m_t1.Join(); } private void SerialRun() { m_bThreadRun = true; SerialPort serial = null; while (m_bThreadRun) { serial = new SerialPort(); Parity parity = Parity.None; if (m_parity == 0) parity = Parity.None; if (m_parity == 1) parity = Parity.Odd; if (m_parity == 2) parity = Parity.Even; StopBits stopBits = StopBits.None; if (m_stopBits == 1) stopBits = StopBits.One; if (m_stopBits == 1.5) stopBits = StopBits.OnePointFive; if (m_stopBits == 2) stopBits = StopBits.Two; while (m_bThreadRun) { if (serial.m_IsConn == false) { serial.Init(m_portName, m_baudRate, parity, m_dataBits, stopBits); //Program.m_MainForm.UpdateListBox(serial.m_strPortMsg); } if (serial.m_IsConn) { serial.Read(); if (serial.m_strPortMsg.Length > 0) { //Program.m_MainForm.UpdateListBox(serial.m_strPortMsg); } m_recvCount = serial.m_recvCount; List buff = serial.GetBuffer(false); if (buff.Count == 0) continue; int nFrameSize = GetFrameSize(); if ((nFrameSize > 0 && serial.GetBufferSize() >= nFrameSize) || (buff[buff.Count - 1] == GetFrameDelimiter())) { if (Parse(buff)) { serial.ClearBuffer(); //Program.m_MainForm.UpdateValue(this.GetValue()); } //Program.m_MainForm.UpdateListBox("[" + buff.Count.ToString() + "] " + Encoding.Default.GetString(buff.ToArray())); } } else { System.Threading.Thread.Sleep(10000); } } } serial.Close(); } } }