应用SerialPort实现串口扫码枪ST2200操作

引用串口类:System.IO.Ports.SerialPort

直接贴代码:

  /// 
    /// 串口 扫描枪
    /// 
    public class ScanProvider
    {
        private SerialPort _serialPort;

        public ScanProvider(string portName, int baudRate)
        {
            _serialPort = new SerialPort();
            this.RegisterSerialPort(portName, baudRate);
            _serialPort.DataReceived +=_serialPort_DataReceived;
        }

        #region Private Methods

        /// 
        /// 注册串口
        /// 
        /// 串口名
        /// 波特率
        private void RegisterSerialPort(string portName, int baudRate)
        {
            // 串口名
            _serialPort.PortName = portName;
            // 波特率
            _serialPort.BaudRate = baudRate;
            // 数据位
            _serialPort.DataBits = 8;
            // 停止位
            _serialPort.StopBits = System.IO.Ports.StopBits.One;
            // 无奇偶校验位
            _serialPort.Parity = System.IO.Ports.Parity.None;
        }

        #endregion

        #region Public 

        /// 
        /// 是否处于打开状态
        /// 
        public bool IsOpen
        {
            get
            {
                return _serialPort != null && _serialPort.IsOpen;
            }
        }

        /// 
        /// 打开串口
        /// 
        /// 
        public bool Open()
        {
            if (_serialPort == null)
                return this.IsOpen;

            if (_serialPort.IsOpen)
                this.Close();

            _serialPort.Open();

            return this.IsOpen;
        }

        /// 
        /// 关闭串口
        /// 
        public void Close()
        {
            if (this.IsOpen)
                _serialPort.Close();
        }

        /// 
        /// 向串口内写入
        /// 
        /// 写入数据
        /// 偏移量
        /// 写入数量
        public void Write(byte[] send, int offSet, int count)
        {
            if (this.IsOpen)
            {
                _serialPort.Write(send, offSet, count);
            }
        }

        public void Dispose()
        {
            if (this._serialPort == null)
                return;
            if (this._serialPort.IsOpen)
                this.Close();
            this._serialPort.Dispose();
            this._serialPort = null;
        }

        #endregion

        void _serialPort_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            // 等待100ms,防止读取不全的情况
            Thread.Sleep(100);
            byte[] m_recvBytes = new byte[_serialPort.BytesToRead];//定义缓冲区大小
            int result = _serialPort.Read(m_recvBytes, 0, m_recvBytes.Length);//从串口读取数据
            if (result <= 0)
                return;
            string strResult = Encoding.ASCII.GetString(m_recvBytes, 0, m_recvBytes.Length);//对数据进行转换
            _serialPort.DiscardInBuffer();

            if (this.DataReceived != null)
                this.DataReceived(this, new SerialSortEventArgs() { Code = strResult });
        }

        public event EventHandler DataReceived;

        #region Static

        /// 
        /// 获取可用串口名称
        /// 
        /// 
        public static string[] GetComNames()
        {
            string[] names = null;
            try
            {
                names = SerialPort.GetPortNames();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return names;
        }

        #endregion
    }

    public class SerialSortEventArgs
    {
        public string Code { get; set; }
    }

应用:

    public partial class MainWindow : Window
    {
        ScanProvider _scanner;

        public MainWindow()
        {
            InitializeComponent();
            // 打开串口
            _scanner = new ScanProvider("COM1", 115200);
            // 打开串口
            if (_scanner.Open())
                //关联事件处理程序
                _scanner.DataReceived += _scanner_DataReceived;
        }

        void _scanner_DataReceived(object sender, SerialSortEventArgs e)
        {
            this.Dispatcher.BeginInvoke(new Action((args) =>
                {
                    this.txtIDCard.Text = args;
                }), e.Code);
        }
    }

其中波特率根据具体的扫码枪设定。

我的扫码枪型号:ScanHome ST2200 。



你可能感兴趣的:(C#)