找回密码
 注册
查看: 515|回复: 3

关于串口编程的问题

[复制链接]
发表于 2004-6-30 09:44:03 | 显示全部楼层 |阅读模式
在使用串口时,如果串口初始化如下:
int setup_com(int fd){
    struct termios options;
    tcgetattr(fd, &options);
    /* Set the baud rates to 38400...*/
    cfsetispeed(&options, B38400);
    cfsetospeed(&options, B38400);
    /* Enable the receiver and set local mode...*/
    options.c_cflag |= (CLOCAL | CREAD);
    /* Set c_cflag options.*/
    options.c_cflag |= PARENB;
    options.c_cflag &= ~PARODD;
    options.c_cflag &= ~CSTOPB;
    options.c_cflag &= ~CSIZE;
    options.c_cflag |= CS8;   
    /* Set c_iflag input options */
    options.c_iflag &=~(IXON | IXOFF | IXANY);
    options.c_iflag &=~(INLCR | IGNCR | ICRNL);
    options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
    /* Set c_oflag output options */
    options.c_oflag &= ~OPOST;   
    /* Set the timeout options */
    options.c_cc[VMIN]  = 0;
    options.c_cc[VTIME] = 10;
    tcsetattr(fd, TCSANOW, &options);
    return 1;
}

在运行时即时改变波特率的话用
cfsetispeed(&options, 波特率);
cfsetospeed(&options, 波特率);
tcsetattr(fd, TCSANOW, &options);

但即时更改数据位停止位或校验方式这些怎么更改,已经试过如下方法无效:
options.c_cflag |= CS7; //原先初始化成CS8八位数据,现在要即时改成CS7七位数据
tcsetattr(fd, TCSANOW, &options);

难道更改这些参数必须关闭端口重新打开初始化?
发表于 2004-6-30 13:30:48 | 显示全部楼层
C8是01100000
C7是01000000
C8|C7还是C8
回复

使用道具 举报

发表于 2004-6-30 13:34:22 | 显示全部楼层
应该先或非C8将C8清除再或C7
回复

使用道具 举报

 楼主| 发表于 2004-6-30 13:56:08 | 显示全部楼层
呵呵,明白了!谢谢!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2025-2-15 13:51 , Processed in 0.039014 second(s), 16 queries .

© 2001-2025 Discuz! Team. Powered by Discuz! X3.5.

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