-
Notifications
You must be signed in to change notification settings - Fork 7
/
tcom.c
executable file
·132 lines (91 loc) · 1.79 KB
/
tcom.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include<sys/types.h>
#include<errno.h>
#include<string.h>
#include<termios.h>
#include<sys/stat.h>
#include<unistd.h>
#include<fcntl.h>
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
//#define BLEN 1024
#define BLEN BUFSIZ
int fd;
int flag=0;
char rbuf[BLEN] = {0};
char wbuf[BLEN] = {0};
void *read_com(void *ptr)
{
int nread=0;
int tnread=0;
int cnt=0;
while(1)
{
if(flag ==1)
{
printf("read com\r\n");
nread = read(fd,rbuf,sizeof(rbuf)-2);
// printf("nread is %d\r\n",nread);
printf("rbuf is %s\r\n",rbuf);
memset(rbuf,sizeof(rbuf),0);
if(nread)
{
tnread += nread;
nread =0;
// printf("tnread is %d\r\n",tnread);
}
flag=0;
}
}
}
void *write_com(void *ptr)
{
int nwrite=0;
int cnt=0;
wbuf[0] = 'h';
wbuf[1] = 'e';
wbuf[2] = 'l';
wbuf[3] = 'l';
wbuf[4] = 'o';
wbuf[5] = '\r';
wbuf[6] = '\n';
wbuf[7] = '\0';
while(1)
{
printf("write com\r\n");
nwrite= write(fd,wbuf,strlen(wbuf));
//printf("nwrite is %d\r\n",nwrite);
memset(wbuf,sizeof(wbuf),0);
flag =1;
sleep(6);
cnt++;
if(cnt > 3)
{
//cnt=0;
//fsync(fd);
sleep(6);
break;
}
}
}
int main(int argc, char** argv)
{
fd = open("/dev/ttyS7",O_RDWR);
if(fd == -1)
{
perror("can not open serial port failed\n");
exit(EXIT_FAILURE);
}
printf("fd is %d\n",fd);
pthread_t thread1,thread2;
int iret1,iret2;
iret1 = pthread_create(&thread1,NULL,read_com,NULL);
iret2 = pthread_create(&thread2,NULL,write_com,NULL);
pthread_join(thread1,NULL);
pthread_join(thread1,NULL);
printf("thread 1 returns %d\n",iret1);
printf("thread 2 returns %d\n",iret2);
close(fd);
printf("done\n");
return 0;
}