I'm basing a project off of your C# 5.0 version of the HID code. Instead of reading byte-by-byte I've tried to setup a system that sends a command and then reads the entire response. This requires multiple calls to GetFeatureReport(...) since the responses can be more than 64 bytes.
I also have a repeat feature that calls my commands to be sent X amount of times. This is executed using a timer set to 10ms timeouts. Whenever I use this feature is when I get the error below, I always send and receive the first command okay but after that the program locks up and about 30 secs later my debugger tells me the error below.
The error I'm getting is the following:
ObjectDisposedException was unhandled by user code
Safe handle has been closed
The code I am using is below, also I am using Visual C# 2010 Express:
private void ExchangeFeatureReports(ref String command)
{
String byteValue = null;
Int32 count = 0;
Byte[] commandBufferTemp = null;
Byte[] inFeatureReportBuffer = null;
Byte[] fullFeatureReportBuffer = null;
Byte[] outFeatureReportBuffer = null;
Boolean success = false;
int j = 0;
try
{
//inFeatureReportBuffer = null;
if ( ( MyHid.Capabilities.FeatureReportByteLength > 0 ) )
{
// The HID has a Feature report.
commandBufferTemp = new Byte[command.Length];
for (int i = 0; i < command.Length; i++)
{
commandBufferTemp[i] = Convert.ToByte(command[i]);
}
// Set the size of the Feature report buffer.
// Subtract 1 from the value in the Capabilities structure because
// the array begins at index 0.
outFeatureReportBuffer = new Byte[ MyHid.Capabilities.FeatureReportByteLength ];
// Store the report ID in the buffer:
outFeatureReportBuffer[ 0 ] = 0;
// Store the report data following the report ID.
// Use the data in the combo boxes on the form.
commandBufferTemp.CopyTo(outFeatureReportBuffer, 1);
// Write a report to the device
success = MyHid.SendFeatureReport(hidHandle, outFeatureReportBuffer);
if (success)
{
lstResults.Items.Add("A Feature report has been written.");
}
else
{
CloseCommunications();
lstResults.Items.Add("The attempt to send a Feature report failed.");
}
// Read a report from the device.
// Set the size of the Feature report buffer.
// Subtract 1 from the value in the Capabilities structure because
// the array begins at index 0.
if ( ( MyHid.Capabilities.FeatureReportByteLength > 0 ) )
{
inFeatureReportBuffer = new Byte[ MyHid.Capabilities.FeatureReportByteLength ];
fullFeatureReportBuffer = new Byte[2048];
}
// Read a report.
do
{
success = MyHid.GetFeatureReport(hidHandle, ref inFeatureReportBuffer);
if ((success) && (inFeatureReportBuffer[1] != '\0'))
{
lstResults.Items.Add("A Feature report has been read.");
for (int i = 1; i < inFeatureReportBuffer.Length; i++, j++)
{
fullFeatureReportBuffer[j] = inFeatureReportBuffer[i];
}
}
else
{
CloseCommunications();
lstResults.Items.Add("The attempt to read a Feature report failed.");
break;
}
System.Threading.Thread.Sleep(100);
} while (inFeatureReportBuffer[1] != '\0');
if (success)
{
DisplayFeatureReport(ref fullFeatureReportBuffer);
}
}
else
{
lstResults.Items.Add( "The HID doesn't have a Feature report." );
}
ScrollToBottomOfListBox();
cmdSend.Enabled = true;
}
catch ( Exception ex )
{
DisplayException( this.Name, ex );
throw ;
}
}
void DisplayFeatureReport(ref Byte[] FeatureReport)
{
String response = System.Text.Encoding.ASCII.GetString(FeatureReport);
response = response.Replace('\0', ' ').TrimEnd();
txtBytesReceived.AppendText(response);
txtBytesReceived.AppendText(Environment.NewLine);
}