forked from Hamlib/Hamlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gs232.c
271 lines (224 loc) · 7.09 KB
/
gs232.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
/*
* Hamlib Rotator backend - GS-232
* Copyright (c) 2001-2010 by Stephane Fillod
* Copyright (c) 2009 by Jason Winningham
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h> /* String function definitions */
#include <unistd.h> /* UNIX standard function definitions */
#include <math.h>
#include "hamlib/rotator.h"
#include "serial.h"
#include "misc.h"
#include "register.h"
#include "gs232a.h"
#define EOM "\r"
#define REPLY_EOM "\r"
#define BUFSZ 64
/**
* gs232_transaction
*
* cmdstr - Command to be sent to the rig.
* data - Buffer for reply string. Can be NULL, indicating that no reply is
* is needed, but answer will still be read.
* data_len - in: Size of buffer. It is the caller's responsibily to provide
* a large enough buffer for all possible replies for a command.
*
* returns:
* RIG_OK - if no error occured.
* RIG_EIO - if an I/O error occured while sending/receiving data.
* RIG_ETIMEOUT - if timeout expires without any characters received.
* RIG_REJECTED - if a negative acknowledge was received or command not
* recognized by rig.
*/
static int
gs232_transaction (ROT *rot, const char *cmdstr,
char *data, size_t data_len)
{
struct rot_state *rs;
int retval;
int retry_read = 0;
char replybuf[BUFSZ];
rs = &rot->state;
transaction_write:
serial_flush(&rs->rotport);
if (cmdstr) {
retval = write_block(&rs->rotport, cmdstr, strlen(cmdstr));
if (retval != RIG_OK)
goto transaction_quit;
}
/* Always read the reply to know whether the cmd went OK */
if (!data)
data = replybuf;
if (!data_len)
data_len = BUFSZ;
memset(data,0,data_len);
retval = read_string(&rs->rotport, data, data_len, REPLY_EOM, strlen(REPLY_EOM));
if (retval < 0) {
if (retry_read++ < rot->state.rotport.retry)
goto transaction_write;
goto transaction_quit;
}
#if 0
/* Check that command termination is correct */
if (strchr(REPLY_EOM, data[strlen(data)-1])==NULL) {
rig_debug(RIG_DEBUG_ERR, "%s: Command is not correctly terminated '%s'\n", __FUNCTION__, data);
if (retry_read++ < rig->state.rotport.retry)
goto transaction_write;
retval = -RIG_EPROTO;
goto transaction_quit;
}
#endif
if (data[0] == '?') {
/* Invalid command */
rig_debug(RIG_DEBUG_VERBOSE, "%s: Error for '%s': '%s'\n",
__FUNCTION__, cmdstr, data);
retval = -RIG_EPROTO;
goto transaction_quit;
}
retval = RIG_OK;
transaction_quit:
return retval;
}
/*
* write-only transaction, no data returned by controller
*/
static int
gs232_wo_transaction (ROT *rot, const char *cmdstr,
char *data, size_t data_len)
{
return write_block(&rot->state.rotport, cmdstr, strlen(cmdstr));
}
static int
gs232_rot_set_position(ROT *rot, azimuth_t az, elevation_t el)
{
char cmdstr[64];
int retval;
unsigned u_az, u_el;
rig_debug(RIG_DEBUG_TRACE, "%s called: %f %f\n", __FUNCTION__, az, el);
u_az = (unsigned)rint(az);
u_el = (unsigned)rint(el);
sprintf(cmdstr, "W%03u %03u" EOM, u_az, u_el);
retval = gs232_wo_transaction(rot, cmdstr, NULL, 0);
if (retval != RIG_OK)
return retval;
return RIG_OK;
}
static int
gs232_rot_get_position(ROT *rot, azimuth_t *az, elevation_t *el)
{
char posbuf[32];
int retval;
rig_debug(RIG_DEBUG_TRACE, "%s called\n", __FUNCTION__);
retval = gs232_transaction(rot, "C2" EOM, posbuf, sizeof(posbuf));
if (retval != RIG_OK || strlen(posbuf) < 10)
return retval;
/* parse */
if (sscanf(posbuf+2, "%f", az) != 1) {
rig_debug(RIG_DEBUG_ERR, "%s: wrong reply '%s'\n", __FUNCTION__, posbuf);
return -RIG_EPROTO;
}
if (sscanf(posbuf+7, "%f", el) != 1) {
rig_debug(RIG_DEBUG_ERR, "%s: wrong reply '%s'\n", __FUNCTION__, posbuf);
return -RIG_EPROTO;
}
rig_debug(RIG_DEBUG_TRACE, "%s: (az, el) = (%.1f, %.1f)\n",
__FUNCTION__, *az, *el);
return RIG_OK;
}
static int
gs232_rot_stop(ROT *rot)
{
int retval;
rig_debug(RIG_DEBUG_TRACE, "%s called\n", __FUNCTION__);
/* All Stop */
retval = gs232_wo_transaction(rot, "S" EOM, NULL, 0);
if (retval != RIG_OK)
return retval;
return RIG_OK;
}
/* ************************************************************************* */
/*
* Generic GS232 (not A, not B) rotator capabilities.
*/
const struct rot_caps gs232_rot_caps = {
.rot_model = ROT_MODEL_GS232,
.model_name = "GS-232",
.mfg_name = "Yaesu/Kenpro",
.version = "0.1",
.copyright = "LGPL",
.status = RIG_STATUS_BETA,
.rot_type = ROT_TYPE_AZEL,
.port_type = RIG_PORT_SERIAL,
.serial_rate_min = 150,
.serial_rate_max = 9600,
.serial_data_bits = 8,
.serial_stop_bits = 1,
.serial_parity = RIG_PARITY_NONE,
.serial_handshake = RIG_HANDSHAKE_NONE,
.write_delay = 0,
.post_write_delay = 0,
.timeout = 400,
.retry = 3,
.min_az = 0.0,
.max_az = 450.0, /* vary according to rotator type */
.min_el = 0.0,
.max_el = 180.0,
.get_position = gs232_rot_get_position,
.set_position = gs232_rot_set_position,
.stop = gs232_rot_stop,
};
/* ************************************************************************* */
/*
* F1TE Tracker, GS232 withtout position feedback
*
* http://www.f1te.org/index.php?option=com_content&view=article&id=19&Itemid=39
*/
const struct rot_caps f1tetracker_rot_caps = {
.rot_model = ROT_MODEL_F1TETRACKER,
.model_name = "GS232/F1TE Tracker",
.mfg_name = "F1TE",
.version = "0.1",
.copyright = "LGPL",
.status = RIG_STATUS_BETA,
.rot_type = ROT_TYPE_AZEL,
.port_type = RIG_PORT_SERIAL,
.serial_rate_min = 150,
.serial_rate_max = 9600,
.serial_data_bits = 8,
.serial_stop_bits = 1,
.serial_parity = RIG_PARITY_NONE,
.serial_handshake = RIG_HANDSHAKE_NONE,
.write_delay = 0,
.post_write_delay = 0,
.timeout = 400,
.retry = 0,
.min_az = 0.0,
.max_az = 360.0, /* vary according to rotator type */
.min_el = 0.0,
.max_el = 180.0,
.get_position = NULL, /* no position feedback available */
.set_position = gs232_rot_set_position,
#if 0
.stop = gs232_rot_stop,
#endif
};