From 048e3402803d7e1f520868a440d7663f01497fc6 Mon Sep 17 00:00:00 2001 From: xxe Date: Tue, 13 Sep 2022 10:43:11 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=B2=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HKCardOUT/COM/COMHelper.cs | 34 ++++++++++++++++++++++++++++++++++ HKCardOUT/HKCardOUT.csproj | 1 + 2 files changed, 35 insertions(+) create mode 100644 HKCardOUT/COM/COMHelper.cs 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 @@ +