I've tried to send a read request every 50ms but this gives me a response with 64bytes most of the time, even if the device is not sending anything.
In my callback, I always have status == USB_TRANSFER_STATUS_COMPLETED and actual_num_bytes == 64.
What I'm doing wrong?
Edit.
What is more, I sniffed the data with Wireshark when the printer is connected to the PC and this is working fine. All data is exchanged with BULK IO/OUT frames.
Here are some functions to show how I'm trying to read.
Code: Select all
static void receive_cb(usb_transfer_t *transfer)
{
xEventGroupSetBits(event_group, DATA_RECEIVED);
}
esp_err_t read_request(void)
{
esp_err_t err;
if (xfer_in == NULL || endpoint_in_address == 0 || last_driver == NULL)
{
return ESP_OK;
}
xfer_in->num_bytes = 64;
xfer_in->device_handle = last_driver->dev_hdl;
xfer_in->bEndpointAddress = endpoint_in_address;
xfer_in->callback = receive_cb;
xfer_in->timeout_ms = 1000;
xfer_in->context = last_driver;
err = usb_host_transfer_submit(xfer_in);
if (err != ESP_OK)
{
ESP_LOGW(TAG, "read_request failed - 0x%04X", err);
}
return ESP_OK;
}
static void read_poll_task(void *arg)
{
ESP_LOGI(TAG, "read_poll task started.");
while (1)
{
if (xfer_in && xfer_in->status == USB_TRANSFER_STATUS_COMPLETED)
{
read_request();
}
if (xEventGroupWaitBits(event_group, DATA_RECEIVED, pdTRUE, pdTRUE, 50 / portTICK_PERIOD_MS) != 0)
{
if (xfer_in->actual_num_bytes == 64)
{
//ESP_LOGI(TAG, "Bytes = 64 - ignoring");
vTaskDelay(pdMS_TO_TICKS(50));
continue;
}
if (xfer_in && xfer_in->status == USB_TRANSFER_STATUS_COMPLETED && xfer_in->actual_num_bytes > 0)
{
uint8_t answer[128];
//ESP_LOGI(TAG, "USB %d bytes received", xfer_in->actual_num_bytes);
ESP_LOG_BUFFER_HEXDUMP("<==", xfer_in->data_buffer, xfer_in->actual_num_bytes, ESP_LOG_INFO)
//send it over uart....
}
}
else
{
vTaskDelay(pdMS_TO_TICKS(50));
}
}
}
Edit2.
I tried many configurations:
- request IO transfer only after a successful OUT transfer
- request IN transfer in separate task every 50/100ms
- change IDF from 4.4 to 5.0
The effect is always the same. While it starts working it is working all the time, and transmission is in both directions. But if it is not working I got no response, sometimes I got the device gone event, and sometimes after a few frames send without a response the transmission start working again and everything works as it should.
Writing to the device is working all the time because I have zero problems with printing anything I want.
Edit3.
I tried also this example:
https://github.com/touchgadget/esp32-us ... rinter.ino
Output was working so I can do some prints. Then the code a bit to send a paper status query every 1 second. And once I got this working and every query I got the correct response. But then I got zero response 0x12.
What I found is that the printer receives these commands correctly and buffers this answer on the printer side, because when I restart the board and send a query for paper status I'm getting answers for frames sent before restart like 0x12 0x12 0x12 0x12 0x12 0x12 ...
So it looks like ESP32 has a problem with transfer IN for some reason.