@@ -200,7 +200,7 @@ public ConnectPortResult ConnectDevice()
200200 {
201201 OnLogMessageAvailable ( NanoDevicesEventSource . Log . OpenDevice ( InstanceId ) ) ;
202202 }
203- else
203+ else
204204 {
205205 // Most likely the device is opened by another app, but cannot be sure
206206 OnLogMessageAvailable ( NanoDevicesEventSource . Log . CriticalError ( $ "Can't open Device: { InstanceId } ") ) ;
@@ -333,13 +333,35 @@ public byte[] ReadBuffer(int bytesToRead)
333333
334334 try
335335 {
336- int readBytes = Device . Read ( buffer , 0 , bytesToRead ) ;
336+ // this loop shall fix issues with M1 and some drivers showing up bytes to fast
337+ List < byte > received = new List < byte > ( ) ;
338+ int readBytes = 0 ;
339+
340+ while ( Device . BytesToRead > 0 && readBytes < bytesToRead )
341+ {
342+ // compute how many bytes shall be read from the device
343+ // so that we don't read more than what was requested
344+ int missingBytes = Math . Min ( Device . BytesToRead , bytesToRead - readBytes ) ;
345+
346+ byte [ ] toRead = new byte [ missingBytes ] ;
337347
348+ // read them
349+ readBytes += Device . Read ( toRead , 0 , toRead . Length ) ;
350+
351+ // add to buffer
352+ received . AddRange ( toRead ) ;
353+ }
354+
355+ // resize read buffer, if needed
338356 if ( readBytes != bytesToRead )
339357 {
340358 Array . Resize ( ref buffer , readBytes ) ;
341359 }
342360
361+ // copy over to read buffer
362+ received . CopyTo ( buffer ) ;
363+
364+ // done here
343365 return buffer ;
344366 }
345367 catch ( TimeoutException )
0 commit comments