필터 지우기
필터 지우기

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

조회 수: 26 (최근 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 CenterFile Exchange에서 Web Services에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by