代码:
1 /*
2 serialTest.c:
3 */
4
5 #include <stdio.h>
6 #include <string.h>
7 #include <errno.h>
8 #include <pthread.h>
9 #include <wiringPi.h>
10 #include <wiringSerial.h>
11 #include <stdlib.h>
12
13
14 int fd;
15
16 void* Sendhandler()
17 {
18 char *sendbuf;
19 sendbuf = (char *)malloc(32*sizeof(32));
20
21 while(1){
22
23 memset(sendbuf,'\0',32);
24 scanf("%s",sendbuf);
25
26 while(*sendbuf){
27
28 serialPutchar(fd,*sendbuf++);
29
30 }
31
32
33 }
34
35
36
37 }
38
39
40 int main ()
41 {
42 int count ;
43 unsigned int nextTime ;
44
45 pthread_t idsend;
46 if ((fd = serialOpen ("/dev/ttyS5", 115200)) < 0)
47 {
48 fprintf (stderr, "Unable to open serial device: %s\n", strerror (errno)) ;
49 return 1 ;
50 }
51
52 pthread_create(&idsend,NULL,Sendhandler,NULL);
53
54 if (wiringPiSetup () == -1)
55 {
56 fprintf (stdout, "Unable to start wiringPi: %s\n", strerror (errno)) ;
57 return 1 ;
58 }
59
60 while(1){
61 while (serialDataAvail (fd))
62 {
63 printf ("%c", serialGetchar(fd)) ;
64 fflush (stdout) ;
65 }
66 }
67
68 printf ("\n") ;
69 return 0 ;
70 }
~
代码逻辑:
打开serial->新建一个线程用来发送数据(主程序用来收数据)
运行结果展示:
