Write data in an USB device using HID protocol. Why I have an error ?

17 vues (au cours des 30 derniers jours)
Joffray Guillory
Joffray Guillory le 14 Sep 2015
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);

Réponses (4)

Guillaume
Guillaume le 14 Sep 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 commentaires
Joffray Guillory
Joffray Guillory le 14 Sep 2015
Thanks Guillaume for your answer.
Yes, I use the library Hidapi. Lot of people seems using this library and they meet no problem. I have just tried 'hid_error' method but it returns nothing (answer null).
My problem is not solved yet :/
Guillaume
Guillaume le 14 Sep 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.

Connectez-vous pour commenter.


muhammad naseem
muhammad naseem le 8 Jan 2020
try this one
i was also facing problem and i slove the problem this way
wmsg(end+(hid.nWriteBuffer-length(wmsg)) + 1) = 0;

RABEHI
RABEHI le 13 Mai 2020
Modifié(e) : RABEHI le 20 Mai 2020
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
Melwin Thomas le 16 Jan 2023
Instead of 'hid_write' use 'hid_send_feature_report'

Catégories

En savoir plus sur C Shared Library Integration dans Help Center et File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by