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); } } }