@ -77,13 +77,27 @@ public class UsbHidProtocol : CommunicationProtocol
{
{
while ( ct . IsCancellationRequested = = false )
while ( ct . IsCancellationRequested = = false )
{
{
// if usb hid device is null => something is wrong => break loop
if ( this . usbHidDevice = = null )
{
this . OnUnintentionallyDisconnected ( ) ;
break ;
}
ReadOnlySpan < byte > readData ;
ReadOnlySpan < byte > readData ;
if ( this . usbHidDevice ! = null )
try
{
{
// read with timeout
// read with timeout
readData = this . usbHidDevice . ReadTimeout ( 2 0 0 , ( int ) ReadTimeoutMs ) ;
readData = this . usbHidDevice . ReadTimeout ( 2 0 0 , ( int ) ReadTimeoutMs ) ;
}
catch // on exception => break loop
{
this . OnUnintentionallyDisconnected ( ) ;
break ;
}
// any data received?
// any data received?
if ( readData . Length > 0 )
if ( readData . Length > 0 )
{
{
foreach ( var readByte in readData )
foreach ( var readByte in readData )
{
{
@ -92,13 +106,6 @@ public class UsbHidProtocol : CommunicationProtocol
}
}
}
}
}
}
// if usb hid device is null => something is wrong => break loop
else
{
this . OnUnintentionallyDisconnected ( ) ;
break ;
}
}
}
}
protected override bool InternalSendBytes ( byte [ ] bytes )
protected override bool InternalSendBytes ( byte [ ] bytes )
@ -108,7 +115,7 @@ public class UsbHidProtocol : CommunicationProtocol
Debug . WriteLine ( $"{nameof(InternalSendBytes)}() of {nameof(UsbHidProtocol)} has {bytesToSend.Count} bytes to send." ) ;
Debug . WriteLine ( $"{nameof(InternalSendBytes)}() of {nameof(UsbHidProtocol)} has {bytesToSend.Count} bytes to send." ) ;
while ( bytesToSend . Count > 0 )
while ( bytesToSend . Count > 0 )
{
{
// take from the list the amount of bytes to send. ToList creates a shallow copy.
// take from the list the amount of bytes to send. ToList creates a shallow copy.
//bytesToSend.CopyTo(0, nextBytes, 0, Math.Min(numBytesToTake, bytesToSend.Count));
//bytesToSend.CopyTo(0, nextBytes, 0, Math.Min(numBytesToTake, bytesToSend.Count));
@ -120,7 +127,7 @@ public class UsbHidProtocol : CommunicationProtocol
// check number of bytes to send
// check number of bytes to send
int numBytesToSend = nextBytes . Count ( ) ;
int numBytesToSend = nextBytes . Count ( ) ;
if ( numBytesToSend > MaxBytesPerReport | | numBytesToSend > byte . MaxValue | | numBytesToSend < = 0 )
if ( numBytesToSend > MaxBytesPerReport | | numBytesToSend > byte . MaxValue | | numBytesToSend < = 0 )
{
{
throw new Exception ( $"'{nameof(InternalSendBytes)}()': Invalid of bytes to send ({nameof(numBytesToSend)})" ) ;
throw new Exception ( $"'{nameof(InternalSendBytes)}()': Invalid of bytes to send ({nameof(numBytesToSend)})" ) ;
}
}
@ -137,10 +144,19 @@ public class UsbHidProtocol : CommunicationProtocol
return false ;
return false ;
}
}
this . usbHidDevice . Write ( bytes ) ;
try
{
this . usbHidDevice . Write ( sendableBytes . ToArray ( ) ) ;
}
catch ( Exception ex )
{
this . logger . LogException ( ex , $"Failed to Write Data to USB HID device " +
$"VID={this.usbHidSettings!.VendorId}, PID={this.usbHidSettings.ProductId}." , nameof ( UsbHidProtocol ) ) ;
return false ;
}
// report sent bytes
// report sent bytes
foreach ( byte b in bytes )
foreach ( byte b in senda bleB ytes)
{
{
this . OnSentData ( new ExtendedByte ( b ) ) ;
this . OnSentData ( new ExtendedByte ( b ) ) ;
}
}