条码扫描器 - 串行端口

问题描述 投票:0回答:4

因此,我尝试使用我的条形码扫描仪作为“串行”设备,而不是键盘模拟器,但它没有创建 com 端口。我已经扫描了手册中的设置代码,将其设置为串行设备,这似乎正确配置了扫描仪(它停止将扫描的代码发送到文本框扩展编辑器),但由于没有 COM 端口,我无法捕获扫描条形码时的数据......

Windows 在第一次插入时安装了驱动程序,但没有提供磁盘\驱动程序...不知道其他人是否遇到过同样的问题.....

这是我的代码......

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

当我尝试打开端口时,收到错误消息......“端口‘com1’不存在”。

当我创建虚拟端口(使用第 3 方应用程序)时,代码会运行,但我仍然没有从扫描仪获取数据....

c# serial-port barcode-scanner
4个回答
2
投票

我只是新手,我有任务 - 通过串口从条形码扫描仪接收数据...我花了很多时间...我有下一个结果

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

1
投票

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

0
投票

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


0
投票

有时您需要为USB条码扫描仪安装专用的虚拟COM驱动程序。例如,在这个网站(hdwr.pl)上您可以下载它。

© www.soinside.com 2019 - 2024. All rights reserved.