LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 881|回复: 0

串口编程问题:读写串口时能读则不能写,能写则不能读,为什么?

[复制链接]
发表于 2004-2-25 12:16:25 | 显示全部楼层 |阅读模式
代码如下:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
main(int argc,char** argv)
{
        int com1;
        struct termios options;
        int i;
        unsigned char cmd[] = {0x1b,0x80,0x07,5,0xa7,0};
        unsigned char buf[1];
        com1=open("/dev/ttyS0",O_RDWR);
        if(com1)
        {
                tcgetattr(com1,&options);
                cfsetispeed(&options,B9600);
                cfsetospeed(&options,B9600);
                options.c_cflag |=(CLOCAL | CREAD);
                tcsetattr(com1,TCSANOW,&options);
                tcflush(com1,TCIOFLUSH);
               
                for(i=0;i<5;i++)
                {
                        usleep(5);
                        write(com1,cmd+i,1);
                }
                for(i=0;i<10;i++)
                {
                        read(com1,buf,1);
                        printf("%c\n",buf[0]);
                }
                close(com1);
        }
        else
        {
                printf("cannot open com1");
                exit(1);
        }
}
com1接着一个设备,给他发{0x1b,0x80,0x07,5,0xa7,0}他就会返回数据
设备能接受到{0x1b,0x80,0x07,5,0xa7,0},也发出数据了,但程序就是不能从串口读出数据,阻塞在read语句那,但如果此时打开minicom,read就能进行下去并能正确读出数据,不过如果先打开minicom后执行程序设备就收不到{0x1b,0x80,0x07,5,0xa7,0}了。
我猜测minicom是对串口做了一些初始化,所以能读
要怎么设置串口才能又能读又能写呢
以上过程中程序没有错误信息返回。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表