-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathseriallink.cpp
More file actions
285 lines (245 loc) · 6.75 KB
/
Copy pathseriallink.cpp
File metadata and controls
285 lines (245 loc) · 6.75 KB
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
/*
(C) Copyright 2015 Jeremy Burton
This file is part of Sark-100-antenna-analyzer.
Sark-100-antenna-analyzer is free software: you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
Sark-100-antenna-analyzer is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty
of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Foobar. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
//#include <string.h>
//#include <unistd.h>
//#include <errno.h>
//#include <poll.h>
//#include <termios.h>
//#include <fcntl.h>
//#include <sys/ioctl.h>
#include "config.h"
#include "scandata.h"
#include "seriallink.h"
extern ScanData scandata;
#ifdef ENABLE_DUMMY_DEV
SerialLink::SerialLink(const char *dev,int speed) : DummyDev ()
{
printf("SerialLink::Init: dummy\n");
}
void SerialLink::TxCmd(const char *cmd)
{
printf("TxCmd: %s\n",cmd);
if (strncmp(cmd,"scan ",5)==0)
{
dummy_data = "Start\n\
5.1,105,0,105\n\
6.336,102.55,0,102.55\n\
7.451,95.45,0,95.45\n\
8.336,84.39,0,84.39\n\
8.904,70.45,0,70.45\n\
9.1,55,0,55\n\
8.904,39.55,0,39.55\n\
8.336,25.61,0,25.61\n\
7.451,14.55,0,14.55\n\
6.336,7.45,0,7.45\n\
5.1,5,0,5\n\
3.864,7.45,0,7.45\n\
2.749,14.55,0,14.55\n\
1.864,25.61,0,25.61\n\
1.296,39.55,0,39.55\n\
1.1,55,0,55\n\
1.296,70.45,0,70.45\n\
1.864,84.39,0,84.39\n\
2.749,95.45,0,95.45\n\
3.864,102.55,0,102.55\n\
5.1,105,0,105\n\
End\n";
}
else
dummy_data = "OK\n";
dummy_pos = 0;
}
#else
SerialLink::SerialLink(const char *dev,int speed) : SerialDev (dev,speed)
{
printf("SerialLink::Init: dev %s\n",dev);
}
void SerialLink::TxCmd(const char *cmd)
{
if (IsUp())
{
printf("%ld> TxCmd: %s\n",time(NULL),cmd);
TxData(cmd, strlen(cmd));
TxData("\n", 1);
}
}
#endif
//Handle standard OK/Error response from config commands
//
int SerialLink::RxResponse1()
{
int state = 0,r,ret=0;
for (;state<10;)
{
r = RxLine();
printf("%ld> RxLine=%d\n",time(NULL),r);
if (r==0)
{
printf("\trxbuff: %s (%d)\n",rxbuff,rxbufflen);
//printf("%ld> RxLine: %s (%d)\n",time(NULL), rxbuff,rxbufflen);
printf("\tstate: %d\n",state);
switch (state)
{
case 0:
if (strcmp(rxbuff,"OK")==0)
{
state=10; //Success
}
else if (strncmp(rxbuff,"Error:",5)==0)
{
ret=2; //Error
state=1;
}
break;
case 1:
if (strcmp(rxbuff,">>")==0)
{
state=10; //Ready for next command
}
break;
}
}
else
{
ret=1; //Timeout
state=10;
}
}
return ret;
}
void SerialLink::Cmd_Scan(long fstart, long fend, long fstep, bool useraw, EventReceiver *erx)
{
char cmd[64];
int state = 0,r;
Sample sample;
long frequency = fstart;
RxFlush();
sprintf(cmd,"%s %ld %ld %ld\r", useraw?"scanr":"scan", fstart, fend>=FMAX ? FMAX : (fend+1), fstep); //Add 1Hz to end freq to make it inclusive
TxCmd(cmd);
scandata.points.resize(0);
//TODO: clean this up, why can I not set it globally?
//std::setlocale(LC_ALL, "sv_SE");
std::setlocale(LC_ALL, "C"); //Need sscanf() to use default locale for number parsing.
for (;state<10;)
{
r = RxLine();
printf("%ld> RxLine=%d\n",time(NULL),r);
if (r==0)
{
printf("\trxbuff: %s (%d)\n",rxbuff,rxbufflen);
//printf("%ld> RxLine: %s (%d)\n",time(NULL),rxbuff,rxbufflen);
printf("\tstate: %d\n",state);
switch (state)
{
case 0:
if (strcmp(rxbuff,"Start")==0)
{
erx->RaiseEvent(EventReceiver::progress_event, 0);
state=1; //Results
}
else if (strncmp(rxbuff,"Error:",5)==0)
{
state=10; //Error
}
else
state=0; //Garbage ? Ignore
break;
case 1:
if (strcmp(rxbuff,"End")==0)
{
erx->RaiseEvent(EventReceiver::progress_event, 100);
state=10; //Done
}
else
{
printf("\tf: %ld\n",frequency);
sample.freq = (double)(frequency);
if (useraw)
{
double vf,vr,vz,va;
sscanf(rxbuff,"%lf,%lf,%lf,%lf",&vf,&vr,&vz,&va);
sample.fromRaw(vf,vr,vz,va);
}
else
{
sscanf(rxbuff,"%lf,%lf,%lf,%lf",&sample.swr,&sample.R,&sample.X,&sample.Z);
}
scandata.points.push_back(sample);
frequency += fstep;
erx->RaiseEvent(EventReceiver::progress_event, (int)((frequency-fstart)*100/(fend-fstart)));
}
break;
}
}
else
state=10; //Timeout
}
scandata.UpdateStats();
}
void SerialLink::Cmd_On()
{
RxFlush();
TxCmd("on\r");
RxResponse1();
}
void SerialLink::Cmd_Off()
{
RxFlush();
TxCmd("off\r");
RxResponse1();
}
void SerialLink::Cmd_Freq(long f)
{
char cmd[32];
RxFlush();
sprintf(cmd,"freq %ld\r", f);
TxCmd(cmd);
RxResponse1();
}
void SerialLink::Cmd_Raw(Sample &sample)
{
int state = 0,r;
RxFlush();
TxCmd("raw\r");
//TODO: clean this up, why can I not set it globally?
std::setlocale(LC_ALL, "C"); //Need sscanf() to use default locale for number parsing.
for (;state<10;)
{
r = RxLine();
printf("%ld> RxLine=%d\n",time(NULL),r);
if (r==0)
{
printf("\trxbuff: %s (%d)\n",rxbuff,rxbufflen);
printf("\tstate: %d\n",state);
switch (state)
{
case 0: //Ignore one blank line
state=1;
break;
case 1: //Results
{
double vf,vr,vz,va;
if (sscanf(rxbuff,"%lf,%lf,%lf,%lf",&vf,&vr,&vz,&va) == 4)
sample.fromRaw(vf,vr,vz,va);
state=10; //End of Results
}
break;
}
}
else
state=10; //Timeout
}
}