我正在使用Visual Studio17在跨平台Xamarine中开发android主机应用程序,并且正在使用usbserialdriver(cdcacmserialdriver)。在android usb主机应用程序中增加USB缓冲区大小存在一个问题,其中缓冲区的大小增加不超过16KB(16,384字节)。但是我必须将缓冲区的大小增加到360960字节。 我尝试过的代码如下:
public static int DEFAULT_READ_BUFFER_SIZE = 360960;// 16 * 1024;
serialIoManager.DataReceived += (sender, e) => {
RunOnUiThread(() => {
UpdateReceivedData(e.Data);
});
};
void UpdateReceivedData(byte[] data){
var message = HexDump.DumpHexString(data) + "\n\n";
var sd = Android.OS.Environment.ExternalStorageDirectory.Path;
var file = System.IO.Path.Combine(sd, "image.txt");
var file1 = System.IO.Path.Combine(sd, "image1.raw");
using (var stream = new FileStream(file1, FileMode.Create))
{
stream.Write(data, 0, 360960);
}
dumpTextView.Append(message);
scrollView.SmoothScrollTo(0, dumpTextView.Bottom);
}
public override int Read(byte[] dest, int timeoutMillis)
{
if (mEnableAsyncReads)
{
UsbRequest request = new UsbRequest();
try
{
request.Initialize(mConnection, mReadEndpoint);
ByteBuffer buf = ByteBuffer.Wrap(dest);
if (!request.Queue(buf, dest.Length))
{
throw new IOException("Error queueing request.");
}
UsbRequest response = mConnection.RequestWait();
if (response == null)
{
throw new IOException("Null response");
}
int nread = buf.Position();
if (nread > 0)
{
System.Buffer.BlockCopy(buf.ToByteArray(), 0, dest, 0, dest.Length);
Log.Debug(TAG, HexDump.DumpHexString(dest, 0, Math.Min(32, dest.Length)));
return nread;
}
else
{
return 0;
}
}
finally
{
request.Close();
}
}
int numBytesRead;
lock (mReadBufferLock)
{
int readAmt = Math.Min(dest.Length, mReadBuffer.Length);
numBytesRead = mConnection.BulkTransfer(mReadEndpoint, mReadBuffer, 360960, timeoutMillis);
if (numBytesRead < 0)
{
if (timeoutMillis == Integer.MaxValue)
{
return -1;
}
return 0;
}
System.Buffer.BlockCopy(mReadBuffer, 0, dest, 0, numBytesRead);
}
return numBytesRead;
}