条形码扫描仪只读一个字符

时间:2016-03-24 18:41:52

标签: c# winforms barcode-scanner virtual-serial-port

我正在开发一个小型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命令有关,但我对如何继续有点困惑。我的代码有问题吗?

感谢您的帮助。

马歇尔

1 个答案:

答案 0 :(得分:2)

您的代码存在很多问题。您描述的问题是由于您将ReadExisting的值分配给文本框而不是附加它。我已经解决了以下几个问题。

注意:

  • 使用AppendText而不是指定将新数据添加到文本框的文本末尾
  • 几乎没有理由打电话给Application.DoEvents
  • 您在两个不同的地方以不一致的方式打开串口
  • 您已经在班级定义了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();
        }
    }
}