-
Notifications
You must be signed in to change notification settings - Fork 7
/
read_celsius.c
executable file
·198 lines (146 loc) · 3.25 KB
/
read_celsius.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include<sys/types.h>
#include<errno.h>
#include<string.h>
#include<stdint.h>
#include<termios.h>
#include<sys/stat.h>
#include<sys/select.h>
#include<unistd.h>
#include<fcntl.h>
#include<stdio.h>
#include<stdlib.h>
#include <termios.h>
//#define BLEN 128
#define BLEN BUFSIZ
int main(int argc, char** argv)
{
int fd;
int wfd;
int rfd;
fd_set fds;
struct timeval tv={5,0};
int retval;
int scnt=0;
int nread,i;
int tnread;
int nwrite=0;
int cnt=0;
int s=0;
int tlen=0;
char wbuff[64] = "Hello\n";
uint8_t recbuf[BLEN]={0};
uint8_t *precbuf=recbuf;
int cnt_rec=0;
int c;
char rbuff[BLEN] = {0};
// tv.tv_sec =5;
// tv.tv_usec = 500000;
// fd = open("/dev/ttyUSB0",O_RDWR|O_NOCTTY|O_NDELAY);
// fd = open("/dev/ttyUSB0",O_RDWR|O_NOCTTY);
//fd = open("/dev/ttyUSB0",O_RDONLY);
fd = open("/dev/ttyUSB0",O_RDWR);
// fd = open("/dev/ttyS8",O_RDWR);
// fd = open("/dev/ttyS4",O_RDWR);
// wfd = open("acom.log",O_WRONLY);
// rfd = open(argv[1],O_RDONLY);
if(fd == -1)
{
perror("can not open serial port failed\n");
exit(EXIT_FAILURE);
}
printf("fd is %d\n",fd);
//tcflush(fd,TCIFLUSH);
tcflush(fd,TCIOFLUSH);
cnt_rec=0;
char cmd[128] = "dht11";
//tlen = write(fd,cmd,6);
#if 0
tlen = write(fd,"d",1);
tlen = write(fd,"h",1);
tlen = write(fd,"t",1);
tlen = write(fd,"1",1);
tlen = write(fd,"1",1);
//sleep(1);
#endif
// printf("tx:the amount is %d\n",tlen);
memset(recbuf,0,sizeof(recbuf));
tlen = write(fd,cmd,5);
while(1)
{
FD_ZERO(&fds);
FD_SET(fd,&fds);
tv.tv_sec =5;
tv.tv_usec = 500000;
//sleep(1);
// tlen = write(fd,cmd,5);
retval = select(fd+1,&fds,NULL,NULL,&tv);
//retval = select(fd+1,&fds,NULL,NULL,NULL);
if(retval == -1)
{
printf("error:select\n");
close(fd);
exit(EXIT_FAILURE);
}
else if(retval)
{
if(FD_ISSET(fd,&fds))
{
// memset(recbuf,0,sizeof(recbuf));
tlen =read(fd,precbuf,BLEN);
// printf("m is %s\n",precbuf);
// printf("mlen is %d\n",tlen);
precbuf +=tlen;
//tlen =read(fd,recbuf,8);
#if 0
printf("rx:the amount is %d\n",tlen);
printf("%d\n",recbuf[0]);
printf("tlen is %d\n",tlen);
#endif
// printf("message is %s\n",recbuf);
//printf("msg is %c %c %c %c\n",recbuf[0],recbuf[1],recbuf[2],recbuf[3]);
// tlen = write(fd,cmd,6);
}
}
#if 1
else
{
precbuf = recbuf;
printf("message is %s\n",precbuf);
#if 0
int i=0;
for(i=0;i<128;i++)
printf("%c",precbuf[i]);
printf("\n");
#endif
memset(recbuf,0,sizeof(recbuf));
precbuf = recbuf;
tlen = write(fd,cmd,6);
printf("send cmd %d\n",scnt++);
//printf("tlen is %d\n",tlen);
//exit(EXIT_FAILURE);
}
#endif
}
#if 0
while((nread =read(rfd,recbuf,BLEN)) > 0)
{
nwrite= write(fd,recbuf,nread);
printf("nwrite is %d\n",nwrite);
tnread=0;
while(tnread != nwrite)
{
printf("---tnread is %d---\n",tnread);
nread = read(fd,rbuff,BLEN);
tnread +=nread;
printf("---nread is %d---\n",nread);
write(wfd,rbuff,nread);
}
}
close(wfd);
close(rfd);
close(fd);
#endif
close(fd);
printf("done\n");
return 0;
}