多线程监听串口

  1 #include <stdio.h>
  2 #include <unistd.h>
  3 #include <stdlib.h>
  4 #include <error.h>
  5 #include <sys/types.h>  
  6 #include <sys/stat.h> 
  7 #include <fcntl.h>     
  8 #include <termios.h>    
  9 #include <sys/time.h>
 10 #include <signal.h>
 11 #include <string.h>
 12 #include <poll.h>
 13 #include <pthread.h>
 14 #include <sys/select.h>  
 15 #include <time.h> 
 16 
 17 
 18 int speed_arr[] = {B115200, B38400, B19200, B9600, B4800, B2400, B1200, B300,
 19         B38400, B19200, B9600, B4800, B2400, B1200, B300, };
 20 int name_arr[] = {115200, 38400,  19200,  9600,  4800,  2400,  1200,  300,
 21         38400,  19200,  9600, 4800, 2400, 1200,  300, };
 22 
 23 
 24 int fd_uart1;
 25 int fd_uart2;
 26 int fd_uart3;
 27 int fd_uart4;
 28 
 29 #define UART1  "/dev/ttysWK0"
 30 #define UART2  "/dev/ttysWK1"//"/dev/ttymxc1"
 31 #define UART3  "/dev/ttysWK2"//"/dev/ttymxc2"
 32 #define UART4  "/dev/ttysWK3"//"/dev/ttymxc3"
 33 void delay(int i)
 34 {
 35     int t,k;
 36     for(t=i;t>0;t--)
 37         for(k=50000;k>0;k--);
 38 }
 39 
 40 int init_serial(char *device,int speed)  
 41 {  
 42     int serial_fd = 0,i; 
 43     serial_fd = open(device, O_RDWR | O_NOCTTY | O_NDELAY);  
 44     if (serial_fd < 0) {  
 45         perror("open");  
 46         return -1;  
 47     }  
 48       
 49     //串口主要设置结构体termios <termios.h>  
 50     struct termios options;  
 51       
 52     /**1. tcgetattr函数用于获取与终端相关的参数。 
 53     *参数fd为终端的文件描述符,返回的结果保存在termios结构体中 
 54     */  
 55     tcgetattr(serial_fd, &options);  
 56     /**2. 修改所获得的参数*/  
 57     options.c_cflag |= (CLOCAL | CREAD);//设置控制模式状态,本地连接,接收使能  
 58     options.c_cflag &= ~CSIZE;//字符长度,设置数据位之前一定要屏掉这个位  
 59     options.c_cflag &= ~CRTSCTS;//无硬件流控  
 60     options.c_cflag |= CS8;//8位数据长度  
 61     options.c_cflag &= ~CSTOPB;//1位停止位  
 62     options.c_iflag |= IGNPAR;//无奇偶检验位  
 63     options.c_oflag = 0; //输出模式  
 64     options.c_lflag = 0; //不激活终端模式  
 65     options.c_cc[VTIME] = 1; // 0.1seconds
 66     options.c_cc[VMIN] = 0;
 67     //cfsetospeed(&options, B115200);//设置波特率  
 68     for ( i= 0;  i < sizeof(speed_arr) / sizeof(int);  i++)
 69        {
 70                if  (speed == name_arr[i])
 71                {
 72                 /*  设置串口的波特率 */
 73                 cfsetispeed(&options, speed_arr[i]);
 74                 cfsetospeed(&options, speed_arr[i]);
 75             }
 76        }  
 77     /**3. 设置新属性,TCSANOW:所有改变立即生效*/  
 78     tcflush(serial_fd, TCIFLUSH);//溢出数据可以接收,但不读  
 79     tcsetattr(serial_fd, TCSANOW, &options);  
 80     return serial_fd;  
 81 }  
 82   
 83 /** 
 84 *串口发送数据 
 85 *@fd:串口描述符 
 86 *@data:待发送数据 
 87 *@datalen:数据长度 
 88 */  
 89 int uart_send(int fd, char *data, int datalen)  
 90 {  
 91     int len = 0;  
 92     len = write(fd, data, datalen);//实际写入的长度  
 93     if(len == datalen) {  
 94         return len;  
 95     } else {  
 96         tcflush(fd, TCOFLUSH);//TCOFLUSH刷新写入的数据但不传送  
 97         return -1;  
 98     }  
 99       
100     return 0;  
101 }  
102   
103 
104 int uart_recv2(int fd, char *data, int datalen)  
105 {  
106     int len;
107     len = read(fd, data, datalen);  
108     //printf("len = %d
", len);  
109     return len;  
110 } 
111 
112 /*******************多线程************************/
113 
114 char buf1[10];
115 char buf2[10];
116 char buf3[10];
117 char buf4[10];
118 
119 
120 
121 
122 void readuart1(void *vptr)
123 {
124     int len;
125     printf("uart1
");
126     while(1){
127         len =uart_recv2(fd_uart1,buf1,10);
128         if(len>0)
129             printf("uart1=%s
",buf1);
130     }
131 }
132 void readuart2(void *vptr)
133 {
134     int len;
135     printf("uart2
");
136     while(1){
137         len =uart_recv2(fd_uart2,buf2,10);
138         if(len>0)
139             printf("uart2=%s
",buf2);
140     }
141 }
142 void readuart3(void *vptr)
143 {
144     int len;
145     printf("uart3
");
146     while(1){
147         len =uart_recv2(fd_uart3,buf3,10);
148         if(len>0)
149             printf("uart3=%s
",buf3);
150     }
151 }
152 void readuart4(void *vptr)
153 {
154     int len;
155     printf("uart4
");
156     while(1){
157         len =uart_recv2(fd_uart4,buf4,10);
158         if(len>0)
159             printf("uart4=%s
",buf4);
160     }
161 }
162 void setTimer(int seconds, int useconds)  
163 {  
164         struct timeval temp;  
165   
166         temp.tv_sec = seconds;  
167         temp.tv_usec = useconds;  
168   
169         select(0, NULL, NULL, NULL, &temp);  
170           
171   
172         return ;  
173 }  
174   
175 
176 void time_300ms(void *vptr)
177 {
178     printf("timer start!
");
179     while(1){
180         setTimer(0, 300000); 
181         printf("timer
");
182     }
183 }
184 
185 int main(int argc, char **argv)     
186 {  
187        fd_uart1=init_serial(UART1,115200);
188         delay(200);
189      fd_uart2=init_serial(UART2,115200);
190      delay(200);
191      fd_uart3=init_serial(UART3,115200);
192      delay(200);
193      fd_uart4=init_serial(UART4,115200);
194      delay(200);
195     char buf[]="hello123456
";  
196     
197     memset(buf1,0,10);
198     memset(buf2,0,10);
199     memset(buf3,0,10);
200     memset(buf4,0,10);
201     
202     uart_send(fd_uart1, buf, 10);
203     usleep(50000);
204     uart_send(fd_uart2, buf, 10); 
205     usleep(50000);
206     uart_send(fd_uart3, buf, 10);
207     usleep(50000);
208     uart_send(fd_uart4, buf, 10); 
209     usleep(50000);
210     pthread_t tid1,tid2,tid3,tid4,tid_timer;
211     pthread_create(&tid1,NULL,&readuart1,NULL);
212     pthread_create(&tid2,NULL,&readuart2,NULL);
213     pthread_create(&tid3,NULL,&readuart3,NULL);
214     pthread_create(&tid4,NULL,&readuart4,NULL);
215     //pthread_create(&tid_timer,NULL,&time_300ms,NULL);
216   while(1){
217         sleep(1);
218         //uart_send(fd_uart1, buf1, 10);
219         //uart_send(fd_uart2, buf2, 10);
220         //uart_send(fd_uart3, buf3, 10);
221         //uart_send(fd_uart4, buf4, 10);
222       }
223       pthread_join(&tid1,NULL);
224     pthread_join(&tid2,NULL);
225     pthread_join(&tid3,NULL);
226     pthread_join(&tid4,NULL);
227     //pthread_join(&tid_timer,NULL);
228     
229     close(fd_uart1); 
230     close(fd_uart2); 
231     close(fd_uart3); 
232     close(fd_uart4); 
233     return 0;  
234 }
View Code

一个线程监听一路串口的收,可以在线程内使用select机制,减少IO操作

转载请注明出处:http://www.cnblogs.com/tla001/ 一起学习,一起进步
原文地址:https://www.cnblogs.com/tla001/p/5541560.html