Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with sending output report #88

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 4 additions & 35 deletions pywinusb/hid/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,40 +531,9 @@ def send_output_report(self, data):
raw_data[index] = data[index]
else:
raw_data = data
#
# Adding a lock when writing (overlapped writes)
over_write = winapi.OVERLAPPED()
over_write.h_event = winapi.CreateEvent(None, 0, 0, None)
if over_write.h_event:
try:
overlapped_write = over_write
winapi.WriteFile(int(self.hid_handle), byref(raw_data), len(raw_data),
None, byref(overlapped_write)) #none overlapped
error = ctypes.GetLastError()
if error == winapi.ERROR_IO_PENDING:
# overlapped operation in progress
result = error
elif error == 1167:
raise HIDError("Error device disconnected before write")
else:
raise HIDError("Error %d when trying to write to HID "\
"device: %s"%(error, ctypes.FormatError(error)) )
result = winapi.WaitForSingleObject(overlapped_write.h_event, 10000 )
if result != winapi.WAIT_OBJECT_0:
# If the write times out make sure to
# cancel it, otherwise memory could
# get corrupted if the async write
# completes after this functions returns
winapi.CancelIo( int(self.hid_handle) )
raise HIDError("Write timed out")
finally:
# Make sure the event is closed so resources aren't leaked
winapi.CloseHandle(over_write.h_event)
else:
return winapi.WriteFile(int(self.hid_handle), byref(raw_data),
len(raw_data),
None, None) #none overlapped
return True #completed

return hid_dll.HiD_SetOutputReport(int(self.hid_handle), byref(raw_data),
len(raw_data))

def send_feature_report(self, data):
"""Send input/output/feature report ID = report_id, data should be a
Expand All @@ -582,7 +551,7 @@ def send_feature_report(self, data):
raw_data = data

return hid_dll.HidD_SetFeature(int(self.hid_handle), byref(raw_data),
len(raw_data))
len(raw_data))

def __reset_vars(self):
"""Reset vars (for init or gc)"""
Expand Down