主题 : Qt中调用ioctl失败 复制链接 | 浏览器收藏 | 打印
级别: 新手上路
UID: 64587
精华: 0
发帖: 13
金钱: 65 两
威望: 13 点
综合积分: 26 分
注册时间: 2012-03-07
最后登录: 2014-06-08
楼主  发表于: 2012-06-14 16:52

 Qt中调用ioctl失败

自己写的motor_drv.c驱动,在用c写的测试程序中ioctl()调用成功,并且控制正常,于是用Qt开发应用程序,在初始化的时候调用::ioctl(),但是返回-1,失败了,难道qt中调用::ioctl()函数和在int main()程序中不一样吗?
这是motor_cmd.h

#ifndef _MOTOR_CMD_H
#define _MOTOR_CMD_H

#include <linux/ioctl.h>

#define MOTOR_MAGIC 'x'        //define magic
#define MOTOR_MAX_NR 8        //define the maxim number

#define MOTOR_FORWARD         _IO(MOTOR_MAGIC, 1)
#define MOTOR_BACK              _IO(MOTOR_MAGIC, 2)
#define MOTOR_BRAKE          _IO(MOTOR_MAGIC, 3)
#define MOTOR_ORIGIN_LEFT   _IO(MOTOR_MAGIC, 4)
#define MOTOR_ORIGIN_RIGHT  _IO(MOTOR_MAGIC, 5)
#define MOTOR_TURN_LEFT     _IO(MOTOR_MAGIC, 6)
#define MOTOR_TURN_RIGHT    _IO(MOTOR_MAGIC, 7)
#define MOTOR_STOP            _IO(MOTOR_MAGIC, 8)

#endif        /*_MOTOR_CMD_H*/

这是test.c(测试程序)
#include <stdio.h>
#include<string.h>
#include <sys/types.h>  
#include <sys/stat.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <netinet/in.h>

#include "motor_cmd.h"
int  main(void)
{
    int fd_pwm;
    int fd_dirc;
    int ret;
    fd_dirc = open("/dev/motor", O_RDWR);
    fd_pwm = open("/dev/motor_pwm", O_RDWR);
    if((fd_dirc < 0) && (fd_pwm < 0))
    {
        printf("Open /dev/motor error\n");
            return -1;
    }
    ret = ioctl(fd_pwm, 0, 300);
    printf("ret=%d\n", ret);
    ret = ioctl(fd_dirc, MOTOR_FORWARD);
    printf("ret=%d", ret);
    close(fd_dirc);
    close(fd_pwm);
}
以上运行正常,而我在Qt中开发的时候,在构造函数里初始化
if(::ioctl(fd_pwm, 0, 300)){
        printf("ioctl pwm failed\n");
        return ;
    }
设备节点打开正常,但一到::ioctl()就错误了,返回的值为-1,可在test.c中是调用正确的啊?
有没有遇到同样问题的,指点一二。

小弟不才,请教一下大家。
try
级别: 新手上路
UID: 73201
精华: 0
发帖: 42
金钱: 210 两
威望: 42 点
综合积分: 84 分
注册时间: 2012-07-05
最后登录: 2013-06-19
1楼  发表于: 2012-07-19 08:55
你关闭了close(fd_dirc);
    close(fd_pwm);
*無鈳取玳
级别: 论坛版主
UID: 27
精华: 12
发帖: 5398
金钱: 40120 两
威望: 17929 点
综合积分: 11036 分
注册时间: 2008-01-16
最后登录: 2014-11-22
2楼  发表于: 2012-07-19 10:54
设备节点打开正常,但一到::ioctl()就错误了,返回的值为-1,可在test.c中是调用正确的啊?

把errno打出来看看具体的错误代码。
"If you have an apple and I have an apple and we exchange apples, then you and I will
still each have one apple. But if you have an idea and I have an idea and we exchange
these ideas, then each of us will have two ideas."