当前位置: 技术问答>linux和unix
write问题一问
来源: 互联网 发布时间:2015-10-08
本文导语: 问题背景: 控制一个计算机外围设备,该设备是USB接口的HID类设备,程序如下: #include #include #include #include int main() { char buf[2]; int w; int fd=open("/dev/usb/hiddev0",O_RDWR); //hiddev0是对应的设备文件 if(fd == ...
问题背景:
控制一个计算机外围设备,该设备是USB接口的HID类设备,程序如下:
#include
#include
#include
#include
int main()
{
char buf[2];
int w;
int fd=open("/dev/usb/hiddev0",O_RDWR); //hiddev0是对应的设备文件
if(fd == -1)
{
printf("Open hiddev0 Error!n");
}else{
printf("Open hiddev0 Ok!n");
buf[0]=0x02;
buf[1]=0x13;
w=write(fd, buf, 2);//这里返回 -1不知道为什么,在这里是发送的命令和操作系统应该没有关系的呀,难到写法有问题?
printf("write hiddev0 %d n",w);
}
close(fd);
return 1;
}
//#########################windows下实现的发送命令代码####################
CHAR OutputReport[2];
OutputReport[0]=0x02;
if (check3==1){//表示锁定
OutputReport[1]=0x14;
}else{
OutputReport[1]=0x13;
}
DWORD BytesWritten = 0;
ULONG Result;
Result = WriteFile
(DeviceHandle,
OutputReport,
Capabilities.OutputReportByteLength,
&BytesWritten,
NULL);
控制一个计算机外围设备,该设备是USB接口的HID类设备,程序如下:
#include
#include
#include
#include
int main()
{
char buf[2];
int w;
int fd=open("/dev/usb/hiddev0",O_RDWR); //hiddev0是对应的设备文件
if(fd == -1)
{
printf("Open hiddev0 Error!n");
}else{
printf("Open hiddev0 Ok!n");
buf[0]=0x02;
buf[1]=0x13;
w=write(fd, buf, 2);//这里返回 -1不知道为什么,在这里是发送的命令和操作系统应该没有关系的呀,难到写法有问题?
printf("write hiddev0 %d n",w);
}
close(fd);
return 1;
}
//#########################windows下实现的发送命令代码####################
CHAR OutputReport[2];
OutputReport[0]=0x02;
if (check3==1){//表示锁定
OutputReport[1]=0x14;
}else{
OutputReport[1]=0x13;
}
DWORD BytesWritten = 0;
ULONG Result;
Result = WriteFile
(DeviceHandle,
OutputReport,
Capabilities.OutputReportByteLength,
&BytesWritten,
NULL);
|
USB驱动是否有问题?
|
如果不知道详细的硬件描述,则先用HIDIOCGUSAGE取得所有的usage_ref.report_type = HID_REPORT_TYPE_OUTPUT的硬件描述,
取得 usage_ref.field_index, usage_ref.usage_code等数据,然后
usage_ref.report_id = 0x02 ;(report_id)
usage_ref.usage_index = 0; (command index)
usage_ref.value = 0x13 ; (command)
然后再
ioctl(fd, HIDIOCSUSAGE, &usage_ref);
rep_info.report_type = HID_REPORT_TYPE_OUTPUT;
rep_info.report_id = 0x02 ;(report_id)
rep_info.num_fields = 1;
ioctl(fd, HIDIOCSREPORT, &rep_info);
取得 usage_ref.field_index, usage_ref.usage_code等数据,然后
usage_ref.report_id = 0x02 ;(report_id)
usage_ref.usage_index = 0; (command index)
usage_ref.value = 0x13 ; (command)
然后再
ioctl(fd, HIDIOCSUSAGE, &usage_ref);
rep_info.report_type = HID_REPORT_TYPE_OUTPUT;
rep_info.report_id = 0x02 ;(report_id)
rep_info.num_fields = 1;
ioctl(fd, HIDIOCSREPORT, &rep_info);
|
printf("error=%d(%s)n", errno, strerror(errno)); 用这个系统错误打出来看看。
|
应该是驱动问题!这个代码没问题。
|
hiddev是不能使用wirte的,应该使用ioctl的,HIDIOCSUSAGE和HIDIOCSREPORT,
HIDIOCSREPORT - struct hiddev_report_info (write)
Instructs the kernel to send a report to the device. This report can
be filled in by the user through HIDIOCSUSAGE calls (below) to fill in
individual usage values in the report beforesending the report in full
to the device.
HIDIOCSUSAGE - struct hiddev_usage_ref (write)
Sets the value of a usage in an output report
HIDIOCSREPORT - struct hiddev_report_info (write)
Instructs the kernel to send a report to the device. This report can
be filled in by the user through HIDIOCSUSAGE calls (below) to fill in
individual usage values in the report beforesending the report in full
to the device.
HIDIOCSUSAGE - struct hiddev_usage_ref (write)
Sets the value of a usage in an output report