当前位置: 技术问答>linux和unix
linux下驱动开发ioctl问题
来源: 互联网 发布时间:2016-12-09
本文导语: 各位大侠好,小弟刚接触linux,想写个驱动程序,其中用到了ioctl向设备发送命令,可是经调试发现,数据虽然可以发送到设备,但是,发送的数据和本人要发送的不符。比如:本人要发送unsigned char temp[32]={0x00 0x34 ...
各位大侠好,小弟刚接触linux,想写个驱动程序,其中用到了ioctl向设备发送命令,可是经调试发现,数据虽然可以发送到设备,但是,发送的数据和本人要发送的不符。比如:本人要发送unsigned char temp[32]={0x00 0x34 0x45 0x90 0x30},前面为伪代码,发送到设备后,发现设备接收到的是{0x00 0x34 0x45 0x7f 0x30},那就意味着,发送的数据必须是有符号的char型。发送函数如下:
typedef signed short s16;
typedef signed int s32;
typedef unsigned int u32;
//static unsigned char PCB01=0x0B;//保存最后一次发送的PCB标志
static unsigned int PCB=0x01;
struct hiddev_devinfo {
u32 bustype;
u32 busnum;
u32 devnum;
u32 ifnum;
s16 vendor;
s16 product;
s16 version;
u32 num_applications;
};
struct hiddev_report_info {
u32 report_type;
u32 report_id;
u32 num_fields;
};
#define HID_REPORT_TYPE_INPUT 1
#define HID_REPORT_TYPE_OUTPUT 2
#define HID_REPORT_TYPE_FEATURE 3
struct hiddev_usage_ref {
u32 report_type;
u32 report_id;
u32 field_index;
u32 usage_index;
u32 usage_code;
u32 value;
};
struct hiddev_usage_ref_multi {
struct hiddev_usage_ref uref;
u32 num_values;
u32 values[1024];
};
send_report(int fd, unsigned char*buf, int n)
{
struct hiddev_usage_ref_multi uref;
struct hiddev_report_info rinfo;
int i;
if(ioctl(fd, HIDIOCINITREPORT, 0)==-1)
{
printf("first EERR");
return -1;/* NOT_OPENED_DEVICE 属于自己定义宏 */
}
//printf("fist EERR");
uref.uref.report_type = HID_REPORT_TYPE_FEATURE;
uref.uref.report_id = HID_REPORT_ID_FIRST;
uref.uref.field_index = 0;
uref.uref.usage_index = 0;
uref.num_values = n;
for (i = 0; i
typedef signed short s16;
typedef signed int s32;
typedef unsigned int u32;
//static unsigned char PCB01=0x0B;//保存最后一次发送的PCB标志
static unsigned int PCB=0x01;
struct hiddev_devinfo {
u32 bustype;
u32 busnum;
u32 devnum;
u32 ifnum;
s16 vendor;
s16 product;
s16 version;
u32 num_applications;
};
struct hiddev_report_info {
u32 report_type;
u32 report_id;
u32 num_fields;
};
#define HID_REPORT_TYPE_INPUT 1
#define HID_REPORT_TYPE_OUTPUT 2
#define HID_REPORT_TYPE_FEATURE 3
struct hiddev_usage_ref {
u32 report_type;
u32 report_id;
u32 field_index;
u32 usage_index;
u32 usage_code;
u32 value;
};
struct hiddev_usage_ref_multi {
struct hiddev_usage_ref uref;
u32 num_values;
u32 values[1024];
};
send_report(int fd, unsigned char*buf, int n)
{
struct hiddev_usage_ref_multi uref;
struct hiddev_report_info rinfo;
int i;
if(ioctl(fd, HIDIOCINITREPORT, 0)==-1)
{
printf("first EERR");
return -1;/* NOT_OPENED_DEVICE 属于自己定义宏 */
}
//printf("fist EERR");
uref.uref.report_type = HID_REPORT_TYPE_FEATURE;
uref.uref.report_id = HID_REPORT_ID_FIRST;
uref.uref.field_index = 0;
uref.uref.usage_index = 0;
uref.num_values = n;
for (i = 0; i