Write data in an USB device using HID protocol. Why I have an error ?
Mostrar comentarios más antiguos
Hi !
I try to pilot an usb device (a mini-circuits test board for ksn synthesizer) via USB HID protocol (hid.dll). The basic functions work, except when I try to write data. Indeed, when I write data using this line :
[res,h] = calllib('hidapi32','hid_write',hid.handle,pbuffer,uint64(length(wmsg)));
the answer is always ' -1'. Can you indicate me where is the mistake ?
Thanks.
the complete code:
if ~libisloaded('hidapi32')
[notfound, warnings] = loadlibrary('hidapi32_thunk_pcwin64.dll','hidapi.h','mfilename', 'mHeaderc');
end
calllib('hidapi32','hid_init');
libfunctions('hidapi32');
vendorID = 0;
productID = 0;
pointeur = calllib('hidapi32','hid_enumerate',uint16(vendorID),uint16(productID));
resultats = calllib('hidapi32','hid_free_enumeration',pointeur)
pNull = libpointer('uint16Ptr');
hid.vendorID = 8398;
hid.productID = 32;
pointeur = calllib('hidapi32','hid_enumerate',uint16(hid.vendorID),uint16(hid.productID));
str = calllib('hidapi32','hid_free_enumeration',pointeur);
[hid.handle,value] = calllib('hidapi32','hid_open',uint16(hid.vendorID),uint16(hid.productID),pNull);
hid.device = calllib('hidapi32','hid_open_path',str.path);
hid.nReadBuffer = 256;
hid.nWriteBuffer = 256;
buffer = zeros(1,hid.nReadBuffer);
pbuffer = libpointer('uint16Ptr',uint16(buffer));
[res,h] = calllib('hidapi32','hid_get_product_string',hid.handle,pbuffer,uint64(length(buffer)));
hid.product_string = sprintf('%s',char(pbuffer.Value));
buffer = zeros(1,hid.nReadBuffer);
pbuffer = libpointer('uint16Ptr',uint16(buffer));
[res,h] = calllib('hidapi32','hid_get_serial_number_string',hid.handle,pbuffer,uint64(length(buffer)));
hid.serial_number = pbuffer.Value(1);
buffer = zeros(1,hid.nReadBuffer);
pbuffer = libpointer('uint16Ptr',uint16(buffer));
[res,h] = calllib('hidapi32','hid_get_manufacturer_string',hid.handle,pbuffer,uint64(length(buffer)));
hid.manufacturer_string = sprintf('%s',char(pbuffer.Value));
pause(1)
buffer = zeros(1,hid.nWriteBuffer);
wmsg = [0 0 0 0 0 0 0 0 1 48 00 09 00 00 00 00 00 00];
wmsg(end+(hid.nWriteBuffer-length(wmsg))) = 0;
% create a unit8 pointer
pbuffer = libpointer('uint8Ptr',uint8(wmsg));
% write the message
[res,h] = calllib('hidapi32','hid_write',hid.handle,pbuffer,uint64(length(wmsg)));
% check the response
if res ~= length(wmsg)
fprintf('hidapi write error: wrote %d, sent %d\n',(length(wmsg)-1),res);
end
calllib('hidapi32','hid_close',hid.handle);
Respuestas (4)
Guillaume
el 14 de Sept. de 2015
It looks like you may be using this library (information that should have been included in your post).
The doc for hid_write says it returns -1 on error, so your call failed to write whatever it is you want to write. According to the doc, you can get more information about the error with hid_error. I suggest you try that, your problem is not caused by matlab.
errcause = calllib('hidapi32','hid_error',hid.handle)
2 comentarios
Joffray Guillory
el 14 de Sept. de 2015
Guillaume
el 14 de Sept. de 2015
Well then, if you have visual studio, I suggest you grab the source code for the library and use the debugging facilities of visual studio to find out what the problem is.
Otherwise, find a mailing list for that library or contact the developers.
muhammad naseem
el 8 de En. de 2020
0 votos
try this one
i was also facing problem and i slove the problem this way
wmsg(end+(hid.nWriteBuffer-length(wmsg)) + 1) = 0;
Hello,
I have a simular project, and my problem is that when I give the vendorID and the productID, nothing happen !
the result is :
path: ''
vendor_id: 0
product_id: 0
serial_number: []
release_number: 0
manufacturer_string: []
product_string: []
usage_page: 0
usage: 0
interface_number: 0
next: []
I have to mention that I read the librery like the following :
addpath('C:\Windows\SysWOW64')
loadlibrary('hidapi')
Can I get some help please ?
Melwin Thomas
el 16 de En. de 2023
0 votos
Instead of 'hid_write' use 'hid_send_feature_report'
Categorías
Más información sobre C Shared Library Integration en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!