@@ -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