diff --git a/PLAN.md b/PLAN.md index 1a85734..23be68c 100644 --- a/PLAN.md +++ b/PLAN.md @@ -12,9 +12,6 @@ control a radio, with rigctld started when the radio is plugged in. - [ ] Run `TEST.md` against real radios, and mark the `radios.conf` values they confirm `# verified`. Fix or remove the ones they don't -- [ ] Tell Elecraft's radios apart. The K3, KX3, KX2 and K4 all answer `ID017;`, - so `radio` lists all four. Find an Elecraft command whose reply names - the model - [ ] Use PTT through a CM108 sound card in `radio configure` for an interface whose radio has no CAT (a Digirig or AIOC on a handheld): Dire Wolf's `PTT CM108`, Graywolf's cm108 method. HamPack's udev rule diff --git a/TEST.md b/TEST.md index 68f29b9..742d7c6 100644 --- a/TEST.md +++ b/TEST.md @@ -35,6 +35,12 @@ For each radio: 5. If it was recognised, mark its values in `radios.conf` `# verified`. If not, note what `radio detect --probe` printed. +An Elecraft radio (K3, K3S, KX2, KX3, K4) answers `ID;` like the others, so +`radio` also asks it `OM;` and reads the model from its option modules, as +Hamlib does. Check `radio detect --probe` names the right one; if not, note +what `echo "OM;" > /dev/ttyUSB0` gets back (with `cat /dev/ttyUSB0` running +in another terminal, at the radio's baud rate). + Interfaces to try, as well as radios with built-in USB: a Digirig (CP2102 and CM108 behind its own hub, RTS keys PTT), an FTDI CAT cable, and an Icom CI-V cable. diff --git a/radio b/radio index 3e94c42..8ad4652 100755 --- a/radio +++ b/radio @@ -309,7 +309,13 @@ for baud in sys.argv[2:]: unkey() found = re.search(rb"ID(\d{3,4});", ask(b"ID;")) if found: - print(baud, "cat_id", found.group(1).decode()) + # Elecraft radios all answer ID017;. Their option modules (OM;) tell + # them apart: twelve characters, which radio reads + options = "" + if found.group(1) == b"017": + om = re.search(rb"OM ([A-Za-z0-9-]{12});", ask(b"OM;")) + options = om.group(1).decode() if om else "" + print(baud, "cat_id", found.group(1).decode(), options) break # To any address (00) from a computer (E0); the radio answers to E0 from its address found = re.search(rb"\xfe\xfe\xe0(.)\x19\x00.\xfd", ask(bytes.fromhex("fefe00e01900fd")), re.S) @@ -323,16 +329,50 @@ for baud in sys.argv[2:]: # answer, or PROBE_ERROR if the port couldn't be opened probe_port() { local reply - ANSWER_BAUD="" ANSWER_FIELD="" ANSWER_VALUE="" PROBE_ERROR="" + ANSWER_BAUD="" ANSWER_FIELD="" ANSWER_VALUE="" ANSWER_OPTIONS="" PROBE_ERROR="" if ! reply="$(ask_radio "$1")"; then PROBE_ERROR="${reply#error }" PROBE_ERROR="${PROBE_ERROR:-python3 failed}" return 1 fi - read -r ANSWER_BAUD ANSWER_FIELD ANSWER_VALUE <<< "$reply" + read -r ANSWER_BAUD ANSWER_FIELD ANSWER_VALUE ANSWER_OPTIONS <<< "$reply" [ -n "$ANSWER_VALUE" ] } +# Which Elecraft radio's option modules these are (its reply to OM;, after +# "OM "), as Hamlib 4.7's elecraft_open() reads them: a KX2 or KX3 has 0 and +# its model number (1 or 2) last; a K4 has S and 4 among them; a K3S has R; +# anything else is a K3. elecraft_model OPTIONS +elecraft_model() { + if [ "${1:10:1}" = 0 ]; then + case "${1:11:1}" in + 1) echo KX2 ;; + 2) echo KX3 ;; + *) return 1 ;; + esac + elif [[ "$1" == *S* && "$1" == *4* ]]; then + echo K4 + elif [[ "$1" == *R* ]]; then + echo K3S + else + echo K3 + fi +} + +# The radios that could have given the probe's answer, as matching prints +# them: those with its ID, narrowed by an Elecraft radio's option modules +answer_models() { + local models model kind narrowed="" + models="$(matching "$ANSWER_FIELD" "$ANSWER_VALUE")" || return 1 + if [ -n "${ANSWER_OPTIONS:-}" ] && kind="$(elecraft_model "$ANSWER_OPTIONS")"; then + while IFS= read -r model; do + [ "$(conf_field "${model%|*}" elecraft)" = "$kind" ] && narrowed+="$model"$'\n' + done <<< "$models" + [ -z "$narrowed" ] || models="${narrowed%$'\n'}" + fi + echo "$models" +} + # Describe the radios matching prints, in one line models_text() { local line names=() @@ -360,7 +400,7 @@ unknown_answer_text() { # Describe the radio that answered describe_answer() { local models - models="$(matching "$ANSWER_FIELD" "$ANSWER_VALUE")" + models="$(answer_models)" if [ -n "$models" ]; then models_text "$models"; else unknown_answer_text; fi } @@ -415,7 +455,7 @@ identify() { if probe_port "$DEVDIR/$port"; then ID_CAT_PORT="$port" ID_BAUD="$ANSWER_BAUD" if [ -z "$ID_MODELS" ]; then - ID_MODELS="$(matching "$ANSWER_FIELD" "$ANSWER_VALUE")" + ID_MODELS="$(answer_models)" [ -n "$ID_MODELS" ] || ID_ANSWER="$(unknown_answer_text)" ID_HOW="answered on $port at $ANSWER_BAUD baud" fi diff --git a/radios.conf b/radios.conf index 6b8a86c..dd8cc05 100644 --- a/radios.conf +++ b/radios.conf @@ -10,6 +10,10 @@ # may give the same reply # civ= - the radio's default CI-V address in hex (Icom). The radio # gives it in its reply to "read transceiver ID" +# elecraft= - which Elecraft radio it is, as its reply to OM; (its option +# modules) says: K3, K3S, KX2, KX3 or K4. They all answer ID; +# alike, so radio asks OM; too, and reads the model from it +# as Hamlib 4.7's elecraft.c does # flrig= - flrig's name for the radio, for `radio configure --flrig` # (flrig 2.0.12's rigs/ sources). Left out where flrig has # no driver for it, or several (the TS-480's HX and SAT) @@ -139,24 +143,36 @@ hamlib=2041 flrig=TS-890S cat_id=024 -# Elecraft. They all answer ID; as a Kenwood TS-570 would +# Elecraft. They all answer ID; as a Kenwood TS-570 would; their reply to OM; +# tells them apart [Elecraft K3] hamlib=2029 flrig=K3 cat_id=017 +elecraft=K3 + +# flrig's K3 driver drives the K3S, which takes the K3's commands +[Elecraft K3S] +hamlib=2043 +flrig=K3 +cat_id=017 +elecraft=K3S [Elecraft KX3] hamlib=2045 flrig=KX3 cat_id=017 +elecraft=KX3 [Elecraft KX2] hamlib=2044 flrig=KX2 cat_id=017 +elecraft=KX2 [Elecraft K4] hamlib=2047 flrig=K4 cat_id=017 +elecraft=K4 diff --git a/tests/conf.bats b/tests/conf.bats index cca2051..e8d8965 100644 --- a/tests/conf.bats +++ b/tests/conf.bats @@ -117,7 +117,8 @@ EOF } check_radio() { - check_keys hamlib usb_serial cat_id civ flrig + check_keys hamlib usb_serial cat_id civ flrig elecraft + [[ "${CONF[elecraft]:-K3}" =~ ^(K3|K3S|KX2|KX3|K4)$ ]] || problem "elecraft= should be K3, K3S, KX2, KX3 or K4" check_required hamlib [[ "${CONF[hamlib]}" =~ ^[0-9]+$ ]] || problem "hamlib= should be a Hamlib model number" [ -n "${CONF[usb_serial]}${CONF[cat_id]}${CONF[civ]}" ] || problem "needs usb_serial=, cat_id= or civ=" diff --git a/tests/radio.bats b/tests/radio.bats index 4bbdb35..ab75228 100644 --- a/tests/radio.bats +++ b/tests/radio.bats @@ -183,12 +183,12 @@ answers() { [ "${lines[2]}" = " Sound card: none found with it" ] } -@test "lists every radio a reply could come from" { +@test "lists every radio a reply could come from, when nothing narrows it" { usb 1-4 0403:6001 FTDI "FT232R USB UART" A12345 port 1-4 ttyUSB0 answers ttyUSB0 "38400 cat_id 017" run bash "$REPO/radio" --probe - [ "${lines[0]}" = "1. One of: Elecraft K3 (Hamlib model 2029), Elecraft KX3 (Hamlib model 2045), Elecraft KX2 (Hamlib model 2044), Elecraft K4 (Hamlib model 2047), answered on ttyUSB0 at 38400 baud" ] + [ "${lines[0]}" = "1. One of: Elecraft K3 (Hamlib model 2029), Elecraft K3S (Hamlib model 2043), Elecraft KX3 (Hamlib model 2045), Elecraft KX2 (Hamlib model 2044), Elecraft K4 (Hamlib model 2047), answered on ttyUSB0 at 38400 baud" ] } @test "says what answered when it doesn't know the radio" { @@ -330,6 +330,12 @@ while time.monotonic() < end: if sys.argv[2] == "kenwood" and b"ID;" in heard: os.write(master, b"ID0761;") heard = b"" + if sys.argv[2] == "elecraft" and b"ID;" in heard: + os.write(master, b"ID017;") + heard = b"" + if sys.argv[2] == "elecraft" and b"OM;" in heard: + os.write(master, b"OM AP--------02;") + heard = b"" if sys.argv[2] == "icom" and civ_request in heard: # With CI-V USB echo on, the radio echoes the request first os.write(master, civ_request + bytes.fromhex("fefee0a41900a4fd")) @@ -616,7 +622,7 @@ DIREWOLF_CONF() { echo "$HAMPACK_STATE/conf.d/direwolf.conf"; } echo 0001 > "$ROOT_HUB/1-2/1-2.1/serial" run bash "$REPO/radio" configure [ "$status" -eq 1 ] - [[ "$output" == *"Couldn't tell which radio it is: it's one of: Elecraft K3 (Hamlib model 2029), Elecraft KX3"* ]] + [[ "$output" == *"Couldn't tell which radio it is: it's one of: Elecraft K3 (Hamlib model 2029), Elecraft K3S (Hamlib model 2043), Elecraft KX3"* ]] [[ "$output" == *"radio configure --model 2045"* ]] run bash "$REPO/radio" configure --model 2045 @@ -1332,3 +1338,37 @@ FLRIG_AUTOSTART() { echo "$HOME/.config/autostart/hampack-flrig.desktop"; } [ "$in_radio" = "$in_rule" ] || { echo "$device: radio $in_radio, rule $in_rule"; return 1; } done } + +# --- Elecraft --- + +@test "tells Elecraft radios apart by their option modules" { + usb 1-4 0403:6001 FTDI "FT232R USB UART" A12345 + port 1-4 ttyUSB0 + touch "$RADIO_DEV/ttyUSB0" + local options expected + for options in "AP--------02:Elecraft KX3 (Hamlib model 2045)" "----------01:Elecraft KX2 (Hamlib model 2044)" \ + "AP-S----4---:Elecraft K4 (Hamlib model 2047)" "AP-S-R------:Elecraft K3S (Hamlib model 2043)" \ + "AP-S--------:Elecraft K3 (Hamlib model 2029)"; do + expected="${options#*:}" + answers ttyUSB0 "38400 cat_id 017 ${options%%:*}" + run bash "$REPO/radio" probe "$RADIO_DEV/ttyUSB0" + [ "$output" = "$expected, at 38400 baud" ] || { echo "${options%%:*}: $output"; return 1; } + done +} + +@test "configure sets up an Elecraft without --model, once its option modules say which" { + configurable + echo 0001 > "$ROOT_HUB/1-2/1-2.1/serial" + answers ttyUSB0 "38400 cat_id 017 AP--------01" + run bash "$REPO/radio" configure + [ "$status" -eq 0 ] + [[ "${lines[0]}" == "Setting up the Elecraft KX2 (Hamlib model 2044), on "* ]] + grep -q -- "-m 2044 " "$HOME/.config/systemd/user/hampack-rigctld.service" +} + +@test "probe asks a real Elecraft for its option modules" { + fake_radio elecraft + run bash "$REPO/radio" probe "$PTY" + [ "$status" -eq 0 ] + [ "$output" = "Elecraft KX3 (Hamlib model 2045), at 38400 baud" ] +}