Hello all,
iam trying to build a USBHid Device with the provided examples, that can be found here: https://developer.mbed.org/handbook/USBHID.
In general the example is working, but there arrives no data at the controller.
The statement if(hid.read(&recv_report)) in line 33 evaluates to true, when i send messages with the python code attached below, but the report always has a length of 0 and it contains no data.
The problem is the same with mbed 2 and mbed 5.
Does anybody got an idea of what might be wrong with my code?
slightly modified version of example
#include "mbed.h"
#include "USBHID.h"
//We declare a USBHID device. Input out output reports have a length of 8 bytes
USBHID hid(8, 8);
//This report will contain data to be sent
HID_REPORT send_report;
HID_REPORT recv_report;
Serial pc(USBTX, USBRX);
Timer debugTimer;
int main(void) {
debugTimer.start();
send_report.length = 8;
while (1)
{
//Fill the report
for (int i = 0; i < send_report.length; i++)
{
send_report.data[i] = rand() & 0xff;
}
//Send the report
hid.send(&send_report);
//try to read a msg
if(hid.read(&recv_report))
{
pc.printf("received at %d, length of report %d: ", debugTimer.read_us(), recv_report.length);
for(int i = 0; i < 8; i++)
{
pc.printf("%d ", recv_report.data[i]);
}
pc.printf("\r\n");
}
wait(0.2);
}
}
python side with pywinusb
import pywinusb.hid as hid
from time import sleep
import random
# handler called when a report is received
def rx_handler(data):
print 'recv: ', data
def findHIDDevice(mbed_usage, mbed_vendor_id):
# Find all devices connected
all_devices = hid.HidDeviceFilter(vendor_id = mbed_vendor_id).get_devices()
if not all_devices:
print "No device connected"
else:
# search for the Mbed
for HIDdevice in all_devices:
try:
HIDdevice.open()
# browse output reports
for report in HIDdevice.find_output_reports():
if mbed_usage in report:
#MBED found
print 'Mbed detected'
print report
#Attach a custom handler when a data is received
HIDdevice.set_raw_data_handler(rx_handler)
#send a report each 0.2 second. The report is a random array of 8 bytes
while True:
for i in range(8):
report[mbed_usage][i] = random.randint(0, 255)
report.send()
sleep(0.2)
except:
print 'close'
print traceback.format_exc()
HIDdevice.close()
if __name__ == '__main__':
# The vendor ID used in the Mbed program
mbed_vendor_id = 0x1234
# Vendor page and usage_id = 2
mbed_usage = hid.get_full_usage_id(0xffab, 0x02)
# Search the Mbed, attach rx handler and send data
findHIDDevice(mbed_usage, mbed_vendor_id)
Hello all,
iam trying to build a USBHid Device with the provided examples, that can be found here: https://developer.mbed.org/handbook/USBHID.
In general the example is working, but there arrives no data at the controller.
The statement if(hid.read(&recv_report)) in line 33 evaluates to true, when i send messages with the python code attached below, but the report always has a length of 0 and it contains no data.
The problem is the same with mbed 2 and mbed 5.
Does anybody got an idea of what might be wrong with my code?
slightly modified version of example
python side with pywinusb