hamlib/tests/example.c

173 lines
5.2 KiB
C
Raw Permalink Normal View History

/* This is a elementary program calling Hamlib to do some useful things.
*
* Edit to specify your rig model and serial port, and baud rate
* before compiling.
* To compile:
2019-12-19 11:51:28 -06:00
* gcc -I../src -I../include -g -o example example.c sprintflst.c -lhamlib
* if hamlib is installed in /usr/local/...
*
*/
#include <stdio.h>
#include <string.h>
#include "hamlib/rig.h"
#include "hamlib/port.h"
#include "hamlib/rig_state.h"
#include "hamlib/riglist.h"
2019-12-19 11:51:28 -06:00
#include "sprintflst.h"
#include "hamlib/rotator.h"
#if 1
#define MODEL RIG_MODEL_DUMMY
#define PATH "/dev/ttyUSB0"
#define BAUD 19200
#else
#define MODEL RIG_MODEL_NETRIGCTL
#define PATH "127.0.0.1:4532"
#define BAUD 0
#endif
int main()
{
RIG *my_rig;
char *info_buf;
freq_t freq;
value_t rawstrength, power, strength;
float s_meter, rig_raw2val(int i, cal_table_t *t);
2019-12-08 23:30:23 -06:00
int status, retcode;
unsigned int mwpower;
rmode_t mode;
pbwidth_t width;
/* Set verbosity level */
rig_set_debug(RIG_DEBUG_TRACE); // Lots of output
/* Instantiate a rig */
my_rig = rig_init(MODEL); // your rig model.
rig_set_conf(my_rig, rig_token_lookup(my_rig, "rig_pathname"), PATH);
HAMLIB_RIGPORT(my_rig)->parm.serial.rate = BAUD; // your baud rate
/* Open my rig */
retcode = rig_open(my_rig);
2019-12-08 23:30:23 -06:00
if (retcode != RIG_OK)
{
rig_debug(RIG_DEBUG_ERR, "%s: rig_open failed %s\n", __func__,
rigerror(retcode));
return 1;
}
/* Give me ID info, e.g., firmware version. */
info_buf = (char *)rig_get_info(my_rig);
printf("Rig_info: '%s'\n", info_buf);
/* Note: As a general practice, we should check to see if a given
* function is within the rig's capabilities before calling it, but
* we are simplifying here. Also, we should check each call's returned
* status in case of error. (That's an inelegant way to catch an unsupported
* operation.)
*/
if (my_rig->caps->rig_model == RIG_MODEL_NETRIGCTL)
{
status = rig_set_vfo_opt(my_rig, 1);
2020-05-27 12:23:08 -05:00
if (status != RIG_OK) { printf("set_vfo_opt failed?? Err=%s\n", rigerror(status)); }
}
2020-05-27 12:23:08 -05:00
/* Main VFO frequency */
status = rig_get_freq(my_rig, RIG_VFO_CURR, &freq);
2020-02-23 11:26:09 -06:00
if (status != RIG_OK) { printf("Get freq failed?? Err=%s\n", rigerror(status)); }
printf("VFO freq. = %.1f Hz\n", freq);
/* Current mode */
status = rig_get_mode(my_rig, RIG_VFO_CURR, &mode, &width);
2020-02-23 11:26:09 -06:00
if (status != RIG_OK) { printf("Get mode failed?? Err=%s\n", rigerror(status)); }
Document fixing MinGW warnings and linker error This commit has been left as documentation should these files be moved to /doc or some such. However, never of these files are built or distributed. ---------------- From example.c the following warning was generated by MinGW: CC example.o example.c: In function ‘main’: example.c:93:32: warning: format ‘%lX’ expects argument of type ‘long unsigned int’, but argument 2 has type ‘rmode_t’ {aka ‘long long unsigned int’} [-Wformat=] 93 | printf("Current mode = 0x%lX = %s, width = %ld\n", mode, rig_strrmode(mode), | ~~^ ~~~~ | | | | long unsigned int rmode_t {aka long long unsigned int} | %llX The 'l' was added as suggested to the format specifier but that resulted in the following warning from Linux: CC example.o ../../hamlib/tests/example.c:93:57: warning: format specifies type 'unsigned long long' but the argument has type 'rmode_t' (aka 'unsigned long') [-Wformat] printf("Current mode = 0x%llX = %s, width = %ld\n", mode, rig_strrmode(mode), ~~~~ ^~~~ %lX CC rigctl-dumpcaps.o 1 warning generated. So casting `mode` to `unsigned long long` quelled both warnings! From testsecurity.c came this warning: CC testsecurity-testsecurity.o In file included from ../include/hamlib/rig.h:49, from ../src/misc.h:26, from testsecurity.c:29: /usr/share/mingw-w64/include/winsock2.h:15:2: warning: #warning Please include winsock2.h before windows.h [-Wcpp] 15 | #warning Please include winsock2.h before windows.h | ^~~~~~~ Apparently winsock2.h being included through misc.h didn't work. Finally, the Mingw linker gave the following error: CCLD testsecurity.exe /usr/bin/x86_64-w64-mingw32-ld: testsecurity-testsecurity.o: in function `main': /home/nate/builds/hamlib-4.7~git/tests/testsecurity.c:97:(.text.startup+0x87): undefined reference to `AESStringCrypt' /usr/bin/x86_64-w64-mingw32-ld: /home/nate/builds/hamlib-4.7~git/tests/testsecurity.c:116:(.text.startup+0x128): undefined reference to `AESStringDecrypt' collect2: error: ld returned 1 exit status Specifcally add the libsecurity.la file path as a specific library. Even though the libhamlib.la seems to have the AESStringCrypt symbol already.
2025-08-18 13:28:35 -05:00
printf("Current mode = 0x%llX = %s, width = %ld\n", (unsigned long long)mode, rig_strrmode(mode),
width);
/* rig power output */
status = rig_get_level(my_rig, RIG_VFO_CURR, RIG_LEVEL_RFPOWER, &power);
2019-12-08 23:30:23 -06:00
if (status != RIG_OK) { rig_debug(RIG_DEBUG_ERR, "%s: error rig_get_level: %s\n", __func__, rigerror(status)); }
printf("RF Power relative setting = %.3f (0.0 - 1.0)\n", power.f);
/* Convert power reading to watts */
status = rig_power2mW(my_rig, &mwpower, power.f, freq, mode);
2019-12-08 23:30:23 -06:00
if (status != RIG_OK) { rig_debug(RIG_DEBUG_ERR, "%s: error rig_get_level: %s\n", __func__, rigerror(status)); }
printf("RF Power calibrated = %.1f Watts\n", mwpower / 1000.);
/* Raw and calibrated S-meter values */
status = rig_get_level(my_rig, RIG_VFO_CURR, RIG_LEVEL_RAWSTR, &rawstrength);
2019-12-08 23:30:23 -06:00
if (status != RIG_OK) { rig_debug(RIG_DEBUG_ERR, "%s: error rig_get_level: %s\n", __func__, rigerror(status)); }
2019-12-08 23:30:23 -06:00
printf("Raw receive strength = %d\n", rawstrength.i);
s_meter = rig_raw2val(rawstrength.i, &my_rig->caps->str_cal);
printf("S-meter value = %.2f dB relative to S9\n", s_meter);
/* now try using RIG_LEVEL_STRENGTH itself */
status = rig_get_strength(my_rig, RIG_VFO_CURR, &strength);
2019-12-08 23:30:23 -06:00
if (status != RIG_OK) { rig_debug(RIG_DEBUG_ERR, "%s: error rig_get_level: %s\n", __func__, rigerror(status)); }
printf("LEVEL_STRENGTH returns %d\n", strength.i);
2019-12-19 11:51:28 -06:00
const struct rig_state *my_rs = HAMLIB_STATE(my_rig);
const freq_range_t *range = rig_get_range(&my_rs->rx_range_list[0],
2020-02-23 11:26:09 -06:00
14074000, RIG_MODE_USB);
2019-12-19 11:51:28 -06:00
2025-08-02 16:10:20 +02:00
if (status != RIG_OK) { rig_debug(RIG_DEBUG_ERR, "%s: error rig_get_range: %s\n", __func__, rigerror(status)); }
2020-02-23 11:26:09 -06:00
if (range)
{
char vfolist[256];
rig_sprintf_vfo(vfolist, sizeof(vfolist), my_rs->vfo_list);
2020-02-23 11:26:09 -06:00
printf("Range start=%"PRIfreq", end=%"PRIfreq", low_power=%d, high_power=%d, vfos=%s\n",
range->startf, range->endf, range->low_power, range->high_power, vfolist);
2019-12-19 11:51:28 -06:00
}
2020-02-23 11:26:09 -06:00
else
{
printf("Not rx range list found\n");
2019-12-19 11:51:28 -06:00
}
printf("Closing and reopening rig\n");
rig_close(my_rig);
int loops = 1;
2020-05-27 12:23:08 -05:00
while (loops--)
{
retcode = rig_open(my_rig);
if (retcode != RIG_OK)
{
rig_debug(RIG_DEBUG_ERR, "%s: rig_open failed %s\n", __func__,
rigerror(retcode));
return 1;
}
#if 0
status = rig_get_freq(my_rig, RIG_VFO_CURR, &freq);
if (status != RIG_OK) { printf("Get freq failed?? Err=%s\n", rigerror(status)); }
printf("VFO freq. = %.1f Hz\n", freq);
printf("rig reopen is OK\n");
#endif
rig_close(my_rig);
}
};