- 论坛徽章:
- 0
|
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <termios.h>
- #include <errno.h>
- #include <sys/select.h>
- #include <time.h>
- #include <sys/time.h>
- #define FALSE -1
- #define TRUE 0
- #define NET_PORT 19988
- int speed_arr[] = { B38400, B19200, B9600, B4800, B2400, B1200, B300,
- B38400, B19200, B9600, B4800, B2400, B1200, B300, };
- int name_arr[] = {38400, 19200, 9600, 4800, 2400, 1200, 300, 38400,
- 19200, 9600, 4800, 2400, 1200, 300, };
- void set_speed(int fd, int speed)
- {
- int i;
- int status;
- struct termios Opt;
- tcgetattr(fd, &Opt);
- for ( i= 0; i < sizeof(speed_arr) / sizeof(int); i++) {
- if (speed == name_arr[i]) {
- tcflush(fd, TCIOFLUSH);
- cfsetispeed(&Opt, speed_arr[i]);
- cfsetospeed(&Opt, speed_arr[i]);
- status = tcsetattr(fd, TCSANOW, &Opt);
- if (status != 0) {
- perror("tcsetattr fd1");
- return;
- }
- tcflush(fd,TCIOFLUSH);
- }
- }
- }
- int set_parity(int fd,int databits,int stopbits,int parity)
- {
- struct termios options;
- if ( tcgetattr( fd,&options) != 0) {
- perror("SetupSerial 1");
- return(FALSE);
- }
- options.c_cflag &= ~CSIZE;
- switch (databits) /*设置数据位数*/
- {
- case 7:
- options.c_cflag |= CS7;
- break;
- case 8:
- options.c_cflag |= CS8;
- break;
- default:
- fprintf(stderr,"Unsupported data size\n");
- return (FALSE);
- }
- switch (parity)
- {
- case 'n':
- case 'N':
- options.c_cflag &= ~PARENB; /* Clear parity enable */
- options.c_iflag &= ~INPCK; /* Enable parity checking */
- break;
- case 'o':
- case 'O':
- options.c_cflag |= (PARODD | PARENB); /* 设置为奇效验*/
- options.c_iflag |= INPCK; /* Disnable parity checking */
- break;
- case 'e':
- case 'E':
- options.c_cflag |= PARENB; /* Enable parity */
- options.c_cflag &= ~PARODD; /* 转换为偶效验*/
- options.c_iflag |= INPCK; /* Disnable parity checking */
- break;
- case 'S':
- case 's': /*as no parity*/
- options.c_cflag &= ~PARENB;
- options.c_cflag &= ~CSTOPB;
- break;
- default:
- fprintf(stderr,"Unsupported parity\n");
- return (FALSE);
- }
- /* 设置停止位*/
- switch (stopbits)
- {
- case 1:
- options.c_cflag &= ~CSTOPB;
- break;
- case 2:
- options.c_cflag |= CSTOPB;
- break;
- default:
- fprintf(stderr,"Unsupported stop bits\n");
- return (FALSE);
- }
- /* Set input parity option */
- if (parity != 'n' && parity != 'N')
- options.c_iflag |= INPCK;
- tcflush(fd,TCIFLUSH);
- options.c_cc[VTIME] = 30; /* 设置超时3 seconds*/
- options.c_cc[VMIN] = 0; /* Update the options and do it NOW */
- /**
- *如果不是开发终端之类的,只是串口传输数据,
- *而不需要串口来处理,那么使用原始模式(Raw Mode)方式来通讯
- **/
- options.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON | IXOFF | IXANY);
- options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG |IEXTEN);
- options.c_oflag &= ~OPOST;
-
- if (tcsetattr(fd,TCSANOW,&options) != 0)
- {
- perror("SetupSerial 3");
- return (FALSE);
- }
- return (TRUE);
- }
- int main()
- {
- int fd;
- fd = open("/dev/console", O_RDWR);
- set_speed(fd,9600);
- set_parity(fd,8,1,'N');
- write(fd, "test", 4);
- close(fd);
- }
复制代码 |
|