【问题标题】:How to get signals from a scanner connected over usb如何从通过 USB 连接的扫描仪获取信号
【发布时间】:2015-03-09 11:20:54
【问题描述】:

我有一台 KODAK i2600 扫描仪通过 SX Virtual Link 连接到网络,该网络中的每台电脑都模拟到扫描仪的 USB 连接。我可以像这样轻松找到模拟的usbs pid和vid:

或者这个:

我尝试使用 LibUsbDotNet 检测信号,但在他们的示例中它不起作用。我试过这个例子:

public static UsbDevice MyUsbDevice;

#region SET YOUR USB Vendor and Product ID!

public static UsbDeviceFinder MyUsbFinder = new UsbDeviceFinder(int.Parse("040a", System.Globalization.NumberStyles.HexNumber), int.Parse("601d", System.Globalization.NumberStyles.HexNumber));


#endregion

public static void Main(string[] args)
{
    ErrorCode ec = ErrorCode.None;

    try
    {
        // Find and open the usb device.
        MyUsbDevice = UsbDevice.OpenUsbDevice(MyUsbFinder);

        // If the device is open and ready
        if (MyUsbDevice == null) throw new Exception("Device Not Found.");

        // If this is a "whole" usb device (libusb-win32, linux libusb-1.0)
        // it exposes an IUsbDevice interface. If not (WinUSB) the 
        // 'wholeUsbDevice' variable will be null indicating this is 
        // an interface of a device; it does not require or support 
        // configuration and interface selection.
        IUsbDevice wholeUsbDevice = MyUsbDevice as IUsbDevice;
        if (!ReferenceEquals(wholeUsbDevice, null))
        {
            // This is a "whole" USB device. Before it can be used, 
            // the desired configuration and interface must be selected.

            // Select config #1
            wholeUsbDevice.SetConfiguration(1);

            // Claim interface #0.
            wholeUsbDevice.ClaimInterface(0);
        }

        // open read endpoint 1.
        UsbEndpointReader reader = MyUsbDevice.OpenEndpointReader(ReadEndpointID.Ep01);


        byte[] readBuffer = new byte[1024];
        while (ec == ErrorCode.None)
        {
            int bytesRead;

            // If the device hasn't sent data in the last 5 seconds,
            // a timeout error (ec = IoTimedOut) will occur. 
            ec = reader.Read(readBuffer, 5000, out bytesRead);

            if (bytesRead == 0) throw new Exception(string.Format("{0}:No more bytes!", ec));
            Console.WriteLine("{0} bytes read", bytesRead);

            // Write that output to the console.
            Console.Write(Encoding.Default.GetString(readBuffer, 0, bytesRead));
        }

        Console.WriteLine("\r\nDone!\r\n");
    }
    catch (Exception ex)
    {
        Console.WriteLine();
        Console.WriteLine((ec != ErrorCode.None ? ec + ":" : String.Empty) + ex.Message);
    }
    finally
    {
        if (MyUsbDevice != null)
        {
            if (MyUsbDevice.IsOpen)
            {
                // If this is a "whole" usb device (libusb-win32, linux libusb-1.0)
                // it exposes an IUsbDevice interface. If not (WinUSB) the 
                // 'wholeUsbDevice' variable will be null indicating this is 
                // an interface of a device; it does not require or support 
                // configuration and interface selection.
                IUsbDevice wholeUsbDevice = MyUsbDevice as IUsbDevice;
                if (!ReferenceEquals(wholeUsbDevice, null))
                {
                    // Release interface #0.
                    wholeUsbDevice.ReleaseInterface(0);
                }

                MyUsbDevice.Close();
            }
            MyUsbDevice = null;

            // Free usb resources
            UsbDevice.Exit();

        }

        // Wait for user input..
        Console.ReadKey();
    }
}

在此示例中,当我尝试打开 USB 设备时,结果为 null。所以我试图用这个例子检测来自任何 USB 设备的任何信号:

public static IDeviceNotifier UsbDeviceNotifier = DeviceNotifier.OpenDeviceNotifier();

    private static void Main(string[] args)
    {
        // Hook the device notifier event
        UsbDeviceNotifier.OnDeviceNotify += OnDeviceNotifyEvent;

        // Exit on and key pressed.
        Console.Clear();            
        Console.WriteLine();
        Console.WriteLine("Waiting for system level device events..");
        Console.Write("[Press any key to exit]");

        while (!Console.KeyAvailable)
            Application.DoEvents();

        UsbDeviceNotifier.Enabled = false;  // Disable the device notifier

        // Unhook the device notifier event
        UsbDeviceNotifier.OnDeviceNotify -= OnDeviceNotifyEvent;
    }

    private static void OnDeviceNotifyEvent(object sender, DeviceNotifyEventArgs e)
    {
        // A Device system-level event has occured

        Console.SetCursorPosition(0,Console.CursorTop);

        Console.WriteLine(e.ToString()); // Dump the event info to output.

        Console.WriteLine();
        Console.Write("[Press any key to exit]");
    }`

在这个例子中,我只能检测到connectdisconnect 信号。所以我尝试使用 LibUsbDotNet 连接到 USB 键盘/鼠标端口,但这也不起作用。

看来我severely 不明白需要做什么 在这里。

接下来,我什至尝试使用 SO 中的这个示例检查某个 USB 设备是否已连接:USB Device Connected 即使这样也没有用。

总结一下,我需要这样做:

  1. 连接到扫描仪所连接的 USB 端口(并监听它通过该端口发送的信号)。
  2. 检测来自扫描按钮的信号:
  3. 当检测到来自 2. 的信号时,我会向扫描仪发送一个信号,以使用我自己的配置使用 TWAIN 或 ISIS 库进行扫描。

【问题讨论】:

  • 如果您想使用 LibUsbDotNet 连接到扫描仪,有两种方法:安装驱动程序过滤器或为设备创建驱动程序。如果您没有有关扫描仪信号的文档,请使用 UsbLyzer 嗅探通过 USB 连接的扫描仪。 UsbDeviceNotifier 仅用于检测 USB 设备的插拔。

标签: c# libusbdotnet


【解决方案1】:

我认为您可以从供应商方面获得 kodak perfect page SDK。 Pinvoke 供应商提供的调用。

查看链接中的开发者工具部分 http://www.kodak.com/eknec/PageQuerier.jhtml?pq-path=2714&pq-locale=en_US&_requestid=3863

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-08
    • 1970-01-01
    • 1970-01-01
    • 2015-05-02
    • 1970-01-01
    • 2014-08-27
    • 2013-03-17
    • 1970-01-01
    相关资源
    最近更新 更多