USB条码阅读器在c#

本文关键字:条码阅读器 USB | 更新日期: 2023-09-27 18:18:17

我想从USB扫描器读取条形码。

当使用SerialPort连接时,我得到像héà&"à')&'_hqà&é"'(èq的代码。

是否有一种方法来解码这个值?

        private void Form1_Load(Object sender, EventArgs e)
        {
            _serialPort = new SerialPort("COM1", 19200, Parity.None, 8, StopBits.One);
            _serialPort.Handshake = Handshake.None;
            _serialPort.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);
            _serialPort.ReadTimeout = 500;
            _serialPort.WriteTimeout = 500;
            _serialPort.Open();
        }

/*

*/

        private void btnStart_Click(Object sender, EventArgs e)
        {
            try
            {
                if (!_serialPort.IsOpen)
                    _serialPort.Open();
                _serialPort.Write("SI'r'n");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error opening/writing to serial port :: " + ex.Message, "Error!");
            }
        }
        void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            Thread.Sleep(500);
            String data = _serialPort.ReadLine();
            this.BeginInvoke(new SetTextDeleg(si_DataReceived), new Object[] { data });
        }
        private void si_DataReceived(String data)
        {
            textBox1.Text = data.Trim().ToString();
        }

谢谢

USB条码阅读器在c#

使用SerialPort-Class的成员

_serialPort = new SerialPort("COM1", 19200, Parity.None, 8, StopBits.One);
_serialPort.Handshake = Handshake.None;
_serialPort.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);
_serialPort.ReadTimeout = 500;
_serialPort.WriteTimeout = 500;
// Encoding
_serialPort.Encoding = Encoding.ASCII;
_serialPort.Open();

谢谢你的帮助,我终于发现我的条形码扫描器给我这么奇怪的代码的原因了。事实上,当我打开我的锁帽时,一切都很好。