基恩士读码器HR-101B USB-COM连接读取数据

最近接了一个项目,需要用到手持读码器,项目负责人采购了一个既能文本输出,又能主动读取读码器缓存数据的码枪HR-101B,研究了一下,特来写写。

  1. 首先要安装HR-100 Setup Tool,基恩士官网驱动地址为http://www.barcodereader.com/cn/
  2. 将USB连接电脑,用码枪先扫描基座后面的条码,在HR-100系列用户手册(供应商提供或官网下载),4-3通讯设定—>USB-COM设定  页面,打印出来此页,扫描“USB-COM界面”条码。
  3. 打开HR-100 Setup Tool,在设定—>读码器设定(R)界面,勾选 “USB-COM界面” 及 “硬件触发模式”。在设定—>PC通信设定(T)界面,确认COM端口,设置波特率,数据长度,奇偶性,停止位,连接方法设为自动。在HR-100 Setup Tool主界面点击设定发送。
  4. 最好先找个串口调试助手之类的工具测试下,能否接收到数据。
  5. 代码
    using System;
    using System.IO.Ports;
    
    namespace Glue.Common
    {
        class KeyenceBarcodeReader_UsbCom
        {
            SerialPort keyenceSerialPort = new System.IO.Ports.SerialPort();
    
            private string receiveData = string.Empty;
            public string ReceiveData
            {
                get { return this.receiveData; }
                set { this.receiveData = value; }
            }
    
            /// <summary>
            /// 构造函数
            /// </summary>
            /// <param name="portName"></param>
            public KeyenceBarcodeReader_UsbCom(string portName)
            {
                this.keyenceSerialPort.PortName = portName;
                this.keyenceSerialPort.BaudRate = 115200;
                this.keyenceSerialPort.DataBits = 8;
                this.keyenceSerialPort.Parity = Parity.Even;//奇偶性-偶数
                this.keyenceSerialPort.StopBits = StopBits.One;//停止位 1bit
                this.keyenceSerialPort.DataReceived += new SerialDataReceivedEventHandler(this.DataReceivedHandler);
            }
    
            /// <summary>
            /// 打开端口连接
            /// </summary>
            public void Open()
            {
                this.keyenceSerialPort.Open();
            }
    
            /// <summary>
            /// 关闭端口连接
            /// </summary>
            public void Close()
            {
                this.keyenceSerialPort.Close();
            }
    
    
            private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
            {
                SerialPort sp = (SerialPort)sender;
                this.receiveData = sp.ReadExisting();
            }
        }
    }

     

  6. 客户端调用
    using System;
    using System.Threading;
    using TestKeyenceBarcodeReader;
    
    namespace TestBarcodeReader
    {
        class Program
        {
            static SerialPortHelper portHelper = new SerialPortHelper("COM3");
            static void Main(string[] args)
            {
                portHelper.Open();
                Thread thread = new Thread(Test);
                thread.Start();
            }
    
            static void Test()
            {
                while (true)
                {
                    Thread.Sleep(1000);
                    Console.WriteLine(portHelper.ReceiveData);
                }
            }
        }
    }

    完~

posted @ 2019-06-20 09:44  StackMiao  阅读(1755)  评论(0编辑  收藏  举报