Hi I am trying to port reading USB data that works in Android but not in Xamarin version of Android.
Basically I am sending various proprietary commands that gives me corresponding data related to it.
I have tried various methods of extracting data from byte buffer but I get the exact same response or 0's in byte buffer depending on how I extract the data; Marshal.Copy() respectively ByteBuffer.Get()
Any help would be appreciated. Thanks in advance!
Android (Works):
public byte[] read(UsbRequest outRequest, int expected) { if (cUSB == null) { Log.e(LOG_MAP.get(LOG_RX), "No connection available."); } ByteBuffer buffer = ByteBuffer.allocate(expected); if (outRequest.equals(cUSB.requestWait())) { UsbRequest inRequest = new UsbRequest(); inRequest.initialize(cUSB, eUSBIn); if (inRequest.queue(buffer)) { cUSB.requestWait(); return buffer.array(); } } return null; } public UsbRequest write(byte[] command) { if (cUSB == null) { Log.e(LOG_MAP.get(LOG_TX), "No connection available"); } ByteBuffer buffer = ByteBuffer.allocate(1); UsbRequest outRequest = new UsbRequest(); outRequest.initialize(cUSB, eUSBOut); buffer.put(command); Log.i(LOG_MAP.get(LOG_TX), "Queue: "+ outRequest.queue(buffer, 1)); return outRequest; }
Xamarin (Doesn't work):
private bool Read(UsbRequest outRequest, int responseLength) { Log.Debug(TAG, "Reading from device"); if (devConnection == null) { Log.Error(TAG, "No connection available."); return false; } ByteBuffer bb = ByteBuffer.AllocateDirect(responseLength); //byte[] response = new byte[responseLength]; byte[] response; //ByteBuffer bb = ByteBuffer.Wrap(response); try { if (outRequest.Equals(devConnection.RequestWait(USBProtocol.TimeoutReadSwHw))) { UsbRequest inRequest = new UsbRequest(); inRequest.Initialize(devConnection, endpointIn); if (inRequest.Queue(bb)) { Log.Verbose(TAG, "pos:" + bb.Position() + "\tlimit:" + bb.Limit() + "\tremaining:" + bb.Remaining()); devConnection.RequestWait(USBProtocol.TimeoutReadSwHw); Log.Verbose(TAG, "offset: " + bb.ArrayOffset() + "\tLimit: " + bb.Limit() + "\tremaing: " + bb.Remaining()); response = new byte[bb.ArrayOffset() - 1]; Marshal.Copy(bb.GetDirectBufferAddress(), response, 0, response.Length); // bb.Rewind(); // bb.Get(response, 0, responseLength); Log.Verbose(TAG, "response: " + BitConverter.ToString(response)); } } } catch (Exception e) { Log.Error(TAG, e.ToString()); // Event error return false; } // Send data to listners //OnUSBDataEvent(args); return true; } private UsbRequest Write(byte[] cmd) { Log.Debug(TAG, "Sending " + BitConverter.ToString(cmd)); if (devConnection== null) { Log.Error(TAG, "No connection available"); return null; } if (cmd.Length < 1) { Log.Error(TAG, "No command given"); return null; } ByteBuffer buffer = ByteBuffer.Allocate(cmd.Length); buffer.Put(cmd); UsbRequest outRequest = new UsbRequest(); outRequest.Initialize(devConnection, endpointOut); bool queued = outRequest.Queue(buffer); Log.Debug(TAG, "Queue: " + queued); return outRequest; if (!outRequest.Equals(devConnection.RequestWait())) { Log.Error(TAG, "Failed to send: " + BitConverter.ToString(cmd)); return null; } Log.Info(TAG, "Command " + BitConverter.ToString(cmd) + " sent successfully"); //return cmd.Length; }