فهرست منبع

优化修改ymodem源码

niujiuru 3 روز پیش
والد
کامیت
7302993d83
3فایلهای تغییر یافته به همراه8 افزوده شده و 8 حذف شده
  1. 5 5
      ymodem/ymodem.c
  2. 1 1
      ymodem/ymodem.h
  3. 2 2
      ymodem/ymodem_test.c

+ 5 - 5
ymodem/ymodem.c

@@ -188,13 +188,13 @@ int ymodem_recv_files(const char *dir)
           {
             for(i = 0; i < packet_length; i++) { block[i] = packet_data[PACKET_HEADER+i]; }
             uart_putchar(ACK);
-            sw_log_debug("[%s] <文件: %s, 大小: %u> 收到第%u包数据, 数据包大小: %u", UART_MODULE_NAME, \
+            sw_log_debug("[%s] <文件: %s, 大小: %u> 收到第%u包数据, 数据包大小: %u字节", UART_MODULE_NAME, \
                           file_name, file_size_val, packets_received, packet_length);
 
             if(!sw_dir_exists(dir)) sw_dir_create(dir);
             if(sw_file_update(path, "ab", (char *)block, packet_length) != packet_length) { uart_putchar(CAN); uart_putchar(CAN); serial_close(s_myUart.h, WAITTHRD_SAFEEXIT_TIMEOUT); return -3; }
           }
-          ++packets_received;
+          ++packets_received; // 累加已接收包数
         }
         break;
       default: // receive error
@@ -202,7 +202,7 @@ int ymodem_recv_files(const char *dir)
         uart_putchar(CRC);
       }
 
-      if(file_done)  break; // 文件接收完毕
+      if(file_done)  { if(!session_done) sw_log_info("[%s] 文件: %s 接收完成, 实际大小: %u字节", UART_MODULE_NAME, file_name, sw_file_getSize(path)); break; } // 文件接收完成
     }
 
     if(session_done) break; // 传输会话结束
@@ -324,7 +324,7 @@ int ymodem_send_file(const char *path)
                            comio_data_recv_proc, NULL, NULL);
   if(!s_myUart.h) { ret = -5; goto ret_p; }
 
-  retry = 0; do { ch = uart_getchar(PACKET_TIMEOUT); } while(ch != CRC && ++retry < MAX_ERRORS);
+  retry = 0; do { ch = uart_getchar(PACKET_TIMEOUT); } while(ch != CRC && ++retry < 30);
   if(ch != CRC) { uart_putchar(CAN); uart_putchar(CAN); ret = -6; goto ret_p; }
 
   retry = 0; do
@@ -338,7 +338,7 @@ int ymodem_send_file(const char *path)
     }
     else if((ch == CRC) && (crc_nak)) { crc_nak = 0; continue; } // 接收到CRC, 重发一次0包
     else if((ch != NAK) || (crc_nak)) { break; } // 接收到NAK, 需要重发一次0包
-  } while(++retry < MAX_ERRORS);
+  } while(++retry < MAX_ERRORS && serial_recvThrd_isAlive(s_myUart.h));
 
 ret_p:
   if(file_buf) free(file_buf);

+ 1 - 1
ymodem/ymodem.h

@@ -27,7 +27,7 @@
 #define CRC (0x43)  /* use in place of first NAK for CRC mode */
 
 /* Number of consecutive receive errors before giving up: */
-#define MAX_ERRORS (30)
+#define MAX_ERRORS (5)
 
 #define UART_MODULE_NAME "YMODEM"         /* 串口模块名称 */
 #define UART_DEVICE_NAME  "/dev/ttymxc2"  /* 串口设备名称 */

+ 2 - 2
ymodem/ymodem_test.c

@@ -9,11 +9,11 @@ int main(int argc,char *argv[])
   int send_file_size = sw_file_getSize(send_file_name);
   if(send_file_size <= 0) return -1;
   ret = ymodem_send_file(send_file_name);
-  sw_log_info("发送测试 -> File: %s, Size: %d, Sent bytes: %d", send_file_name, send_file_size, ret);
+  sw_log_info("[%s] 发送测试 -> File: %s, Size: %d, Sent bytes: %d", UART_MODULE_NAME, send_file_name, send_file_size, ret);
 
   // 接收测试
   ret = ymodem_recv_files("./received");
-  sw_log_info("接收测试 <- Received files: %d", ret);
+  sw_log_info("[%s] 接收测试 <- Received files: %d", UART_MODULE_NAME, ret);
 
   return 0;
 }