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

14 ビュー (過去 30 日間)
Joffray Guillory
Joffray Guillory 2015 年 9 月 14 日
回答済み: Melwin Thomas 2023 年 1 月 16 日
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);

回答 (4 件)

Guillaume
Guillaume 2015 年 9 月 14 日
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 件のコメント
Joffray Guillory
Joffray Guillory 2015 年 9 月 14 日
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 2015 年 9 月 14 日
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
muhammad naseem 2020 年 1 月 8 日
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 2020 年 5 月 13 日
編集済み: RABEHI 2020 年 5 月 20 日
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 2023 年 1 月 16 日
Instead of 'hid_write' use 'hid_send_feature_report'

カテゴリ

Help Center および File ExchangeC Shared Library Integration についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by