- 论坛徽章:
- 0
|
网上的例子:看了应该懂 关键是termios结构的设置,man tcgetattr
- #include <stdio.h>
- #include <fcntl.h>
- #include <stdlib.h>
- #include <string.h>
- #include <errno.h>
- #include <termios.h>
- #include <unistd.h>
- #include <sys/types.h>
- #define TRUE 1
- #define FALSE 0
- 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)
- {
- struct termios term;
- int i = 0;
- int status = 0;
- tcgetattr(fd,&term);//取得属性,终端也是文件,只是一种特殊的文件
- for(i; i<sizeof(speed_arr)/sizeof(speed_arr[0]);i++)
- {
- if(speed == name_arr[i])
- {
- tcflush(fd,TCIOFLUSH);
- cfsetispeed(&term,speed_arr[i]);
- cfsetospeed(&term,speed_arr[i]);
-
- if(tcsetattr(fd,TCSANOW,&term) == -1);//只要any成功用tcgetattr再作判断
- {
- printf("tcsetattr error,allofthem not set:%s\n",strerror(errno));
- exit(errno);
- }
- tcfflush(fd,TCIOFLUSH);
- }
-
- }
- }
- int set_parity(int fd,int databits,int stopbits,int parity)
- {
- struct termios term;
- if(tcgetattr(fd,&term) == -1)
- {
- printf("tcgetattr error:%s\n",strerror(errno));
- exit(errno);
- }
- term.c_cflag &= ~CSIZE;//character size mask
- switch(databits)
- {
- case 7:
- term.c_cflag |= CS7;
- break;
- case 8:
- term.c_cflag |= CS8;
- break;
- default:
- fprintf(stderr,"Unsupported data size\n");
- return (FALSE);
- }
- switch(stopbits)
- {
- case 1:
- term.c_cflag &= ~CSTOPB;
- break;
- case 2:
- term.c_cflag |= CSTOPB;
- break;
- default:
- fprintf(stderr,"Unsupported stop bits\n");
- return (FALSE);
- }
- switch(parity)
- {
- case 'n':
- case 'N':
- term.c_cflag &= ~PARENB; /* Clear parity enable */
- term.c_iflag &= ~INPCK; /* Enable parity checking */
- break;
- case 'o':
- case 'O':
- term.c_cflag |= (PARODD | PARENB); /* 设置为奇效验*/
- term.c_iflag |= INPCK; /* Disnable parity checking */
- break;
- case 'e':
- case 'E':
- term.c_cflag |= PARENB; /* Enable parity */
- term.c_cflag &= ~PARODD; /* 转换为偶效验*/
- term.c_iflag |= INPCK; /* Disnable parity checking */
- break;
- case 'S':
- case 's': /*as no parity*/
- term.c_cflag &= ~PARENB;
- term.c_cflag &= ~CSTOPB;
- break;
- default:
- fprintf(stderr,"Unsupported parity\n");
- return (FALSE);
- }
- if (parity != 'n')
- term.c_iflag |= INPCK;
- term.c_cc[VTIME] = 150; // 15 seconds
- term.c_cc[VMIN] = 0;
- tcflush(fd,TCIFLUSH); /* Update the term and do it NOW */
- if (tcsetattr(fd,TCSANOW,&term) != 0)
- {
- perror("SetupSerial 3");
- return (FALSE);
- }
- return (TRUE);
- }
- int OpenDev(char *Dev)
- {
- int fd;
- fd = open( Dev, O_RDWR ); //| O_NOCTTY | O_NDELAY
- if (-1 == fd)
- { /*设置数据位数*/
- perror("Can't Open Serial Port");
- return -1;
- }
- else
- fcntl(fd,F_SETFL,0);
-
- return fd;
- }
- int main(int argc, char **argv)
- {
- int fd;
- int nread;
- char buff[512];
- char *dev ="/dev/ttyS1";
- fd = OpenDev(dev);
- if (fd>0)
- set_speed(fd,19200);
- else
- {
- printf("Can't Open Serial Port!\n");
- exit(0);
- }
- if (set_parity(fd,8,1,'N')== FALSE)
- {
- printf("Set Parity Error\n");
- exit(1);
- }
- while(1)
- {
- while((nread = read(fd,buff,512))>0)
- {
- printf("\nLen %d\n",nread);
- buff[nread+1]='\0';
- printf("\n%s",buff);
- }
- }
- //close(fd);
- //exit(0);
- }
复制代码
[ 本帖最后由 lsmup 于 2007-4-6 23:59 编辑 ] |
|