locked
receive the data which is read in the program RRS feed

  • Question

  • i want receive the data which is read in the program below and process on it and its containing byte and make decision on them.

    the code is:

    using System;
    using System.Text;
    using LibUsbDotNet;
    using LibUsbDotNet.LibUsb;
    using LibUsbDotNet.Main;
    using System.Windows;
     
    namespace WpfApplication1
    {


        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {


            private void Init(object sender, RoutedEventArgs e)
            {
                byte[] data = ReadPolling.Read();

            }
        }



        public class ReadPolling
        {
            public static UsbDevice MyUsbDevice;

            #region SET YOUR USB Vendor and Product ID!

            public static UsbDeviceFinder MyUsbFinder = new UsbDeviceFinder(1234, 1);

            #endregion

            public static byte[] Read()
            {
                Error ec = Error.Success;

                using (UsbContext context = new UsbContext())
                {
                    try
                    {
                        // Find and open the usb device.
                        MyUsbDevice = (UsbDevice)context.Find(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.
                        var reader = MyUsbDevice.OpenEndpointReader(ReadEndpointID.Ep01);


                        byte[] readBuffer = new byte[1024];
                        while (ec == Error.Success)
                        {
                            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");
                        return readBuffer;
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine();
                        Console.WriteLine((ec != Error.Success ? 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;
                        }

                    }
                }
                return null;
            }
        }

    }


    Monday, April 8, 2019 5:49 PM

All replies

  • Exactly what kind of a device are you trying to read?  There are many types of USB device, and in many cases libusb is not the right solution.  Do you know the descriptors of your device, so you know what interface number and what endpoint number you need to read?  Do you know how it returns data, and what format?

    The "Read" function above returns a byte array.  You can do whatever you want with it.


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Monday, April 8, 2019 6:35 PM
  • Hello,

    Please refrain from using a title for your post that don't describe what you are asking for. Use a title related to your question instead.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, April 8, 2019 6:40 PM
  • Hi

    Thank you for posting here.

    Since this thread is related to LibUsbDotNet, you could post in the following forum.

    https://forums.obdev.at/viewtopic.php?t=6561

    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    The Visual C# forum discusses and asks questions about the C# programming language, IDE, libraries, samples, and tools.

    Best regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, April 9, 2019 6:47 AM