- 论坛徽章:
- 0
|
串口是这样初始化的:
- // stalk_serial_init: initialize serial port
- // return: open file descriptor
- int stalk_serial_init(char* dev, unsigned int speed)
- {
- int fd;
- struct termios term;
- if ( (fd = open(dev, O_RDWR | O_NOCTTY | O_NDELAY)) < 0)
- errx(1, "open serial device error");
- if (fcntl(fd, F_SETFL, 0) < 0)
- errx(1, "fcntl error");
- if (tcgetattr(fd, &term) < 0)
- errx(1, "tcgetattr error");
- term.c_cflag &= ~(CSIZE | CSTOPB);
- term.c_cflag |= (CS8 | CREAD | CLOCAL | CRTSCTS);
- term.c_iflag = 0;
- term.c_oflag = 0;
- term.c_lflag = 0;
- term.c_cc[VMIN] = 1;
- term.c_cc[VTIME] = 0;
- cfsetispeed(&term, speed);
- cfsetospeed(&term, speed);
- if (tcsetattr(fd, TCSANOW, &term) < 0)
- errx(1, "tcsetattr error");
- return fd;
- }
复制代码
其中 speed 是串口速度,值为 termios.h 中宏定义的诸如 B9600 之类的东西
[ 本帖最后由 isjfk 于 2006-9-6 12:46 编辑 ] |
|