Barcode Scanner - Serial Port

时间:2016-07-28 20:26:06

标签: c# serial-port barcode-scanner

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....

3 个答案:

答案 0 :(得分:0)

您可以在下面的代码中使用。我可以打开我在特定端口配置的COM。 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();
    }

答案 1 :(得分:0)

我正在编写自己的条形码脚本。我的扫描仪默认为即插即用USB-HID ...人机接口设备...而不是USB-COMn端口。我必须扫描条形码才能将其切换到串口模式。您可以在“设备管理器”树中观察转换过程...作为"端口"分支萌芽,包含条形码扫描仪的详细信息。我的COM3。

答案 2 :(得分:0)

我只是新手,正在执行任务-通过串行端口从BarCode扫描器接收数据...我花了很多时间...而我得到下一个结果

<link href="https://fonts.googleapis.com/css?family=Montserrat|Ubuntu" rel="stylesheet">

<h1>This is a title, styled with the h1 tag</h1>

<p>This is a paragraph, not styled with the h1 tag</p>