MarshallTrufant
MarshallTrufant

Reputation: 45

Barcode Scanner Only Reading One Character

I am working on a small winform application that reads input from a barcode scanner on a virtual com port and writes the data back to a text box on my winform. I am new to C# so have been struggling through. My current code is below and adapted from 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();
        }
    }
}

The full string I am trying to scan is "3894038" but I am only able to get the textbox to display one character at a time in the text box. I suspect it has something to do with my .ReadExisting command, but I am a bit perplexed on how to proceed. Is there something wrong with my code?

Thanks for the help in advance.

Marshall

Upvotes: 1

Views: 2265

Answers (1)

Jason Watkins
Jason Watkins

Reputation: 3785

There are quite a few issues with your code. The issue you described is caused by the fact that you are assigning the value of ReadExisting to the textbox rather than appending it. I've fixed that and several other issues below.

Notes:

  • Use AppendText instead of assigning to add new data to the end of the text box's text
  • There is virtually never a good reason to call Application.DoEvents
  • You open the serial port in an inconsistent way in two different places
  • You already have sp in defined at the class level; there's no need to hide it with a cast of the event sender.

Fixed code:

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

Upvotes: 2

Related Questions