本文介绍了Xamarin.Forms:使用 UsbDeviceConnection 库读取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我必须从 UsbDeviceConnection 读取数据,但调用 UsbDeviceConnection.BulkTransfer(......) 总是返回 -1.
I Have to read data from UsbDeviceConnection but the call UsbDeviceConnection.BulkTransfer(......) always returns -1.
当我必须写入数据时,一切正常,但是当我尝试接收时,调用总是返回 -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;
}
}
我希望 byteReaded 的值大于 0,因为我通过putty"发送数据但始终为 -1
I expect the value of byteReaded to be great than 0 because I send data by 'putty' but is always -1
我错过了什么?
推荐答案
找到了这个有效的解决方案 https://github.com/ysykhmd/usb-serial-for-xamarin-android
Found this solution that works https://github.com/ysykhmd/usb-serial-for-xamarin-android
这篇关于Xamarin.Forms:使用 UsbDeviceConnection 库读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!