-
Notifications
You must be signed in to change notification settings - Fork 1
/
linux3600.c
292 lines (258 loc) · 7.32 KB
/
linux3600.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/* open3600 - linux3600 library functions
* This file contains the common functions that are unique to
* Linux. The entire file is ignored in case of Windows
*
* Version 0.01
*
* Control WS3600 weather station
*
* Copyright 2003-2005, Kenneth Lavrsen, Grzegorz Wisniewski, Sander Eerkes
* This program is published under the GNU General Public license
*/
#define DEBUG 0
#include "eeprom.h"
#include "mcdelay.h"
#include "mcdelay.c"
/********************************************************************
* open_weatherstation, Windows version
*
* Input: devicename (COM1, COM2 etc)
*
* Returns: Handle to the weatherstation (type WEATHERSTATION)
*
********************************************************************/
WEATHERSTATION open_weatherstation (char *device) {
WEATHERSTATION ws;
struct termios adtio;
unsigned char buffer[BUFFER_SIZE];
long i;
print_log(1,"open_weatherstation");
//calibrate nanodelay function
microdelay_init(1);
//Setup serial port
if ((ws = open(device, O_RDWR | O_NOCTTY)) < 0)
{
printf("\nUnable to open serial device %s\n", device);
exit(EXIT_FAILURE);
}
if ( flock(ws, LOCK_EX) < 0 ) {
perror("\nSerial device is locked by other program\n");
exit(EXIT_FAILURE);
}
//We want full control of what is set and simply reset the entire adtio struct
memset(&adtio, 0, sizeof(adtio));
// Serial control options
adtio.c_cflag &= ~PARENB; // No parity
adtio.c_cflag &= ~CSTOPB; // One stop bit
adtio.c_cflag &= ~CSIZE; // Character size mask
adtio.c_cflag |= CS8; // Character size 8 bits
adtio.c_cflag |= CREAD; // Enable Receiver
//adtio.c_cflag &= ~CREAD; // Disable Receiver
adtio.c_cflag &= ~HUPCL; // No "hangup"
adtio.c_cflag &= ~CRTSCTS; // No flowcontrol
adtio.c_cflag |= CLOCAL; // Ignore modem control lines
// Baudrate, for newer systems
cfsetispeed(&adtio, BAUDRATE);
cfsetospeed(&adtio, BAUDRATE);
// Serial local options: adtio.c_lflag
// Raw input = clear ICANON, ECHO, ECHOE, and ISIG
// Disable misc other local features = clear FLUSHO, NOFLSH, TOSTOP, PENDIN, and IEXTEN
// So we actually clear all flags in adtio.c_lflag
adtio.c_lflag = 0;
// Serial input options: adtio.c_iflag
// Disable parity check = clear INPCK, PARMRK, and ISTRIP
// Disable software flow control = clear IXON, IXOFF, and IXANY
// Disable any translation of CR and LF = clear INLCR, IGNCR, and ICRNL
// Ignore break condition on input = set IGNBRK
// Ignore parity errors just in case = set IGNPAR;
// So we can clear all flags except IGNBRK and IGNPAR
adtio.c_iflag = IGNBRK|IGNPAR;
// Serial output options
// Raw output should disable all other output options
adtio.c_oflag &= ~OPOST;
adtio.c_cc[VTIME] = 10; // timer 1s
adtio.c_cc[VMIN] = 0; // blocking read until 1 char
if (tcsetattr(ws, TCSANOW, &adtio) < 0)
{
printf("Unable to initialize serial device");
exit(0);
}
tcflush(ws, TCIOFLUSH);
for (i = 0; i < 448; i++) {
buffer[i] = 'U';
}
write(ws, buffer, 448);
set_DTR(ws,0);
set_RTS(ws,0);
i = 0;
do {
sleep_short(10);
i++;
} while (i < INIT_WAIT && !get_DSR(ws));
if (i == INIT_WAIT)
{
print_log(2,"Connection timeout 1");
printf ("Connection timeout\n");
close_weatherstation(ws);
exit(0);
}
i = 0;
do {
sleep_short(10);
i++;
} while (i < INIT_WAIT && get_DSR(ws));
if (i != INIT_WAIT) {
set_RTS(ws,1);
set_DTR(ws,1);
} else {
print_log(2,"Connection timeout 2");
printf ("Connection timeout\n");
close_weatherstation(ws);
exit(0);
}
write(ws, buffer, 448);
return ws;
}
/********************************************************************
* close_weatherstation, Linux version
*
* Input: Handle to the weatherstation (type WEATHERSTATION)
*
* Returns nothing
*
********************************************************************/
void close_weatherstation(WEATHERSTATION ws)
{
tcflush(ws,TCIOFLUSH);
close(ws);
return;
}
/********************************************************************
* set_DTR
* Sets or resets DTR signal
*
* Inputs: serdevice - opened file handle
* val - value to set
*
* Returns nothing
*
********************************************************************/
void set_DTR(WEATHERSTATION ws, int val)
{
//TODO: use TIOCMBIC and TIOCMBIS instead of TIOCMGET and TIOCMSET
int portstatus;
ioctl(ws, TIOCMGET, &portstatus); // get current port status
if (val)
{
print_log(5,"Set DTR");
portstatus |= TIOCM_DTR;
}
else
{
print_log(5,"Clear DTR");
portstatus &= ~TIOCM_DTR;
}
ioctl(ws, TIOCMSET, &portstatus); // set current port status
/*if (val)
ioctl(ws, TIOCMBIS, TIOCM_DTR);
else
ioctl(ws, TIOCMBIC, TIOCM_DTR);*/
}
/********************************************************************
* set_RTS
* Sets or resets RTS signal
*
* Inputs: serdevice - opened file handle,
* val - value to set
*
* Returns nothing
*
********************************************************************/
void set_RTS(WEATHERSTATION ws, int val)
{
//TODO: use TIOCMBIC and TIOCMBIS instead of TIOCMGET and TIOCMSET
int portstatus;
ioctl(ws, TIOCMGET, &portstatus); // get current port status
if (val)
{
print_log(5,"Set RTS");
portstatus |= TIOCM_RTS;
}
else
{
print_log(5,"Clear RTS");
portstatus &= ~TIOCM_RTS;
}
ioctl(ws, TIOCMSET, &portstatus); // set current port status
/*if (val)
ioctl(ws, TIOCMBIS, TIOCM_RTS);
else
ioctl(ws, TIOCMBIC, TIOCM_RTS);
*/
}
/********************************************************************
* get_DSR
* Checks status of DSR signal
*
* Inputs: ws - opened file handle
*
*
* Returns: status of DSR signal
*
********************************************************************/
int get_DSR(WEATHERSTATION ws)
{
int portstatus;
ioctl(ws, TIOCMGET, &portstatus); // get current port status
if (portstatus & TIOCM_DSR)
{
print_log(5,"Got DSR = 1");
return 1;
}
else
{
print_log(5,"Got DSR = 0");
return 0;
}
}
/********************************************************************
* get_CTS
* Checks status of CTS signal
*
* Inputs: ws - opened file handle
*
*
* Returns: status of CTS signal
*
********************************************************************/
int get_CTS(WEATHERSTATION ws)
{
int portstatus;
ioctl(ws, TIOCMGET, &portstatus); // get current port status
if (portstatus & TIOCM_CTS)
{
print_log(5,"Got CTS = 1");
return 1;
}
else
{
print_log(5,"Got CTS = 0");
return 0;
}
}
/********************************************************************
* sleep_short - Linux version
*
* Inputs: Time in milliseconds (integer)
*
* Returns: nothing
*
********************************************************************/
void sleep_short(int milliseconds)
{
usleep(milliseconds * 1000);
}
/* Note: if you see timing issues, maybe you need to adjust this ... */
void nanodelay() {
microdelay(10);
}