User45362
User45362

Reputation: 31

Barcode Scanner - Serial Port

So, I'm trying to use my Barcode Scanner as a 'Serial' device as opposed to a Keyboard emulator but it is not creating the com port. I have scanned the set-up codes from the manual that set it as a Serial device, that seems to configure the scanner correctly (it stops sending scanned codes to text-box\text editor) but because there is no COM port, I cannot capture the data when I scan a barcode......

Windows installed the driver when it was first plugged in, there wasn't a disk\driver supplied... wondered if anyone else has experienced the same issue.....

Here is my code....

class Program
{
    // Create the serial port with basic settings
    private SerialPort port = new SerialPort("com1", 9600, Parity.None, 8, StopBits.One);

    [STAThread]
    static void Main(string[] args)
    {
        new Program();
    }

    private Program()
    {

        string[] ports = System.IO.Ports.SerialPort.GetPortNames();

        Console.WriteLine("Incoming Data:");

        // Attach a method to be called when there
        // is data waiting in the port's buffer
        port.DataReceived += new
          SerialDataReceivedEventHandler(port_DataReceived);

        // Begin communications
        port.Open();

        // Enter an application loop to keep this thread alive
        Application.Run();
    }

    private void port_DataReceived(object sender,
      SerialDataReceivedEventArgs e)
    {
        // Show all the incoming data in the port's buffer
        Console.WriteLine(port.ReadExisting());
    }
}

I get the error message..... 'The port 'com1' does not exist'..... when I try to open the Port.

When I create a virtual Port (using 3rd party app) the code runs BUT I still don't get the data from the Scanner....

Upvotes: 3

Views: 15649

Answers (4)

Andrew
Andrew

Reputation: 147

Sometimes you need to install dedicated virtual COM Driver for USB barcode scanner. For example on this website (hdwr.pl) you can download it.

Upvotes: 0

OLegaFrEND
OLegaFrEND

Reputation: 21

I just newbie, and I was having task - recieve data from BarCode scaner by serial port... I spent a lot of time... and I have next result

using System.IO.Ports;
using System.Timers;

namespace BarCode_manager
{
    public partial class MainWindow : Window
    {

        private static SerialPort currentPort = new SerialPort();
        private static System.Timers.Timer aTimer;

        private delegate void updateDelegate(string txt);

        public MainWindow()
        {
            InitializeComponent();
            currentPort.PortName = "COM6";
            currentPort.BaudRate = 9600;
            currentPort.ReadTimeout = 1000;

            aTimer = new System.Timers.Timer(1000);
            aTimer.Elapsed += OnTimedEvent;
            aTimer.AutoReset = true;
            aTimer.Enabled = true;
        }

        private void OnTimedEvent(object sender, ElapsedEventArgs e)
        {
           if (!currentPort.IsOpen)
            {
                currentPort.Open();
                System.Threading.Thread.Sleep(100); /// for recieve all data from scaner to buffer
                currentPort.DiscardInBuffer();      /// clear buffer          
            }
            try
            {
                string strFromPort = currentPort.ReadExisting();
                lblPortData.Dispatcher.BeginInvoke(new updateDelegate(updateTextBox), strFromPort);
            }
            catch { }
        }

        private void updateTextBox(string txt)
        {
            if (txt.Length != 0)
            {
                aTimer.Stop();
                aTimer.Dispose();
                txtReceive.Text = Convert.ToString(aTimer.Enabled);
                currentPort.Close();
            }
            lblPortData.Text = txt;
            lblCount.Content = txt.Length;
            txtReceive.Text = Convert.ToString(aTimer.Enabled);
        }          

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (currentPort.IsOpen)
                currentPort.Close();
        }
    }
}

Upvotes: 2

Joe
Joe

Reputation: 1

I'm in the process of writing my own barcode scripts. My scanner defaults to being a plug-n-play USB-HID...Human Interface Device...as opposed to being a USB-COMn port. I have to scan a barcode to switch it over to serial port mode. You can watch the transformation process in the Device Manager tree...as a "Ports" branch sprouts out, containing your barcode scanner's details. Mine's COM3.

Upvotes: 0

Ramesh Periyasamy
Ramesh Periyasamy

Reputation: 11

you may used below code. I can able to open the COM which I configured in specific port. SerialPort _serialPort;

    // delegate is used to write to a UI control from a non-UI thread
    private delegate void SetTextDeleg(string text);

    private void Form1_Load(object sender, EventArgs e)
    {
        // all of the options for a serial device
        // can be sent through the constructor of the SerialPort class
        // PortName = "COM1", Baud Rate = 19200, Parity = None, 
        // Data Bits = 8, Stop Bits = One, Handshake = None
        //_serialPort = new SerialPort("COM8", 19200, Parity.None, 8, StopBits.One);
        _serialPort = new SerialPort("COM8", 19200, Parity.None, 8, StopBits.One);
        _serialPort.Handshake = Handshake.None;
        _serialPort.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);
        _serialPort.ReadTimeout = 500;
        _serialPort.WriteTimeout = 500;
        _serialPort.Open();
    }

Upvotes: 1

Related Questions