优化传输文件时的速度

This commit is contained in:
Yu-886 2025-03-25 16:37:37 +08:00
parent 7e98d775b4
commit 8e4f811ee8
4 changed files with 42 additions and 24 deletions

View File

@ -114,13 +114,12 @@ uint32_t WaitPrnBufBytes(stream_data_t* esp_data_interface,uint32_t len, uint32_
}
else
{
vTaskDelay(10/ portTICK_PERIOD_MS);
now_time = xTaskGetTickCount();
if (now_time >= end_time)
{
break;
}
vTaskDelay(1000/ portTICK_PERIOD_MS);
}
}

View File

@ -95,9 +95,9 @@ void Transfer_file(uint8_t *buffer)
static FILE* file;
char* data_buff = NULL;
char file_name[64];
ESP_LOGI(TAG,"Transfer_file");
// ESP_LOGI(TAG,"Transfer_file");
data_len = buffer[1] << 8 | buffer[2];
ESP_LOGI(TAG,"data_len:%d",data_len);
// ESP_LOGI(TAG,"data_len:%d",data_len);
data_buff = malloc(data_len +10);
if(buffer[0] == 0x01) {//文件名
if (WaitPrnBufBytes(&uart_interface_data,data_len,1000,(uint8_t*)&data_buff[0]) == data_len)
@ -124,27 +124,30 @@ void Transfer_file(uint8_t *buffer)
} else if(buffer[0] == 0x02) {//文件内容
// 包数 | (0xff - 包数)| 数据 | crc
// 01 | 00 | data | crc
if (WaitPrnBufBytes(&uart_interface_data,data_len,1000,(uint8_t*)&data_buff[0]) == data_len)
if (WaitPrnBufBytes(&uart_interface_data,data_len,0xffff,(uint8_t*)&data_buff[0]) == data_len)
{
crc_received = data_buff[data_len-1]<<8 | data_buff[data_len-2];
data_buff[data_len-2] = 0;
data_buff[data_len-1] = 0;
//
size_t written = fwrite(&data_buff[2], 1, (data_len - 4), file);
if (written != (data_len - 4)) {
ESP_LOGE("SPIFFS", "fwrite failed, written=%d", written);
} else {
ESP_LOGI("SPIFFS", "Write success: %d bytes", written);
}
// data_buff[data_len-2] = 0;
// data_buff[data_len-1] = 0;
// //
// size_t written = fwrite(&data_buff[2], 1, (data_len - 4), file);
// if (written != (data_len - 4)) {
// ESP_LOGE("SPIFFS", "fwrite failed, written=%d", written);
// } else {
// ESP_LOGI("SPIFFS", "Write success: %d bytes", written);
// }
if(data_buff[0] == 0xff && data_buff[1] == 0xff)
{
fclose(file);
}
// fclose(file);
ESP_LOGI(TAG,"data_buff[0]:%X %X",data_buff[0],data_buff[1]);
}
// ESP_LOGI(TAG,"data_buff[0]:%X %X",data_buff[0],data_buff[1]);
crc_calc = crc16_modbus((uint8_t*)&data_buff[2],data_len-4);
}else{
ESP_LOGE(TAG,"fail:%d",data_len);
}
}
ESP_LOGI(TAG,"crc:%X----%X",crc_calc,crc_received);
// ESP_LOGI(TAG,"crc:%X----%X",crc_calc,crc_received);
free(data_buff);
}
@ -162,21 +165,23 @@ void Read_CMD_task(void *pvParameter)
{
frameStreamRead(&uart_interface_data,&ch,1);
ESP_LOGI(TAG, "Read:%X",ch);
// ESP_LOGI(TAG, "Read:%X,%ld",ch,frameStreamRecLen(&uart_interface_data));
if(ch == 0xAA)
{
if (WaitPrnBufBytes(&uart_interface_data,2,1000,&data[0]) == 2)
if (WaitPrnBufBytes(&uart_interface_data,2,0xffff,&data[0]) == 2)
{
if (data[0] == 0x55) {
ESP_LOGI(TAG, "---Read:%X %X",data[0],data[1]);
// ESP_LOGI(TAG, "---Read:%X %X",data[0],data[1]);
HandleCmd(Cmd_Receiver,data[1]);
}
}
}
// HandleRecChar(ch);
// ESP_LOG_BUFFER_HEXDUMP("Recv str: ", ch, 1, ESP_LOG_INFO);
}
}else{
vTaskDelay(10/ portTICK_PERIOD_MS);
}
}
}

View File

@ -79,7 +79,7 @@ void app_main(void)
init_spiffs();
list_files_in_spiffs();
ESPFlash_Init();
stream_init(&uart_interface_data,1024*10);
stream_init(&uart_interface_data,1024*50);
ESP_LOGI(TAG, "mem free:%ld",esp_get_free_heap_size());
gpio_config_t config = {

View File

@ -36,7 +36,21 @@ static void echo_task(void *arg)
// Write data back to the USB SERIAL JTAG
if (len) {
usb_serial_jtag_write_bytes((const char *) data, len, 20 / portTICK_PERIOD_MS);
// usb_serial_jtag_write_bytes((const char *) data, len, 20 / portTICK_PERIOD_MS);
if(frameStreamRecEmptyLen(&uart_interface_data) < 4096)
{
ESP_LOGE(TAG,"frameStream Be almost full:%d",len);
while (1)
{
vTaskDelay(10/ portTICK_PERIOD_MS);
if(frameStreamRecEmptyLen(&uart_interface_data) > 4096*3)
{
ESP_LOGE(TAG,"frameStream: %ld",frameStreamRecEmptyLen(&uart_interface_data));
break;
}
}
}
frameStreamWrite(&uart_interface_data,&data[0],len);
// uart_write_bytes(EX_UART_NUM, (const char *) data, len);
// data[len] = '\0';
@ -154,7 +168,7 @@ void usb_serial2Uart(void *pvParameters)
//Install UART driver, and get the queue.
uart_driver_install(UART_NUM_1, BUF_SIZE * 2, BUF_SIZE * 2, 20, &uart0_queue, 0);
uart_param_config(UART_NUM_1, &uart_config);
xTaskCreate(echo_task, "USB SERIAL JTAG_echo_task", ECHO_TASK_STACK_SIZE, NULL, 10, NULL);
xTaskCreate(echo_task, "USB SERIAL JTAG_echo_task", ECHO_TASK_STACK_SIZE, NULL, 20, NULL);
//Create a task to handler UART event from ISR
xTaskCreate(uart_event_task, "uart_event_task", 2048*10, NULL, 12, NULL);