免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2562 | 回复: 6
打印 上一主题 下一主题

[函数] 管道不能工作(在线等) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-01-25 09:17 |只看该作者 |倒序浏览
当我把管道和鼠标设备的描述符用select控制的时候,select只能检测到鼠标的数据,此时
我无论如何向管道写数据都收不到。难道select不能监视硬件设备文件描述符?
很急啊,我都调试了一天了,就是不知道原因在哪里?望大家帮忙!俺先谢谢了。
代码如下:

#include "/debug/gcc4_debug.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>

#include <sys/select.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/param.h>

#include <termios.h>
#include <linux/kd.h>
#include <errno.h>
#include <pthread.h>

/* Mouse button bits*/
#define MSG_MS_ON   0x01
#define MSG_KB_ON   0x02

#define KEY_DEV "/dev/tty"
#define MOUSE_DEV "/dev/psaux"
#define TRAN_DEV "/dev/ttyS0"

static int m_pipe_fd[2];
static int m_ms_fd = -1;

static int mouse_init ();
static void mouse_uninit ();
static int pipe_init ();
static void pipe_uninit ();
static void set_status (char status);
static void thread_send (void *param);

static int mouse_init ()
{
    unsigned char IMPS2_Param[] = { 243, 200, 243, 100, 243, 80 };
    if ((m_ms_fd = open (MOUSE_DEV, O_RDWR)) < 0)
    {
        perror ("open");
        exit (1);
    }
    //write (m_ms_fd, IMPS2_Param, sizeof (IMPS2_Param));

    return 0;
}

static void mouse_uninit ()
{
    if (m_ms_fd != -1)
        close (m_ms_fd);
}

static int pipe_init ()
{
    if (pipe (m_pipe_fd) < 0)
    {
        perror ("pipe");
        exit (1);
    }
    return 0;
}

static void pipe_uninit ()
{
    close (m_pipe_fd[0]);
    close (m_pipe_fd[1]);
}


void set_status (char status)
{

    if (write (m_pipe_fd[1], &status, 1) != 1)
    {
        printf ("write to pipe error!\n");
        perror ("write");
    }

}


static void thread_send (void *param)
{
    char cmd;
    int ret;
    int maxfd;
    fd_set rset;
    unsigned int status;

    status = 0;
    FD_ZERO (&rset);

    maxfd = m_pipe_fd[0];
    maxfd = MAX (maxfd, m_ms_fd);
    maxfd = maxfd + 1;

    FD_SET (m_pipe_fd[0], &rset);
    FD_SET (m_ms_fd, &rset);

    while (1)
    {
        cmd = 0;
        printf ("Select now...\n");
        if (select (maxfd, &rset, NULL, NULL, NULL) < 0)
        {
            printf ("select ...\n");
            if (errno == EINTR)
                continue;
            else
                perror ("select");
        }

        if (FD_ISSET (m_pipe_fd[0], &rset))
        {
            printf ("Read a cmd\n");
            ret = read (m_pipe_fd[0], &cmd, 1);
            if (ret != 1)
            {
                perror ("read");
                exit (0);
            }
        }

        char buffer[1024];
        int n;

        if (FD_ISSET (m_ms_fd, &rset))
        {
            n = read (m_ms_fd, buffer, 1024);
            printf ("Read form ms -%d \n",n);
        }

        switch (cmd)
        {
        case 'm':
            status |= MSG_MS_ON;
            break;
        case 's':
            status &= (~MSG_MS_ON);
            break;
        case 'k':
            status |= MSG_KB_ON;
            break;
        case 'b':
            status &= (~MSG_KB_ON);
            break;
        default:
            break;
        }

    }
}

int main (int argc, char **argv)
{
    pthread_t thread;
    int ret;
    char c;

    pipe_init ();
    mouse_init ();

    ret = pthread_create (&thread, NULL, (void *) &thread_send, NULL);
    if (ret != 0)
    {
        printf ("Create msg server error!\n");
        perror ("thread create");
        return ret;
    }

    c = '?';
    while (c != 27)
    {
        switch (c)
        {
        case '?':
            printf ("Usage: Please input a char\n"
                    "\t?---print help\n"
                    "\tm---mouse send on\n"
                    "\ts---mouse send off\n"
                    "\tk---keyboard send on\n"
                    "\tb---keyboard send off\n");
            break;
        case 'm':
            set_status ('m');
            break;
        case 's':
            set_status ('s');
            break;
        case 'k':
            set_status ('k');
            break;
        case 'b':
            set_status ('b');
            break;
        default:
            break;
        }
        c = getchar ();
    }

    pipe_uninit ();
    mouse_uninit ();
    return 0;
}

论坛徽章:
0
2 [报告]
发表于 2008-01-25 09:55 |只看该作者
看来有问题只能靠自己,
select监视驱动不行。非得用poll。

论坛徽章:
0
3 [报告]
发表于 2008-01-25 10:34 |只看该作者
还是盼望哪位大哥帮忙解释一下,同时监视管道和鼠标设备。poll行,select就不行。
但是select是可以监视多个硬件设备的,这我试过。
管道混一起了就不行了。

论坛徽章:
0
4 [报告]
发表于 2008-01-26 02:44 |只看该作者
原帖由 closetome123 于 2008-1-25 09:17 发表
当我把管道和鼠标设备的描述符用select控制的时候,select只能检测到鼠标的数据,此时
我无论如何向管道写数据都收不到。难道select不能监视硬件设备文件描述符?
很急啊,我都调试了一天了,就是不知道原因在 ...


极其低级的错误!

好好看看select的手册,注意一下原型中三个fdset的指针没有const修饰!!
明白了???

每次select前都必须重新构造描述符集!否则......

论坛徽章:
0
5 [报告]
发表于 2008-01-26 04:50 |只看该作者
原帖由 JohnBull 于 2008-1-26 02:44 发表


极其低级的错误!

好好看看select的手册,注意一下原型中三个fdset的指针没有const修饰!!
明白了???

每次select前都必须重新构造描述符集!否则......

强人。。。

论坛徽章:
0
6 [报告]
发表于 2008-01-27 00:46 |只看该作者
原帖由 JohnBull 于 2008-1-26 02:44 发表


极其低级的错误!

好好看看select的手册,注意一下原型中三个fdset的指针没有const修饰!!
明白了???

每次select前都必须重新构造描述符集!否则......


不愧是牛老师,哈哈

论坛徽章:
0
7 [报告]
发表于 2008-02-01 10:34 |只看该作者
真是犯的低级错误啊,
多谢大哥指导!
当初我还以为select起作用了,陷进去后一直以为select的fdset还能用。
原来一直就没有起作用。
以后一定仔仔细细看manpages。
至于原型中三个fdset的指针,我看手册里没有提到要用const修饰。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP