Hamlib/rigs/kit/rs_hfiq.c

401 wiersze
11 KiB
C
Czysty Zwykły widok Historia

2017-05-29 11:12:52 +00:00
/*
* Hamlib rs-hfiq backend - main file
* Copyright (c) 2017 by Volker Schroer
*
*
* 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
*
*/
/*
*
* For information about the controls see
2017-05-29 11:12:52 +00:00
* https://sites.google.com/site/rshfiqtransceiver/home/technical-data/interface-commands
*
*/
#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 "hamlib/rig.h"
#include "serial.h"
#include "misc.h"
2017-05-29 11:12:52 +00:00
#define RSHFIQ_INIT_RETRY 5
2017-05-29 11:12:52 +00:00
#define RSHFIQ_LEVEL_ALL (RIG_LEVEL_RFPOWER_METER|RIG_LEVEL_TEMP_METER)
2021-08-04 08:23:56 +00:00
int rshfiq_version_major, rshfiq_version_minor;
2017-05-29 11:12:52 +00:00
static int rshfiq_open(RIG *rig)
{
2019-11-30 16:19:08 +00:00
int retval;
int flag;
char versionstr[20];
char stopset[2];
stopset[0] = '\r';
stopset[1] = '\n';
rig_debug(RIG_DEBUG_TRACE, "%s: Port = %s\n", __func__,
rig->state.rigport.pathname);
rig->state.rigport.timeout = 2000;
rig->state.rigport.retry = 1;
retval = serial_open(&rig->state.rigport);
if (retval != RIG_OK)
{
return retval;
}
retval = ser_get_dtr(&rig->state.rigport, &flag);
if (retval == RIG_OK)
{
rig_debug(RIG_DEBUG_TRACE, "%s: DTR: %d\n", __func__, flag);
}
else
{
rig_debug(RIG_DEBUG_TRACE, "%s: Could not get DTR\n", __func__);
}
if (flag == 0)
{
flag = 1; // Set DTR
retval = ser_set_dtr(&rig->state.rigport, flag);
if (retval == RIG_OK)
{
rig_debug(RIG_DEBUG_TRACE, "%s: set DTR\n", __func__);
}
}
//There is a delay between when the port is open and the RS-HFIQ can receive and respond.
//Make a few attempts at getting the version string just in case the RS-HFIQ has to catch up first.
retval = -RIG_ETIMEOUT;
for (int init_retry_count = 0; (init_retry_count < RSHFIQ_INIT_RETRY)
&& (retval == -RIG_ETIMEOUT); init_retry_count++)
2019-11-30 16:19:08 +00:00
{
rig_flush(&rig->state.rigport);
snprintf(versionstr, sizeof(versionstr), "*w\r");
rig_debug(RIG_DEBUG_TRACE, "%s: cmdstr = %s\n", __func__, versionstr);
2019-11-30 16:19:08 +00:00
retval = write_block(&rig->state.rigport, versionstr, strlen(versionstr));
2019-11-30 16:19:08 +00:00
if (retval != RIG_OK)
{
return retval;
}
retval = read_string(&rig->state.rigport, versionstr, 20, stopset, 2, 0, 1);
}
2019-11-30 16:19:08 +00:00
if (retval <= 0)
{
return retval;
2019-11-30 16:19:08 +00:00
}
rig_flush(&rig->state.rigport);
2019-11-30 16:19:08 +00:00
versionstr[retval] = 0;
rig_debug(RIG_DEBUG_TRACE, "%s: Rigversion = %s\n", __func__, versionstr);
if (!strstr(versionstr, "RS-HFIQ"))
{
rig_debug(RIG_DEBUG_WARN, "%s: Invalid Rigversion: %s\n", __func__, versionstr);
return -RIG_ECONF;
}
retval = sscanf(versionstr, "RS-HFIQ FW %d.%d", &rshfiq_version_major,
&rshfiq_version_minor);
if (retval <= 0)
{
rig_debug(RIG_DEBUG_WARN,
"%s: Failed to parse RS-HFIQ firmware version string. Defaulting to 2.0.\n",
__func__);
rshfiq_version_major = 2;
rshfiq_version_minor = 0;
}
rig_debug(RIG_DEBUG_VERBOSE,
"RS-HFIQ returned firmware version major=%d minor=%d\n", rshfiq_version_major,
rshfiq_version_minor);
if (rshfiq_version_major < 4)
{
rig_debug(RIG_DEBUG_WARN,
"%s: RS-HFIQ firmware major version is less than 4. RFPOWER_METER support will be unavailable.\n",
__func__);
}
2019-11-30 16:19:08 +00:00
return RIG_OK;
2017-05-29 11:12:52 +00:00
}
static int rshfiq_set_freq(RIG *rig, vfo_t vfo, freq_t freq)
{
2019-11-30 16:19:08 +00:00
char fstr[9];
char cmdstr[15];
int retval;
snprintf(fstr, sizeof(fstr), "%lu", (unsigned long int)(freq));
rig_debug(RIG_DEBUG_TRACE, "%s called: %s %s\n", __func__,
rig_strvfo(vfo), fstr);
2017-05-29 11:12:52 +00:00
rig_flush(&rig->state.rigport);
2017-05-29 11:12:52 +00:00
2019-11-30 16:19:08 +00:00
snprintf(cmdstr, sizeof(cmdstr), "*f%lu\r", (unsigned long int)(freq));
2017-05-29 11:12:52 +00:00
2019-11-30 16:19:08 +00:00
retval = write_block(&rig->state.rigport, cmdstr, strlen(cmdstr));
2017-05-29 11:12:52 +00:00
2019-11-30 16:19:08 +00:00
if (retval != RIG_OK)
{
return retval;
}
2017-05-29 11:12:52 +00:00
2019-11-30 16:19:08 +00:00
return RIG_OK;
2017-05-29 11:12:52 +00:00
}
static int rshfiq_get_freq(RIG *rig, vfo_t vfo, freq_t *freq)
{
2019-11-30 16:19:08 +00:00
char cmdstr[15];
char stopset[2];
int retval;
rig_flush(&rig->state.rigport);
2019-11-30 16:19:08 +00:00
stopset[0] = '\r';
stopset[1] = '\n';
2019-11-30 16:19:08 +00:00
snprintf(cmdstr, sizeof(cmdstr), "*f?\r");
rig_debug(RIG_DEBUG_TRACE, "%s: cmdstr = %s\n", __func__, cmdstr);
retval = write_block(&rig->state.rigport, cmdstr, strlen(cmdstr));
if (retval != RIG_OK)
{
return retval;
}
retval = read_string(&rig->state.rigport, cmdstr, 9, stopset, 2, 0, 1);
2019-11-30 16:19:08 +00:00
if (retval <= 0)
{
return retval;
}
2021-08-04 08:23:56 +00:00
rig_debug(RIG_DEBUG_TRACE, "%s: reply = %s\n", __func__, cmdstr);
2019-11-30 16:19:08 +00:00
cmdstr[retval] = 0;
*freq = atoi(cmdstr);
if (*freq == 0) // fldigi interprets zero frequency as error
{
*freq = 1; // so return 1 ( freq= 1Hz )
}
return RIG_OK;
2017-05-29 11:12:52 +00:00
}
2019-11-30 16:19:08 +00:00
static int rshfiq_set_ptt(RIG *rig, vfo_t vfo, ptt_t ptt)
2017-05-29 11:12:52 +00:00
{
2019-11-30 16:19:08 +00:00
char cmdstr[5];
int retval;
2019-11-30 16:19:08 +00:00
cmdstr[0] = '*';
cmdstr[1] = 'x';
cmdstr[3] = '\r';
cmdstr[4] = 0;
if (ptt == RIG_PTT_ON)
{
cmdstr[2] = '1';
}
else
{
cmdstr[2] = '0';
}
rig_debug(RIG_DEBUG_TRACE, "%s: cmdstr = %s\n", __func__, cmdstr);
retval = write_block(&rig->state.rigport, cmdstr, strlen(cmdstr));
return retval;
2017-05-29 11:12:52 +00:00
}
2021-08-04 08:23:56 +00:00
static int rshfiq_get_level(RIG *rig, vfo_t vfo, setting_t level, value_t *val)
{
rig_debug(RIG_DEBUG_VERBOSE, "%s called. level type =%"PRIll"\n", __func__,
level);
2021-08-04 08:23:56 +00:00
char cmdstr[15];
char stopset[2];
int retval;
if (!val)
{
2021-08-04 08:23:56 +00:00
return -RIG_EINVAL;
}
switch (level)
{
//Requires RS-HFIQ firmware version 4 or later
case RIG_LEVEL_RFPOWER_METER:
if (rshfiq_version_major <= 3)
2021-08-04 08:23:56 +00:00
{
return -RIG_ENAVAIL;
}
2021-08-04 08:23:56 +00:00
rig_flush(&rig->state.rigport);
snprintf(cmdstr, sizeof(cmdstr), "*L\r");
2021-08-04 08:23:56 +00:00
rig_debug(RIG_DEBUG_TRACE, "RIG_LEVEL_RFPOWER_METER command=%s\n", cmdstr);
2021-08-04 08:23:56 +00:00
retval = write_block(&rig->state.rigport, cmdstr, strlen(cmdstr));
2021-08-04 08:23:56 +00:00
if (retval != RIG_OK)
{
return retval;
}
2021-08-04 08:23:56 +00:00
stopset[0] = '\r';
stopset[1] = '\n';
2021-08-04 08:23:56 +00:00
retval = read_string(&rig->state.rigport, cmdstr, 9, stopset, 2, 0, 1);
2021-08-04 08:23:56 +00:00
rig_debug(RIG_DEBUG_TRACE, "RIG_LEVEL_RFPOWER_METER reply=%s\n", cmdstr);
2021-08-04 08:23:56 +00:00
if (retval <= 0)
{
return retval;
}
2021-08-04 08:23:56 +00:00
cmdstr[retval] = 0;
2021-08-04 08:23:56 +00:00
//Range is 0-110. Unit is percent
val->i = atoi(cmdstr);
val->f = (float)(val->i) / 100;
2021-08-04 08:23:56 +00:00
rig_debug(RIG_DEBUG_TRACE, "RIG_LEVEL_RFPOWER_METER val=%f\n", val->f);
2021-08-04 08:23:56 +00:00
return RIG_OK;
break;
2021-08-04 08:23:56 +00:00
case RIG_LEVEL_TEMP_METER:
2021-08-04 08:23:56 +00:00
rig_flush(&rig->state.rigport);
snprintf(cmdstr, sizeof(cmdstr), "*T\r");
rig_debug(RIG_DEBUG_TRACE, "RIG_LEVEL_TEMP_METER command=%s\n", cmdstr);
retval = write_block(&rig->state.rigport, cmdstr, strlen(cmdstr));
if (retval != RIG_OK)
{
return retval;
}
stopset[0] = '\r';
stopset[1] = '\n';
retval = read_string(&rig->state.rigport, cmdstr, 9, stopset, 2, 0, 1);
rig_debug(RIG_DEBUG_TRACE, "RIG_LEVEL_TEMP_METER reply=%s\n", cmdstr);
if (retval <= 0)
{
return retval;
}
cmdstr[retval] = 0;
sscanf(cmdstr, "%d.", &val->i);
rig_debug(RIG_DEBUG_TRACE, "RIG_LEVEL_TEMP_METER val=%d\n", val->i);
return RIG_OK;
break;
2021-08-04 08:23:56 +00:00
break;
2021-08-04 08:23:56 +00:00
default:
rig_debug(RIG_DEBUG_VERBOSE, "%s: Unrecognized RIG_LEVEL_* enum: %"PRIll"\n",
__func__, level);
return -RIG_EDOM;
2021-08-04 08:23:56 +00:00
break;
}
}
static int rshfiq_get_mode(RIG *rig, vfo_t vfo, rmode_t *mode, pbwidth_t *width)
{
*mode = RIG_MODE_IQ;
return RIG_OK;
}
2019-11-30 16:19:08 +00:00
const struct rig_caps rshfiq_caps =
{
RIG_MODEL(RIG_MODEL_RSHFIQ),
2019-11-30 16:19:08 +00:00
.model_name = "RS-HFIQ",
.mfg_name = "HobbyPCB",
.version = "20211020.0",
2019-11-30 16:19:08 +00:00
.copyright = "LGPL",
.status = RIG_STATUS_STABLE,
2019-11-30 16:19:08 +00:00
.rig_type = RIG_TYPE_TRANSCEIVER,
.ptt_type = RIG_PTT_RIG,
.port_type = RIG_PORT_SERIAL,
.dcd_type = RIG_DCD_NONE,
.serial_rate_min = 57600,
.serial_rate_max = 57600,
.serial_data_bits = 8,
.serial_stop_bits = 1,
.serial_parity = RIG_PARITY_NONE,
.serial_handshake = RIG_HANDSHAKE_NONE,
.write_delay = 0,
.post_write_delay = 1,
.timeout = 1000,
.retry = 3,
2021-08-04 08:23:56 +00:00
.has_get_level = RSHFIQ_LEVEL_ALL,
.rx_range_list1 = { {.startf = kHz(3500), .endf = MHz(30), .modes = RIG_MODE_IQ, .low_power = -1, .high_power = -1, RIG_VFO_A},
2019-11-30 16:19:08 +00:00
RIG_FRNG_END,
},
.rx_range_list2 = { {.startf = kHz(3500), .endf = MHz(30), .modes = RIG_MODE_IQ, .low_power = -1, .high_power = -1, RIG_VFO_A},
2019-11-30 16:19:08 +00:00
RIG_FRNG_END,
},
.tx_range_list1 = { {.startf = kHz(3500), .endf = kHz(3800), .modes = RIG_MODE_IQ, .low_power = -1, .high_power = -1, RIG_VFO_A},
{.startf = kHz(7000), .endf = kHz(7200), .modes = RIG_MODE_IQ, .low_power = -1, .high_power = -1, RIG_VFO_A},
{.startf = kHz(10100), .endf = kHz(10150), .modes = RIG_MODE_IQ, .low_power = -1, .high_power = -1, RIG_VFO_A},
{.startf = MHz(14), .endf = kHz(14350), .modes = RIG_MODE_IQ, .low_power = -1, .high_power = -1, RIG_VFO_A},
{.startf = MHz(21), .endf = kHz(21450), .modes = RIG_MODE_IQ, .low_power = -1, .high_power = -1, RIG_VFO_A},
{.startf = kHz(24890), .endf = kHz(24990), .modes = RIG_MODE_IQ, .low_power = -1, .high_power = -1, RIG_VFO_A},
{.startf = MHz(28), .endf = kHz(29700), .modes = RIG_MODE_IQ, .low_power = -1, .high_power = -1, RIG_VFO_A},
2019-11-30 16:19:08 +00:00
RIG_FRNG_END,
},
.tuning_steps = { {RIG_MODE_IQ, Hz(1)}, RIG_TS_END, },
2019-11-30 16:19:08 +00:00
.rig_open = rshfiq_open,
.get_freq = rshfiq_get_freq,
.set_freq = rshfiq_set_freq,
.set_ptt = rshfiq_set_ptt,
.get_level = rshfiq_get_level,
.get_mode = rshfiq_get_mode,
2017-05-29 11:12:52 +00:00
};