Home > Back-end >  USB HID read data problems, how to know a machine to send the data, and then read the corresponding
USB HID read data problems, how to know a machine to send the data, and then read the corresponding

Time:09-18

If the PC software written instructions, and then read data is normal, but the PC is not send instructions, send the data under a machine at other times, how to read?

Written instructions or PC, PC receives the command returns the data to the PC, the PC will receive send data to the PC,

But, after a period of time, PC send other new data to the PC (sending time indefinite), PC at this time how to know that the PC sends data, and then to receive?

How much time I've ever had, ms read data, but it can't normal read data?

PC data sent to the PC, if there was any state, and then to inform PC to read the data?

With Bus Hound or other tools can normal caught the PC data,

How can normal PC software to receive the data?


CodePudding user response:

Similar to read serial ports, first you have to find the device name, with the device name can like open file operations.

CodePudding user response:

reference 1st floor chinayu2007 response:
and read a serial port, first you have to find the device name, with the device name can like open file operations.


PC after sending an instruction, can transmit the data on normal receipt,
Write with: FileStream. Wirte ()
Read with BeginRead ()


But in the PC without sending instructions, under a pass data to PC on board? Don't know when to go to, have been circulating to read?

CodePudding user response:

Make a thread has been read, cycle of Sleep (1) it can reduce the CPU share,

CodePudding user response:

reference u010165006 reply: 3/f
make a thread has been read, cycle of Sleep (1) it can reduce the CPU share,


Open a thread has been read is to read down a machine to send data, but the PC sends out instructions,
The original method is:
1. Use the FileStream. Write () to Write data,
2. Use the FileStream. BeginRead or FileStream. Read Read data

to invoke reflection, write data to the actual wrote down machine,
That just call FileStream. Write (), in the Bus Hound can't see the data output, after the calls Read, Bus Hound output data, and the data returned by the
But the problem is the child thread has been called the FileStream. Data Read Read, so now the program execution has stopped in there to Read, not bottom go to,






CodePudding user response:

Read when there is no data to Read not return? Why is FileStream. Read, action object FileStream is why?

CodePudding user response:

Thread loop reads, with Sleep up some CPU, two CPP to read, write FileStream CriticalSection is used to control and avoid conflict,

CodePudding user response:

USB HID not familiar with, so use of written by c # read USB HID device testing software
http://download.csdn.net/download/haiguozhe/8908701#comment
change,

 



 


Public class DeviceInformation
{
Public Boolean DeviceIsDetected;
Public String myDevicePathName;
Public HIDD_ATTRIBUTES DeviceAttributes;
Public HIDP_CAPS "Capabilities;
Public IntPtr DeviceNotificationHandle;
Public int NumberOfInputBuffers;
Public Boolean ExclusiveAccess;
Public FileStream FileStreamDeviceData;
Public SafeFileHandle HidHandle;
Public String HidUsage;
}

Private void ReOpenDeviceFileStreamHandler (DeviceInformation findDevice)
{
//close the handle and the read/write mode to open the
FindDevice. HidHandle. Close ();
FindDevice. HidHandle=FileIO. CreateFile (findDevice. MyDevicePathName, FileIO GENERIC_READ | FileIO. GENERIC_WRITE, FileIO. FILE_SHARE_READ | FileIO. FILE_SHARE_WRITE, IntPtr. Zero, FileIO. OPEN_EXISTING, 0, 0).
The Debug. WriteLine (" in the function ReOpenDeviceFileStreamHandler "+ enclosing MyDebugging. ResultOfAPICall (" CreateFile"));

If (findDevice. HidHandle. IsInvalid)
{
FindDevice. ExclusiveAccess=true;
Debug. WriteLine (" this device is a system "+ findDevice. HidUsage +", "+" for this device, Windows 2000 and Windows XP to use the Input and Output reports function. ");
}
The else
{
If (findDevice. "Capabilities. InputReportByteLength & gt; 0)
{
//Set the size of the Input report buffer.
Byte [] inputReportBuffer=null;

InputReportBuffer=new Byte [findDevice. "Capabilities. InputReportByteLength];
FindDevice. FileStreamDeviceData=https://bbs.csdn.net/topics/new FileStream (findDevice HidHandle, FileAccess. Read | FileAccess. Write, inputReportBuffer. Length, false);
}

//Flush any waiting reports in the input buffer. (optional)
_hidObject. FlushQueue (findDevice. HidHandle);
}
}


Write data:

 

If (device) FileStreamDeviceData) CanWrite)
{
Device. FileStreamDeviceData. Write (outputReportBuffer, 0, outputReportBuffer. Length);
Success=true;
}

Read data:
 
Ar=device. FileStreamDeviceData. BeginRead (inputReportBuffer, 0, inputReportBuffer Length, new AsyncCallback (GetInputReportData), inputReportBuffer);



If now drab with written instructions, and then read the instruction, can send instructions and receive the returned normal, but if we don't send instruction, will not receive a machine data,
If the child thread calls to read, can read all the data from the lower machine is normal, but can't send out instructions,

CodePudding user response:

The question now is to open a thread calls asynchronous reads:
Device. FileStreamDeviceData. BeginRead (inputReportBuffer, 0, inputReportBuffer Length, new AsyncCallback (GetInputReportData), inputReportBuffer);
The child thread has been on standby, waiting for data to, then reads the data processing,

Now call write (after) cannot calls read or BeginRead instructions issued?

Actually call asynchronous read, he itself to have another thread,
Issued instructions can stop asynchronous read threads, issued an order, after open the read again?

CodePudding user response:

Issued instructions can stop asynchronous read threads, issued an order, after open the read again?
-
Can be hung thread, issued, and then continue to thread,
You said device. FileStreamDeviceData. BeginRead will have been waiting to have data to back, check for a member function to determine whether there is data, to have data to read,

CodePudding user response:

references 9 f u010165006 response:
issued instructions can stop asynchronous read threads, issued an order, after open the read again?
-
Can be hung thread, issued, and then continue to thread,
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related