- /*
- ============================================================================
- Name : testio.c
- Author :
- Version :
- Copyright : Your copyright notice
- Description : Hello World in C, Ansi-style
- ============================================================================
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <termios.h>
- #include <errno.h>
- #include <string.h>
- #define BUFF_SIZE 1024
- int set_option(int fd,int baudrate,int bits,char event,int stops);
- int main(void) {
- unsigned char buff[BUFF_SIZE];
- ssize_t rd;
- fd_set rdset;
- int fd = open("/dev/ttyS0",O_RDWR|O_NOCTTY|O_NDELAY);
- if(fd < 0){
- //printf("open file test.txt fail!\n");
- perror("open file");
- return 1;
- }
- else
- {
- printf("opened serial port id %d\n",fd);
- }
-
- int fcr = fcntl(fd,F_SETLK,0);
- if(fcr < 0)
- {
- perror("ftcnl:");
- }
- else
- {
- printf("ftcnl %d\n",fcr);
- }
- if(isatty(STDIN_FILENO)==0)
- {
- printf("standard input is not a termial device");
- }
- else
- {
- printf("isatty\n");
- }
- set_option(fd,115200,8,'N',1);
- memset(buff,0,BUFF_SIZE);
- while(1)
- {
- FD_ZERO(&rdset);
- FD_SET(fd,&rdset);
- FD_SET(0,&rdset);
-
- if(select(fd+1,&rdset,NULL,NULL,NULL) < 0)
- {
- perror("select");
- break;
- }
-
- if(FD_ISSET(0,&rdset))
- {
- rd = read(fd,buff,BUFF_SIZE);
- if(rd > 0){
- buff[rd] = 0;
- //printf("after seek just read data %d bytes\n",rd);
- //printf("%s",buff);
- write(1,buff,rd);
- }
- }
- if(FD_ISSET(fd,&rdset))
- {
- rd = read(0,buff,BUFF_SIZE);
- if(rd > 0){
- buff[rd] = 0;
- //printf("after seek just read data %d bytes\n",rd);
- //printf("%s",buff);
- write(fd,buff,rd);
- }
- }
- }
- close(fd);
- return EXIT_SUCCESS;
- }
- int set_option(int fd,int baudrate,int bits,char event,int stops)
- {
- struct termios newtio,oldtio;
- if(tcgetattr(fd,&oldtio) != 0)
- {
- perror("get attr");
- return -1;
- }
- bzero(&newtio,sizeof(newtio));
- newtio.c_cflag |= CLOCAL | CREAD;
- newtio.c_cflag &= ~CSIZE;
- switch(bits)
- {
- case 7: newtio.c_cflag |= CS7;
- break;
- case 8: newtio.c_cflag |= CS8;
- break;
- }
- switch(event)
- {
- case 'N': newtio.c_cflag &= ~PARENB;
- break;
- case 'O': newtio.c_cflag |= PARENB;
- newtio.c_cflag |= PARODD;
- newtio.c_cflag |= (INPCK | ISTRIP);
- break;
- case 'E': newtio.c_cflag |= PARENB;
- newtio.c_cflag |= ~PARODD;
- newtio.c_cflag |= (INPCK | ISTRIP);
- break;
-
- }
- switch(baudrate)
- {
- case 9600:
- cfsetispeed(&newtio,B9600);
- cfsetospeed(&newtio,B9600);
- break;
- case 115200:
- default:
- cfsetispeed(&newtio,B115200);
- cfsetospeed(&newtio,B115200);
- break;
-
- }
- if(stops == 1)
- {
- newtio.c_cflag &= ~CSTOPB;
- }
- else if(stops == 2)
- {
- newtio.c_cflag |= CSTOPB;
- }
- newtio.c_cc[VTIME] = 0;
- newtio.c_cc[VMIN] = 0;
- tcflush(fd,TCIFLUSH);
- if(tcsetattr(fd,TCSANOW,&newtio) != 0)
- {
- perror("set attr");
- return -1;
- }
- printf("set done!\n");
- return 0;
- }
在ubuntu10.4下测试可用
阅读(1666) | 评论(0) | 转发(2) |