diff --git a/bindings/python/test_Hamlib_Rig_class.py b/bindings/python/test_Hamlib_Rig_class.py index 3f60f0ce6..83387836f 100755 --- a/bindings/python/test_Hamlib_Rig_class.py +++ b/bindings/python/test_Hamlib_Rig_class.py @@ -70,6 +70,7 @@ class TestClass: 'scan', 'send_dtmf', 'send_morse', +'send_raw', 'set_ant', 'set_bank', 'set_channel', diff --git a/bindings/python/test_rig.py b/bindings/python/test_rig.py index 5c0c305ec..ee3c28e31 100755 --- a/bindings/python/test_rig.py +++ b/bindings/python/test_rig.py @@ -190,6 +190,23 @@ class TestClass: assert rig.set_spectrum_callback(None) is None + def do_test_send_raw(self, rig): + """rig_send_raw() tests""" + + # When using the Dummy driver, rig.c replies with a copy of the data + test_string_1 = "test string 012\n" + test_string_2 = test_string_1 * 2 + assert rig.send_raw(test_string_1) == test_string_1 + assert rig.send_raw(test_string_2, "\n") == test_string_1 + assert rig.send_raw(test_string_2, bytes([10])) == test_string_1 + + test_bytes_1 = bytes("test bytes\x00\x01\x02", "ASCII") + test_bytes_2 = test_bytes_1 * 2 + assert rig.send_raw(test_bytes_1) == test_bytes_1 + assert rig.send_raw(test_bytes_1, "s") == b"tes" + assert rig.send_raw(test_bytes_2, b"\x02") == test_bytes_1 + + def test_with_open(self, model, rig_file, serial_speed): """Call all the methods that depend on open()""" rig = Hamlib.Rig(model) @@ -211,6 +228,9 @@ class TestClass: self.do_test_antenna(rig) self.do_test_squelch(rig) self.do_test_callback(rig) + # When using the Dummy driver, rig.c replies with a copy of the data + if model == Hamlib.RIG_MODEL_DUMMY: + self.do_test_send_raw(rig) assert rig.close() is None assert rig.state.comm_state == 0 diff --git a/bindings/rig.swg b/bindings/rig.swg index a062bcfef..7d577afc3 100644 --- a/bindings/rig.swg +++ b/bindings/rig.swg @@ -735,6 +735,48 @@ int *rig_spectrum_cb_python(RIG *rig, struct rig_spectrum_line *rig_spectrum_lin return status; } +#ifdef SWIGPYTHON + PyObject *send_raw(PyObject *send_obj, PyObject *term_obj=NULL) + { + char *send, *term; + size_t send_len; + int reply_len = MAX_RETURNSTR; + unsigned char reply_buffer[MAX_RETURNSTR]; + int count; + + if (PyUnicode_Check(send_obj)) { + send = PyUnicode_AsUTF8AndSize(send_obj, &send_len); + } else if (PyBytes_Check(send_obj)) { + PyBytes_AsStringAndSize(send_obj, &send, &send_len); + } else { + SWIG_Python_RaiseOrModifyTypeError("Expected string or bytes for send argument"); + return NULL; + } + + // Using NULL for length in PyUnicode_AsUTF8AndSize() and PyBytes_AsStringAndSize() + // because we can't accept '\0' because there is no length for term in rig_send_raw() + if (PyUnicode_Check(term_obj)) { + term = PyUnicode_AsUTF8AndSize(term_obj, NULL); + } else if (PyBytes_Check(term_obj)) { + PyBytes_AsStringAndSize(term_obj, &term, NULL); + } else if (term_obj == Py_None) { + term = NULL; + } else { + SWIG_Python_RaiseOrModifyTypeError("Expected string or bytes or NULL for term argument"); + return NULL; + } + + count = rig_send_raw(self->rig, send, send_len, reply_buffer, reply_len, term); + self->error_status = count < 0 ? count : RIG_OK; + + if (PyUnicode_Check(send_obj)) { + return PyUnicode_FromStringAndSize(reply_buffer, count); + } else { + return PyBytes_FromStringAndSize(reply_buffer, count); + } + } +#endif + //#ifndef SWIGJAVA /* TODO */ void get_level(setting_t level, vfo_t vfo = RIG_VFO_CURR)