File: rigfreqwalk.c

package info (click to toggle)
hamlib 4.6.5-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,984 kB
  • sloc: ansic: 262,996; sh: 6,135; cpp: 1,578; perl: 876; makefile: 855; python: 148; awk: 58; xml: 26
file content (166 lines) | stat: -rw-r--r-- 3,970 bytes parent folder | download | duplicates (2)
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
/*
 * Hamlib sample program
 */

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <math.h>

#include <hamlib/rig.h>

#include <hamlib/config.h>

#define HISTORYSIZE 10
double history[HISTORYSIZE];
int nhistory;
int historyinit = 1;

double compute_mean(const double arr[], int length)
{
    double sum = 0.0;

    for (int i = 0; i < length; i++)
    {
        sum += arr[i];
    }

    return sum / length;
}

double sigma(double arr[], int length)
{
    double mean = compute_mean(arr, length);
    double sum_of_squares = 0.0;

    for (int i = 0; i < length; i++)
    {
        sum_of_squares += pow(arr[i] - mean, 2);
    }

    return sqrt(sum_of_squares / length);
}

int main(int argc, const char *argv[])
{
    RIG *my_rig;        /* handle to rig (nstance) */
    int strength;       /* S-Meter level */
    int retcode;        /* generic return code from functions */

    rig_model_t myrig_model;



    if (argc != 8)
    {
        fprintf(stderr, "%s: version 1.0\n", argv[0]);
        fprintf(stderr,
                "Usage: %s [model#] [comport] [baud] [start freq] [stop_freq] [stepsize] [seconds/step]\n",
                argv[0]);
        return 1;
    }

    /* Turn off backend debugging output */
    rig_set_debug_level(RIG_DEBUG_NONE);

    /*
     * allocate memory, setup & open port
     */

//    hamlib_port_t myport;
    myrig_model = atoi(argv[1]);
//    strncpy(myport.pathname, argv[2], HAMLIB_FILPATHLEN - 1);
//    myport.parm.serial.rate = atoi(argv[3]);

    my_rig = rig_init(myrig_model);

    if (!my_rig)
    {
        fprintf(stderr, "Unknown rig num: %u\n", myrig_model);
        fprintf(stderr, "Please check riglist.h\n");
        exit(1); /* whoops! something went wrong (mem alloc?) */
    }

    rig_set_conf(my_rig, rig_token_lookup(my_rig, "rig_pathname"), argv[2]);

    retcode = rig_open(my_rig);

    if (retcode != RIG_OK)
    {
        printf("rig_open: error = %s\n", rigerror(retcode));
        exit(2);
    }

//    printf("Port %s opened ok\n", SERIAL_PORT);

    long freq1, freq2;
    int stepsize, seconds;
    int n = sscanf(argv[4], "%ld", &freq1);
    n += sscanf(argv[5], "%ld", &freq2);
    n += sscanf(argv[6], "%d", &stepsize);
    n += sscanf(argv[7], "%d", &seconds);

    if (n != 4)
    {
        fprintf(stderr, "Error parsing %s/%s/%s/%s as start/stop/step/seconds\n",
                argv[4], argv[5], argv[6], argv[7]);
        return 1;
    }

    printf("Start:%ld Stop:%ld Step:%d Wait:%d\n", freq1, freq2, stepsize, seconds);

    while (1)
    {
        for (long f = freq1; f <= freq2; f += stepsize)
        {
            retcode = rig_set_freq(my_rig, RIG_VFO_CURR, (freq_t)f);

            if (retcode != RIG_OK)
            {
                fprintf(stderr, "%s: Error rig_set_freq: %s\n", __func__, rigerror(retcode));
                return 1;
            }

            sleep(seconds);
            retcode = rig_get_strength(my_rig, RIG_VFO_CURR, &strength);

            if (retcode != RIG_OK)
            {
                int static  once = 1;

                if (once)
                {
                    once = 0;
                    fprintf(stderr, "rig_get_strength error: %s\n", rigerror(retcode));
                }

                strength = 1;
            }

            history[nhistory++] = strength;

            if (historyinit)
            {
                for (int i = 0; i < HISTORYSIZE; ++i) { history[i] = strength; }

                historyinit = 0;
            }

            nhistory %= HISTORYSIZE;
            double s = sigma(history, HISTORYSIZE);
            char timebuf[64];
            rig_date_strget(timebuf, sizeof(timebuf), 1);
            printf("%s\t%ld\t%d\t%f\n", timebuf, f, strength, s);
            fflush(stdout);

        }
    }

    rig_close(my_rig); /* close port */
    rig_cleanup(my_rig); /* if you care about memory */

    printf("port %s closed ok \n", argv[2]);

    return 0;
}