luigifab / python-radexreader

Reader for the RADEX RD1212 and the RADEX ONE Geiger counters.
GNU General Public License v2.0
11 stars 3 forks source link

Using ESP32S3 and USB Host framework (C language) to get the right response #15

Open andyparkers opened 11 months ago

andyparkers commented 11 months ago

Is it correct to send data to Radex ONE this way and try to parse measure?

Form and send a new packet:

struct Keys {
    int key_a;
    int key_b;
    int key_c;
    int key_d;
} keys;

void ProcessKeys(uint8_t* input_array) {
    keys.key_a += 0x04;
    keys.key_c -= 0x04;
    if (keys.key_a > 0xff) {
        keys.key_a -= 0xfe;
        keys.key_b += 0x01;
        if (keys.key_b > 0xff) {
            keys.key_b = 0x00;
        }
        keys.key_c -= 0x01;
    }
    else if (keys.key_c < 0x00) {
        keys.key_c += 0xff;
        keys.key_d -= 0x01;
        if (keys.key_d < 0x00) {
            keys.key_d = 0xff;
        }
    }

    uint8_t data[18] = {0x7b, 0xff, 0x20, 0, 0x06, 0, (uint8_t)(keys.key_a),
                        (uint8_t)(keys.key_b), 0, 0, (uint8_t)(keys.key_c),
                        (uint8_t)(keys.key_d), 0, 0x08, 0x0c, 0, 0xf3, 0xf7};

    for (int i = 0; i < 18; ++i) {
        input_array[i] = data[i];
    }
}

Parsing in callback function by the end of the transfer function:

void usb_transfer_complete(usb_transfer_t *transfer) {
    printf("Transfer status %d, actual number of bytes transferred %d\n", transfer->status, transfer->actual_num_bytes);
    ESP_LOGI(TAG, "Enter transfer callback");
    if (transfer->status == USB_TRANSFER_STATUS_COMPLETED) {
        ESP_LOGI(TAG, "USB transfer completed successfully!");
        uint8_t* data = transfer->data_buffer;
        ESP_LOGI(TAG, "Response: %f", ((float)(data[20]) + (float)(data[21]) * 256
                                        + (float)(data[22]) * 256 * 256) / 100.0);
    }
    else {
        ESP_LOGI(TAG, "USB transfer failed with error code %d", transfer->status);
    }
}

Result:

  1. All this done according to 'init.py' file, but I get weird values in response, for instance, '107861.3' and '146730.2'. What do I do wrong?
  2. Where did you find info about data format? Did you reverse-engineer that?
luigifab commented 11 months ago

Yes, init.py commands are reverse-engineered. I used the official tool to capture usb packets with Wireshark.

If you can read your device measures with my tool but not with your tool... you probably sent a wrong command. Suggestion: try to log your commands and my commands to compare them. Perhaps you must initialize struct Keys, I'm not sure.

andyparkers commented 11 months ago

I provided you with a part of the whole code only, it was initialized beforehand according to init.py. I am wondering how I can log what I get in response, can I just print(hexa)? If I do this, I get: b'z\xff\x80\x16\x00(\x00\x00\x00&\x80\x00\x08\x00\x00\x0c\x00\x00\x00\x0b\x00\x00\x00\xcdr\x01\x00\x0f\x00\x00\x00 \x0c\x85' for 0.11 µSv/h, so the actual index of hexa[20] is [12] (because B in hex is 11 in decimal), it is like shifted right by 8 elements and I won't get full answer log message. I might assume that this happens due to partial data request since you take 42 bytes only in this line:

elif self.com == 'ONEv1':
    return self.serial.read(12 + 21 + 2 + 2 + 2 + 2 + 1)

but anyway this is weird for me. What about my case: recently I have found out that I can't get any response from MCU and can only send data and it is the same as yours in hid_set_report (successfully anyway, callback function on the end of transfer is called), now I am trying to read the response. If you have any thoughts how to do this, please help me out. In documentation there is a send-only example, so I can send data using usb_host_transfer_submit(), but have no idea how to obtain the response.

luigifab commented 11 months ago

Yes, print(hexa) or print(hexa[20]) are beautify ways.

For the Radex One (ONEv1), if I remember correctly, to get a measure:

If usb_host_transfer_submit is the equivalent of hid_set_report, you need an equivalent of hid_get_report.

In the documentation, I don't understand anything, perhaps it's that: the peripherals/usb/host/hid example demonstrates the possibility to receive reports from a USB HID device with several interfaces.