Xamarin.Forms: Read Data using UsbDeviceConnection Library

和自甴很熟 提交于 2021-02-10 14:31:45

问题


I Have to read data from UsbDeviceConnection but the call UsbDeviceConnection.BulkTransfer(......) always returns -1.

when I have to write data, all works fine, but when I try to receive, the call always returns -1 (receive failed).

 [Obsolete]
        public string ConnectAndRead()
        {


            // Get a usbManager that can access all of the devices
            var usbManager = (UsbManager)Forms.Context.GetSystemService(Context.UsbService);

            while(usbManager.DeviceList.Count == 0)
            {
                Debug.WriteLine($"Nessun dispositivo connesso alle {DateTime.Now}");

                Thread.Sleep(5000);
            }
            var deviceConnected = usbManager.DeviceList.FirstOrDefault();

            if (deviceConnected.Value == null)
                //throw new Exception("Dispositivo non trovato, provare a configurarlo in Impostazioni");
                Debug.WriteLine( "Dispositivo non trovato");

           //string usbPort = deviceConnected.Key;
            UsbDevice usbDevice = deviceConnected.Value;
            if (!usbManager.HasPermission(usbDevice))
            {
                try
                {
                    PendingIntent pi = PendingIntent.GetBroadcast(Forms.Context, 0, new Intent(ACTION_USB_PERMISSION), 0);
                    usbManager.RequestPermission(usbDevice, pi);
                    //throw new Exception("Rilanciare la stampa");

                }
                catch (Exception ex)
                {
                    //throw new Exception(ex.Message);
                    return ex.Message;
                }
                Debug.WriteLine("Non avevo i permessi");
            }
            try
            {
                UsbDeviceConnection deviceConnection = usbManager.OpenDevice(usbDevice);

                // Get the usbInterface for the device.  It and usbEndpoint implement IDisposable, so wrap in a using
                // You may want to loop through the interfaces to find the one you want (instead of 0)
                using (var usbInterface = usbDevice.GetInterface(0))
                {
                    //var interfaceClass = usbInterface.InterfaceClass;
                    // Get the endpoint, again implementing IDisposable, and again the index you need
                    using (var usbEndpoint = usbInterface.GetEndpoint(2))
                    {

                        byte[] encodingSetting =
                            //new byte[] { (byte)0x80, 0x25, 0x00, 0x00, 0x00, 0x00, 0x08 }; //baudrate 9600
                            new byte[] { (byte)0x00, 0x4b, 0x00, 0x00, 0x00, 0x00, 0x08 };   //baudrate 19200
                        // Make request or whatever you need to do
                        deviceConnection.ControlTransfer(
                            UsbAddressing.Out,
                            0x20,   //SET_LINE_CODING
                            0,      //value
                            0,      //index
                            encodingSetting,  //buffer
                            7,      //length
                            0);     //timeout


                        byte[] buffer = new byte[4096];
                        StringBuilder str = new StringBuilder();

                        while (true)
                        {
                            //int byteReaded = deviceConnection.BulkTransfer(usbEndpoint, buffer, buffer.Length, 5000);
                            int byteReaded = deviceConnection.BulkTransfer(usbEndpoint, buffer, 1, 5000);
                            if (byteReaded > 0)
                            {
                                foreach (byte b in buffer)
                                {
                                    str.Append((char)b);
                                }

                                return str.ToString();
                            }


                        }

                    }
                }
            }catch(Exception ex)
            {
                return ex.Message;
            }




        }

I expect the value of byteReaded to be great than 0 because I send data by 'putty' but is always -1

what am I missing?


回答1:


Found this solution that works https://github.com/ysykhmd/usb-serial-for-xamarin-android



来源:https://stackoverflow.com/questions/57772270/xamarin-forms-read-data-using-usbdeviceconnection-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!