1 #include <stdio.h>
2 #include <string.h>
3 #include <sys/types.h>
4 #include <errno.h>
5 #include <sys/stat.h>
6 #include <fcntl.h>
7 #include <unistd.h>
8 #include <termios.h>
9 #include <stdlib.h>
10
11 int set_opt(int fd,int nSpeed, int nBits, char nEvent, int nStop)
12 {
13 struct termios newtio,oldtio;
14 if ( tcgetattr( fd,&oldtio) != 0)
15 {
16 perror("SetupSerial 1");
17 return -1;
18 }
19 bzero( &newtio, sizeof( newtio ) );
20 newtio.c_cflag |= CLOCAL | CREAD;
21 newtio.c_cflag &= ~CSIZE;
22
23 switch( nBits )
24 {
25 case 7:
26 newtio.c_cflag |= CS7;
27 break;
28 case 8:
29 newtio.c_cflag |= CS8;
30 break;
31 }
32
33 switch( nEvent )
34 {
35 case 'O': //奇校验
36 newtio.c_cflag |= PARENB;
37 newtio.c_cflag |= PARODD;
38 newtio.c_iflag |= (INPCK | ISTRIP);
39 break;
40 case 'E': //偶校验
41 newtio.c_iflag |= (INPCK | ISTRIP);
42 newtio.c_cflag |= PARENB;
43 newtio.c_cflag &= ~PARODD;
44 break;
45 case 'N': //无校验
46 newtio.c_cflag &= ~PARENB;
47 break;
48 }
49
50 switch( nSpeed )
51 {
52 case 2400:
53 cfsetispeed(&newtio, B2400);
54 cfsetospeed(&newtio, B2400);
55 break;
56 case 4800:
57 cfsetispeed(&newtio, B4800);
58 cfsetospeed(&newtio, B4800);
59 break;
60 case 9600:
61 cfsetispeed(&newtio, B9600);
62 cfsetospeed(&newtio, B9600);
63 break;
64 case 115200:
65 cfsetispeed(&newtio, B115200);
66 cfsetospeed(&newtio, B115200);
67 break;
68 default:
69 cfsetispeed(&newtio, B9600);
70 cfsetospeed(&newtio, B9600);
71 break;
72 }
73 if( nStop == 1 )
74 {
75 newtio.c_cflag &= ~CSTOPB;
76 }
77 else if ( nStop == 2 )
78 {
79 newtio.c_cflag |= CSTOPB;
80 }
81 newtio.c_cc[VTIME] = 0;
82 newtio.c_cc[VMIN] = 0;
83 tcflush(fd,TCIFLUSH);
84 if((tcsetattr(fd,TCSANOW,&newtio))!=0)
85 {
86 perror("com set error");
87 return -1;
88 }
89 printf("set done!\n");
90 return 0;
91 }
92
93 int open_port(int fd,int comport)
94 {
95 char *dev[]={"/dev/ttySAC0","/dev/ttySAC1","/dev/ttySAC3","/dev/ttySAC4"};
96 long vdisable;
97 if (comport==1)
98 {
99 fd = open( dev[comport - 1], O_RDWR|O_NOCTTY|O_NDELAY);
100 if (-1 == fd)
101 {
102 perror("Can't Open Serial Port");
103 return(-1);
104 }
105 else
106 {
107 printf("%s .....\n", dev[comport - 1]);
108 }
109 }
110 else if(comport==2)
111 { fd = open( dev[comport - 1], O_RDWR|O_NOCTTY|O_NDELAY);
112 if (-1 == fd)
113 {
114 perror("Can't Open Serial Port");
115 return(-1);
116 }
117 else
118 {
119 printf("%s .....\n", dev[comport - 1]);
120 }
121 }
122 else if (comport==3)
123 {
124 fd = open( dev[comport - 1], O_RDWR|O_NOCTTY|O_NDELAY);
125 if (-1 == fd)
126 {
127 perror("Can't Open Serial Port");
128 return(-1);
129 }
130 else
131 {
132 printf("%s .....\n", dev[comport - 1]);
133 }
134 }
135 if(fcntl(fd, F_SETFL, 0)<0)
136 {
137 printf("fcntl failed!\n");
138 }
139 else
140 {
141 printf("fcntl=%d\n",fcntl(fd, F_SETFL,0));
142 }
143 if(isatty(STDIN_FILENO)==0)
144 {
145 printf("standard input is not a terminal device\n");
146 }
147 else
148 {
149 printf("isatty success!\n");
150 }
151 printf("fd-open=%d\n",fd);
152 return fd;
153 }
154
155 int main(void)
156 {
157 int fd;
158 int nread,i;
159 char buff[]="Hello\n";
160
161 if((fd=open_port(fd,3))<0)
162 {
163 perror("open_port error");
164 return;
165 }
166 if((i=set_opt(fd,115200,8,'N',1))<0)
167 {
168 perror("set_opt error");
169 return;
170 }
171 printf("fd=%d\n",fd);
172 while(1){
173 errno = 0;
174 nread=read(fd,buff,8);
175 if(0 < nread){
176 printf("nread=%d,%s\n",nread,buff);
177 }
178 }
179 close(fd);
180 return;
181 }