首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 嵌入开发 > VxWorks >

串口收发的怪异有关问题

2012-03-03 
串口收发的怪异问题求助vxworks5.5.1+s3c2440平台在使用串口读写时,出现如下的问题,能够正常接收,但是无法

串口收发的怪异问题求助
vxworks5.5.1+s3c2440平台
在使用串口读写时,出现如下的问题,能够正常接收,但是无法发送数据
代码如下:

C/C++ code
static int fd;static SEM_ID dataSemId;int taskIdOne, taskIdTwo;char sendBuf[] = "$GPRMC,A*20\r\n";int tty_receive(){    int width;    struct fd_set readFds;    char tch = 0;    int recfd = 0;    recfd = fd;    while(1)    {        FD_ZERO(&readFds);           FD_SET(recfd, &readFds);        width = recfd + 1;                if(select(width, &readFds,NULL,NULL,NULL) == ERROR)        {            logMsg("select() error!\n", 1,2,3,4,5,6);            return(ERROR);        }        if(FD_ISSET(recfd, &readFds))        {            if (read(recfd, &tch, 1) == 0)            {                 logMsg("read() error!\n", 1,2,3,4,5,6);                 return(ERROR);            }            if ('$' == tch)            {                  semGive (dataSemId);  // --> 触发发送            }        }    }        close(recfd);    return 1;}int tty2_send(void){    int bytes_out;    int writeFd = 0;    /*writeFd = open("/tyCo/2", O_RDWR, 0);*/    // 注1    writeFd = fd;                                // 注2        if (writeFd < 0)    {        logMsg("open serial error!\n", 1,2,3,4,5,6);        return(ERROR);    }    else        logMsg("open serial succeed!\n", 1,2,3,4,5,6);        while(1)    {        semTake (dataSemId, WAIT_FOREVER);                bytes_out = write(writeFd, sendBuf, strlen(sendBuf));        if (bytes_out == 0)        {            logMsg("write error() - bytes_out = 0.\n", 1,2,3,4,5,6);            return(ERROR);        }        else        {            logMsg("output chars total: %d\n", bytes_out,2,3,4,5,6);   // 注3        }    }    return 1;}int app(){    dataSemId = semBCreate (SEM_Q_FIFO, SEM_EMPTY);    if (NULL == dataSemId)    {        logMsg("create bianry sem error\n", 1,2,3,4,5,6);        return(ERROR);    }        fd = open("/tyCo/2", O_RDWR, 0);    if (fd < 0)    {        logMsg("app(): open serial error\n", 1,2,3,4,5,6);        return(ERROR);    }    else        logMsg("app(): open serial succeed - fd(%d)\n", fd,2,3,4,5,6);    if (ioctl(fd, FIOSETOPTIONS, OPT_RAW) == ERROR)    {        logMsg("ioctl(OPT_RAW) ERROR!\n", 1,2,3,4,5,6);        return(ERROR);    }    if (ioctl(fd, FIOBAUDRATE, 115200) == ERROR)    {        logMsg("ioctl(115200) ERROR!\n", 1,2,3,4,5,6);        return(ERROR);    }    if (ioctl(fd, SIO_HW_OPTS_SET, CS8) == ERROR)    {        logMsg("ioctl(CS8) ERROR!\n", 1,2,3,4,5,6);        return(ERROR);    }        if (taskIdOne = taskSpawn("comrec",90,0x100,2000,(FUNCPTR)tty_receive,                          0,0,0,0,0,0,0,0,0,0) == ERROR)    {        logMsg("Error in spawning task comrec\n", 1,2,3,4,5,6);        return(ERROR);    }    if (taskIdTwo = taskSpawn("comsend",90,0x100,2000,(FUNCPTR)tty2_send,                          0,0,0,0,0,0,0,0,0,0) == ERROR)    {        logMsg("Error in spawning task comsend\n", 1,2,3,4,5,6);        return(ERROR);    }        return(OK);}void close_com(void){    taskDelete(taskIdOne);    taskDelete(taskIdTwo);    semDelete(dataSemId);    close(fd);}


现象:
(1)按照上面代码运行,能正常接收代码,发送任务中能运行到(注3),但PC机串口收不到任务字符(硬件确定正常),而且semGive (dataSemId)只运行6次就无法触发发送任务了,这是为什么,代码哪里有问题?
(2)发送任务中如果使用(注2),则串口可以正常发送数据,而使用app函数中打开的fd全局变量,却无法发送数据,这是什么原因???难道是任务tty_receive占用了串口fd?????
(3)在shell中,“i”查看任务,发现任务tty_receive的ErrorNO为 0x3d0002,这是什么意思?是否对程序有影响???

请大家多指点指点!

[解决办法]
MARK,顶

热点排行