Skip to content

Commit 606abfe

Browse files
authored
Merge pull request #51 from DiebBlue/main
Thank you for the PR
2 parents 17f02e3 + 0bd2327 commit 606abfe

6 files changed

Lines changed: 24 additions & 10 deletions

File tree

UsbSerialForAndroid/driver/CdcAcmSerialDriver.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class CdcAcmSerialPort : CommonUsbSerialPort
6868
private static int SET_CONTROL_LINE_STATE = 0x22;
6969
private static int SEND_BREAK = 0x23;
7070

71-
private IUsbSerialDriver Driver;
71+
private new readonly IUsbSerialDriver Driver;
7272

7373
//public CdcAcmSerialPort(UsbDevice device, int portNumber) : base(device, portNumber)
7474
//{

UsbSerialForAndroid/driver/Ch34xSerialDriver.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public class Ch340SerialPort : CommonUsbSerialPort
5959
private UsbEndpoint mReadEndpoint;
6060
private UsbEndpoint mWriteEndpoint;
6161

62-
private IUsbSerialDriver Driver;
62+
private new readonly IUsbSerialDriver Driver;
6363
private string TAG => (Driver as Ch34xSerialDriver)?.TAG;
6464

6565
public Ch340SerialPort(UsbDevice device, int portNumber, IUsbSerialDriver driver) : base(device, portNumber)

UsbSerialForAndroid/driver/Cp21xxSerialDriver.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public class Cp21xxSerialPort : CommonUsbSerialPort
8686
private UsbEndpoint mReadEndpoint;
8787
private UsbEndpoint mWriteEndpoint;
8888

89-
private new IUsbSerialDriver Driver;
89+
private readonly new IUsbSerialDriver Driver;
9090
private string TAG => (Driver as Cp21xxSerialDriver)?.TAG;
9191

9292

UsbSerialForAndroid/driver/FtdiSerialDriver.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ private class FtdiSerialPort : CommonUsbSerialPort
103103
private Boolean rts = false;
104104
private int breakConfig = 0;
105105

106-
private IUsbSerialDriver Driver;
106+
private new readonly IUsbSerialDriver Driver;
107107

108108

109109
private String TAG = typeof (FtdiSerialDriver).Name;

UsbSerialForAndroid/driver/ProlificSerialDriver.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ protected enum DeviceType { DEVICE_TYPE_01, DEVICE_TYPE_T, DEVICE_TYPE_HX, DEVIC
132132
Boolean mStopReadStatusThread = false;
133133
private IOException mReadStatusException = null;
134134

135-
private IUsbSerialDriver Driver;
135+
private new readonly IUsbSerialDriver Driver;
136+
136137

137138
private string TAG => (Driver as ProlificSerialDriver)?.TAG;
138139

UsbSerialForAndroid/driver/STM32SerialDriver.cs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public class STM32SerialPort : CommonUsbSerialPort
5656
bool mRts = false;
5757
bool mDtr = false;
5858

59-
IUsbSerialDriver Driver;
59+
private new readonly IUsbSerialDriver Driver;
6060

6161
const int USB_WRITE_TIMEOUT_MILLIS = 5000;
6262

@@ -141,8 +141,15 @@ public override int Read(byte[] dest, int timeoutMillis)
141141
try
142142
{
143143
request.Initialize(mConnection, mReadEndpoint);
144-
ByteBuffer buf = ByteBuffer.Wrap(dest);
145-
if (!request.Queue(buf, dest.Length))
144+
145+
// wrap not work here
146+
// byte[] is a primitive C# value type and not a Java.Lang.Object reference type
147+
// when you do ByteBuffer.Wrap (dest), Java has no reference to the actual C# byte[], Java will instead make a copy of the bytes.
148+
// ByteBuffer buf = ByteBuffer.Wrap(dest);
149+
150+
ByteBuffer buf = ByteBuffer.AllocateDirect(dest.Length);
151+
152+
if (!request.Queue(buf, buf.Limit()))
146153
throw new IOException("Error queuing request");
147154

148155
UsbRequest response = mConnection.RequestWait();
@@ -151,7 +158,13 @@ public override int Read(byte[] dest, int timeoutMillis)
151158

152159
int nread = buf.Position();
153160
if (nread > 0)
154-
return nread;
161+
{
162+
// set back buffer position to 0
163+
buf.Rewind();
164+
// copy the bytes back
165+
buf.Get(dest, 0, nread);
166+
return nread;
167+
}
155168

156169
return 0;
157170
}
@@ -166,7 +179,7 @@ public override int Read(byte[] dest, int timeoutMillis)
166179
{
167180
int readAmt = Math.Min(dest.Length, mReadBuffer.Length);
168181
numBytesRead = mConnection.BulkTransfer(mReadEndpoint, mReadBuffer, readAmt, timeoutMillis);
169-
if(numBytesRead < 0)
182+
if(numBytesRead <= 0)
170183
{
171184
// This sucks: we get -1 on timeout, not 0 as preferred.
172185
// We *should* use UsbRequest, except it has a bug/api oversight

0 commit comments

Comments
 (0)