1

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

我收到类似 héà&"à')&'_hqà&é"'(èq使用 SerialPort 连接时的代码。

有没有办法解码这个值?

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

谢谢

4

2 回答 2

1

使用 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();
于 2013-06-13T11:58:57.717 回答
0

谢谢你的帮助,我终于找到了我的条形码扫描仪给我这样奇怪的代码的方式。事实上,当我打开 Lock cap 时,一切正常。它也适用于记事本。

于 2013-06-16T12:19:28.990 回答