diff --git a/HKCardOUT/COM/COMHelper.cs b/HKCardOUT/COM/COMHelper.cs new file mode 100644 index 0000000..420166f --- /dev/null +++ b/HKCardOUT/COM/COMHelper.cs @@ -0,0 +1,34 @@ +using HKCardOUT.Helper; +using System; +using System.Collections.Generic; +using System.IO.Ports; +using System.Text; + +namespace COM +{ + public class COMHelper : Singleton + { + public SerialPort serialPort; + public void OpenCOM() + { + serialPort = new SerialPort("COM3", 9600, Parity.None, 8, StopBits.One); + //数据接受 + serialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceived); + serialPort.ReceivedBytesThreshold = 1; + serialPort.RtsEnable = true; + try + { + if (serialPort.IsOpen) return; + serialPort.Open(); + } + catch { } + } + /// 数据接收事件 + private void DataReceived(object sender, SerialDataReceivedEventArgs e) + { + byte[] readBuffer = new byte[serialPort.ReadBufferSize]; + serialPort.Read(readBuffer, 0, readBuffer.Length); + string str = Encoding.Default.GetString(readBuffer); + } + } +} diff --git a/HKCardOUT/HKCardOUT.csproj b/HKCardOUT/HKCardOUT.csproj index e196092..716ea4d 100644 --- a/HKCardOUT/HKCardOUT.csproj +++ b/HKCardOUT/HKCardOUT.csproj @@ -31,6 +31,7 @@ +