|
12345678910111213141516171819202122232425262728293031323334 |
- using HKCardOUT.Helper;
- using System;
- using System.Collections.Generic;
- using System.IO.Ports;
- using System.Text;
-
- namespace COM
- {
- public class COMHelper : Singleton<COMHelper>
- {
- 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);
- }
- }
- }
|