1
// ----------------------------------------------------------------------------
5
// This file is part of flrig.
7
// flrig is free software; you can redistribute it and/or modify
8
// it under the terms of the GNU General Public License as published by
9
// the Free Software Foundation; either version 3 of the License, or
10
// (at your option) any later version.
12
// flrig is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
// GNU General Public License for more details.
17
// You should have received a copy of the GNU General Public License
18
// along with this program. If not, see <http://www.gnu.org/licenses/>.
19
// ----------------------------------------------------------------------------
34
#include "socket_io.h"
42
const char *nuline = "\n";
44
static int iBaudRates[] = { 300, 600, 1200, 2400, 4800, 9600,
45
19200, 38400, 57600, 115200, 230400, 460800 };
46
const char *szBaudRates[] = { "300", "600", "1200", "2400", "4800", "9600",
47
"19200", "38400", "57600", "115200", "230400", "460800", NULL };
51
if (n > (int)sizeof(iBaudRates)) return 1200;
52
return (iBaudRates[n]);
56
bool startXcvrSerial()
58
bypass_serial_thread_loop = true;
59
// setup commands for serial port
60
if (progStatus.xcvr_serial_port == "NONE") {
61
bypass_serial_thread_loop = false;
65
RigSerial.Device(progStatus.xcvr_serial_port);
66
RigSerial.Baud(BaudRate(progStatus.comm_baudrate));
67
RigSerial.Stopbits(progStatus.stopbits);
68
RigSerial.Retries(progStatus.comm_retries);
69
RigSerial.Timeout(progStatus.comm_timeout);
70
RigSerial.RTSptt(progStatus.comm_rtsptt);
71
RigSerial.DTRptt(progStatus.comm_dtrptt);
72
RigSerial.RTSCTS(progStatus.comm_rtscts);
73
RigSerial.RTS(progStatus.comm_rtsplus);
74
RigSerial.DTR(progStatus.comm_dtrplus);
76
if (!RigSerial.OpenPort()) {
77
LOG_ERROR("Cannot access %s", progStatus.xcvr_serial_port.c_str());
79
} else if (debug::level == debug::DEBUG_LEVEL) {
94
progStatus.xcvr_serial_port.c_str(),
95
progStatus.comm_baudrate,
97
progStatus.comm_retries,
98
progStatus.comm_timeout,
99
progStatus.serloop_timing,
100
progStatus.comm_rtscts,
101
progStatus.comm_catptt,
102
progStatus.comm_rtsptt,
103
progStatus.comm_dtrptt,
104
progStatus.comm_rtsplus,
105
progStatus.comm_dtrplus );
108
RigSerial.FlushBuffer();
109
// bypass_serial_thread_loop = false;
113
bool startAuxSerial()
115
if (progStatus.aux_serial_port == "NONE") return false;
117
AuxSerial.Device(progStatus.aux_serial_port);
118
AuxSerial.Baud(1200);
119
AuxSerial.RTS(progStatus.aux_rts);
120
AuxSerial.DTR(progStatus.aux_dtr);
122
if (!AuxSerial.OpenPort()) {
123
LOG_ERROR("Cannot access %s", progStatus.aux_serial_port.c_str());
129
bool startSepSerial()
131
if (progStatus.sep_serial_port == "NONE") return false;
133
SepSerial.Device(progStatus.sep_serial_port);
134
SepSerial.Baud(1200);
136
SepSerial.RTSCTS(false);
137
SepSerial.RTS(progStatus.sep_rtsplus);
138
SepSerial.RTSptt(progStatus.sep_rtsptt);
140
SepSerial.DTR(progStatus.sep_dtrplus);
141
SepSerial.DTRptt(progStatus.sep_dtrptt);
143
if (!SepSerial.OpenPort()) {
144
LOG_ERROR("Cannot access %s", progStatus.sep_serial_port.c_str());
150
char replybuff[RXBUFFSIZE+1];
157
memset(replybuff, 0, RXBUFFSIZE + 1);
158
if (progStatus.use_tcpip)
159
numread = read_from_remote(replystr);
161
numread = RigSerial.ReadBuffer(replybuff, RXBUFFSIZE);
162
for (int i = 0; i < numread; replystr += replybuff[i++]);
165
LOG_DEBUG("rsp:%3d, %s", numread, str2hex(replystr.c_str(), replystr.length()));
169
int sendCommand (string s, int nread)
171
int numwrite = (int)s.size();
173
if (progStatus.use_tcpip) {
175
send_to_remote(s, progStatus.byte_interval);
177
progStatus.comm_wait + progStatus.tcpip_ping_delay +
178
(int)((nread + progStatus.comm_echo ? numwrite : 0)*11000.0/RigSerial.Baud() );
179
while (timeout > 0) {
180
if (timeout > 10) MilliSleep(10);
181
else MilliSleep(timeout);
185
if (nread == 0) return 0;
186
return readResponse();
189
if (RigSerial.IsOpen() == false)
192
LOG_DEBUG("cmd:%3d, %s", (int)s.length(), str2hex(s.data(), s.length()));
195
RigSerial.WriteBuffer(s.c_str(), numwrite);
197
if (nread == 0) return 0;
198
int timeout = progStatus.comm_wait +
199
(int)((nread + progStatus.comm_echo ? numwrite : 0)*11000.0/RigSerial.Baud()
200
+ progStatus.use_tcpip ? progStatus.tcpip_ping_delay : 0);
201
while (timeout > 0) {
202
if (timeout > 10) MilliSleep(10);
203
else MilliSleep(timeout);
207
return readResponse();
219
int numwrite = (int)command.length();
221
LOG_DEBUG("cmd:%3d, %s", numwrite, how == ASC ? command.c_str() : str2hex(command.data(), numwrite));
223
if (progStatus.use_tcpip) {
224
send_to_remote(command, progStatus.byte_interval);
225
if (nread == 0) return 0;
227
if (RigSerial.IsOpen() == false) {
228
LOG_DEBUG("cmd: %s", how == ASC ? command.c_str() : str2hex(command.data(), command.length()));
233
RigSerial.WriteBuffer(command.c_str(), numwrite);
234
if (nread == 0) return 0;
237
// minimimum time to wait for a response
238
int timeout = (int)((nread + progStatus.comm_echo ? numwrite : 0)*11000.0/RigSerial.Baud()
239
+ progStatus.use_tcpip ? progStatus.tcpip_ping_delay : 0);
240
while (timeout > 0) {
241
if (timeout > 10) MilliSleep(10);
242
else MilliSleep(timeout);
246
// additional wait for xcvr processing
247
string returned = "";
248
static char sztemp[100];
250
while (waited < msec) {
252
returned.append(replystr);
253
if ( ((int)returned.length() >= nread) ||
254
(returned.find(term) != string::npos) ) {
256
snprintf(sztemp, sizeof(sztemp), "%s rcvd in %d msec", info.c_str(), waited + timeout);
257
showresp(level, how, sztemp, command, returned);
265
snprintf(sztemp, sizeof(sztemp), "%s TIMED OUT in %d ms", info.c_str(), waited + timeout);
266
showresp(ERR, how, sztemp, command, returned);
270
int waitResponse(int timeout)
273
if (!progStatus.use_tcpip && RigSerial.IsOpen() == false)
277
if (!(n = readResponse()))
278
while (timeout > 0) {
279
if (timeout > 10) MilliSleep(10);
280
else MilliSleep(timeout);
287
void clearSerialPort()
289
if (RigSerial.IsOpen() == false) return;
290
RigSerial.FlushBuffer();
293
void showresp(int level, int how, string s, string tx, string rx)
297
struct tm *local = localtime(&now);
299
strftime(sztm, sizeof(sztm), "%H:%M:%S", local);
301
string s1 = how == HEX ? str2hex(tx.c_str(), tx.length()) : tx;
302
string s2 = how == HEX ? str2hex(rx.c_str(), rx.length()) : rx;
305
while((p = s1.find('\r')) != string::npos)
306
s1.replace(p, 1, "<cr>");
307
while((p = s1.find('\n')) != string::npos)
308
s1.replace(p, 1, "<lf>");
309
while((p = s2.find('\r')) != string::npos)
310
s2.replace(p, 1, "<cr>");
311
while((p = s2.find('\n')) != string::npos)
312
s2.replace(p, 1, "<lf>");
317
SLOG_QUIET("%s: %10s\ncmd %s\nans %s", sztm, s.c_str(), s1.c_str(), s2.c_str());
320
SLOG_ERROR("%s: %10s\ncmd %s\nans %s", sztm, s.c_str(), s1.c_str(), s2.c_str());
323
SLOG_WARN("%s: %10s\ncmd %s\nans %s", sztm, s.c_str(), s1.c_str(), s2.c_str());
326
SLOG_INFO("%s: %10s\ncmd %s\nans %s", sztm, s.c_str(), s1.c_str(), s2.c_str());
329
SLOG_DEBUG("%s: %10s\ncmd %s\nans %s", sztm, s.c_str(), s1.c_str(), s2.c_str());