~ubuntu-branches/ubuntu/wily/flrig/wily

« back to all changes in this revision

Viewing changes to src/rig_io.cxx

  • Committer: Package Import Robot
  • Author(s): Kamal Mostafa
  • Date: 2014-10-25 11:17:10 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20141025111710-n32skgya3l9u1brw
Tags: 1.3.17-1
* New upstream release (Closes: #761839)
* Debian Standards-Version: 3.9.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// ----------------------------------------------------------------------------
2
 
// Copyright (C) 2014
3
 
//              David Freese, W1HKJ
4
 
//
5
 
// This file is part of flrig.
6
 
//
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.
11
 
//
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.
16
 
//
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
 
// ----------------------------------------------------------------------------
20
 
 
21
 
#include <math.h>
22
 
#include <string>
23
 
 
24
 
#include <FL/Fl.H>
25
 
 
26
 
#include "rig.h"
27
 
#include "support.h"
28
 
#include "util.h"
29
 
#include "debug.h"
30
 
#include "status.h"
31
 
#include "rig_io.h"
32
 
 
33
 
using namespace std;
34
 
 
35
 
extern bool test;
36
 
 
37
 
int rig_nbr = 0;
38
 
 
39
 
const char *nuline = "\n";
40
 
 
41
 
static int iBaudRates[] = { 300, 600, 1200, 2400, 4800, 9600,
42
 
        19200, 38400, 57600, 115200, 230400, 460800 };
43
 
const char *szBaudRates[] = { "300", "600", "1200", "2400", "4800", "9600",
44
 
        "19200", "38400", "57600", "115200", "230400", "460800", NULL };
45
 
 
46
 
int BaudRate(int n)
47
 
{
48
 
        if (n > (int)sizeof(iBaudRates)) return 1200;
49
 
        return (iBaudRates[n]);
50
 
}
51
 
 
52
 
 
53
 
bool startXcvrSerial()
54
 
{
55
 
        bypass_serial_thread_loop = true;
56
 
// setup commands for serial port
57
 
        if (progStatus.xcvr_serial_port == "NONE") return false;
58
 
 
59
 
        RigSerial.Device(progStatus.xcvr_serial_port);
60
 
        RigSerial.Baud(BaudRate(progStatus.comm_baudrate));
61
 
        RigSerial.Stopbits(progStatus.stopbits);
62
 
        RigSerial.Retries(progStatus.comm_retries);
63
 
        RigSerial.Timeout(progStatus.comm_timeout);
64
 
        RigSerial.RTSptt(progStatus.comm_rtsptt);
65
 
        RigSerial.DTRptt(progStatus.comm_dtrptt);
66
 
        RigSerial.RTSCTS(progStatus.comm_rtscts);
67
 
        RigSerial.RTS(progStatus.comm_rtsplus);
68
 
        RigSerial.DTR(progStatus.comm_dtrplus);
69
 
 
70
 
        if (!RigSerial.OpenPort()) {
71
 
                LOG_ERROR("Cannot access %s", progStatus.xcvr_serial_port.c_str());
72
 
                return false;
73
 
        } else if (debug::level == debug::DEBUG_LEVEL) {
74
 
                LOG_DEBUG("\n\
75
 
Serial port:\n\
76
 
    Port     : %s\n\
77
 
    Baud     : %d\n\
78
 
        Stopbits : %d\n\
79
 
    Retries  : %d\n\
80
 
    Timeout  : %d\n\
81
 
    Loop     : %d\n\
82
 
    RTSCTS   : %d\n\
83
 
    CATptt   : %d\n\
84
 
    RTSptt   : %d\n\
85
 
    DTRptt   : %d\n\
86
 
    RTS+     : %d\n\
87
 
    DTR+     : %d\n",
88
 
                        progStatus.xcvr_serial_port.c_str(),
89
 
                        progStatus.comm_baudrate,
90
 
                        progStatus.stopbits,
91
 
                        progStatus.comm_retries,
92
 
                        progStatus.comm_timeout,
93
 
                        progStatus.serloop_timing,
94
 
                        progStatus.comm_rtscts,
95
 
                        progStatus.comm_catptt,
96
 
                        progStatus.comm_rtsptt,
97
 
                        progStatus.comm_dtrptt,
98
 
                        progStatus.comm_rtsplus,
99
 
                        progStatus.comm_dtrplus );
100
 
        }
101
 
 
102
 
        RigSerial.FlushBuffer();
103
 
//      bypass_serial_thread_loop = false;
104
 
        return true;
105
 
}
106
 
 
107
 
bool startAuxSerial()
108
 
{
109
 
        if (progStatus.aux_serial_port == "NONE") return false;
110
 
 
111
 
        AuxSerial.Device(progStatus.aux_serial_port);
112
 
        AuxSerial.Baud(1200);
113
 
        AuxSerial.RTS(progStatus.aux_rts);
114
 
        AuxSerial.DTR(progStatus.aux_dtr);
115
 
 
116
 
        if (!AuxSerial.OpenPort()) {
117
 
                LOG_ERROR("Cannot access %s", progStatus.aux_serial_port.c_str());
118
 
                return false;
119
 
        }
120
 
        return true;
121
 
}
122
 
 
123
 
bool startSepSerial()
124
 
{
125
 
        if (progStatus.sep_serial_port == "NONE") return false;
126
 
 
127
 
        SepSerial.Device(progStatus.sep_serial_port);
128
 
        SepSerial.Baud(1200);
129
 
 
130
 
        SepSerial.RTSCTS(false);
131
 
        SepSerial.RTS(progStatus.sep_rtsplus);
132
 
        SepSerial.RTSptt(progStatus.sep_rtsptt);
133
 
 
134
 
        SepSerial.DTR(progStatus.sep_dtrplus);
135
 
        SepSerial.DTRptt(progStatus.sep_dtrptt);
136
 
 
137
 
        if (!SepSerial.OpenPort()) {
138
 
                LOG_ERROR("Cannot access %s", progStatus.sep_serial_port.c_str());
139
 
                return false;
140
 
        }
141
 
        return true;
142
 
}
143
 
 
144
 
char replybuff[RXBUFFSIZE+1];
145
 
string replystr;
146
 
 
147
 
int readResponse()
148
 
{
149
 
        int numread = 0;
150
 
        replystr.clear();
151
 
        memset(replybuff, 0, RXBUFFSIZE + 1);
152
 
        numread = RigSerial.ReadBuffer(replybuff, RXBUFFSIZE);
153
 
        LOG_DEBUG("rsp:%3d, %s", numread, str2hex(replybuff, numread));
154
 
        for (int i = 0; i < numread; replystr += replybuff[i++]);
155
 
        return numread;
156
 
}
157
 
 
158
 
int sendCommand (string s, int nread)
159
 
{
160
 
        int numwrite = (int)s.size();
161
 
 
162
 
        LOG_DEBUG("cmd:%3d, %s", (int)s.length(), str2hex(s.data(), s.length()));
163
 
        if (RigSerial.IsOpen() == false) {
164
 
                replystr.clear();
165
 
                return 0;
166
 
        }
167
 
 
168
 
        if (progStatus.byte_interval == 0)
169
 
                RigSerial.WriteBuffer(s.c_str(), numwrite);
170
 
        else
171
 
                for (size_t i = 0; i < s.length(); i++) {
172
 
                        RigSerial.WriteByte(s[i]);
173
 
                        MilliSleep(progStatus.byte_interval);
174
 
                }
175
 
 
176
 
        MilliSleep( progStatus.comm_wait );
177
 
 
178
 
        if (nread == 0) {
179
 
                replystr.clear();
180
 
                return 0;
181
 
        }
182
 
        if (nread > 0)
183
 
                MilliSleep( (int)((nread + progStatus.comm_echo ? numwrite : 0)*11000.0/RigSerial.Baud()));
184
 
 
185
 
        return readResponse();
186
 
 
187
 
}
188
 
 
189
 
int waitResponse(int timeout)
190
 
{
191
 
        int n = 0;
192
 
        if (RigSerial.IsOpen() == false)
193
 
                return 0;
194
 
        MilliSleep(10);
195
 
        if (!(n = readResponse()))
196
 
                for (int t = 0; t <= timeout; t += 50) {
197
 
                        if ((n = readResponse())) break;
198
 
                        MilliSleep(50);
199
 
                        Fl::awake();
200
 
                }
201
 
        return n;
202
 
}
203
 
 
204
 
void clearSerialPort()
205
 
{
206
 
        if (RigSerial.IsOpen() == false) return;
207
 
        RigSerial.FlushBuffer();
208
 
}