条码扫描器只能读取一个字符

Barcode Scanner Only Reading One Character

我正在开发一个小型 winform 应用程序,该应用程序从虚拟 com 端口上的条形码扫描仪读取输入,并将数据写回我的 winform 上的文本框。我是 C# 的新手,所以一直在苦苦挣扎。我当前的代码如下,改编自 here

namespace Barcode_Scanner
{
    public partial class Form1 : Form
    {
        SerialPort sp;
        

       public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string[] ports = SerialPort.GetPortNames();
            comboBox1.DataSource = ports;
            Application.DoEvents();
            

          
            
        }

        private void btn_getComData_Click(object sender, EventArgs e)
        {
            try
            {
                if (!sp.IsOpen)
                    sp.Open();
                sp.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);
                
            }
            catch (Exception ex)
            {
                MessageBox.Show("There was a problem with the Serial Port: " + ex.Message, "Error!");
            }
        }
        void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            sp = (SerialPort)sender;
            string data = sp.ReadExisting(); 
            txt_comData.Text = data;           
            Application.DoEvents();
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            // Makes sure serial port is open before trying to write
            string portname = comboBox1.SelectedItem.ToString();
            sp = new SerialPort(portname, 9600, Parity.None, 8, StopBits.One);
            sp.Handshake = Handshake.None;
            sp.Open();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            sp.Close();
        }
    }
}

我要扫描的完整字符串是“3894038”,但我只能让文本框在文本框中一次显示一个字符。我怀疑这与我的 .ReadExisting 命令有关,但我对如何进行感到有点困惑。我的代码有问题吗?

提前感谢您的帮助。

马歇尔

您的代码存在不少问题。您描述的问题是由于您将 ReadExisting 的值分配给文本框而不是附加它而引起的。我已经解决了这个问题以及下面的其他几个问题。

备注:

  • 使用AppendText而不是赋值来将新数据添加到文本框文本的末尾
  • 几乎没有理由调用 Application.DoEvents
  • 你在两个不同的地方打开串口的方式不一致
  • 您已经在 class 级别定义了 sp;无需通过事件发送者的转换来隐藏它。

固定码:

namespace Barcode_Scanner
{
    public partial class Form1 : Form
    {
        SerialPort sp;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string[] ports = SerialPort.GetPortNames();
            comboBox1.DataSource = ports;
        }

        private void btn_getComData_Click(object sender, EventArgs e)
        {
            try
            {
                if (!sp.IsOpen)
                {
                    button1_Click(null, EventArgs.Empty);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("There was a problem with the Serial Port: " + ex.Message, "Error!");
            }
        }

        void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            string data = sp.ReadExisting();
            txt_comData.Appendtext(data);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Makes sure serial port is open before trying to write
            string portname = comboBox1.SelectedItem.ToString();
            sp = new SerialPort(portname, 9600, Parity.None, 8, StopBits.One);
            sp.Handshake = Handshake.None;
            sp.Open();
            sp.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            sp.Close();
        }
    }
}