upacker icon indicating copy to clipboard operation
upacker copied to clipboard

数据包超长得情况下丢包有问题

Open Sunxiaowei123370 opened this issue 4 years ago • 16 comments

else if (packer->state == 2) { //长度信息 packer->flen |= (uint16_t)d << 8; packer->calc ^= d & 0x3F;

   //数据包超长得情况下直接丢包
    if ((packer->flen & 0x3FFF) > MAX_PACK_SIZE)
    {
        packer->state = 0;
    }
    packer->state = 3;
    packer->cnt = 0;
}

问题: 当数据包超长时并不会直接丢包因为底下又重新给 state 赋值导致state = 0被覆盖。 解决方法: 在 packer->state = 0; 的下一行直接return 0;您看这样改是否妥当。

Sunxiaowei123370 avatar Mar 13 '21 09:03 Sunxiaowei123370

是的,如果包长太长,会继续遍历完整个包; 但是这里不能直接丢包,如果当前正在处理的数据包含了下一包的包头,就是沾包的情况,直接return下一包会被丢掉; 能跳过肯定是最好的,但是也不知道跳过多少才对,万一跳过下一包的包头,干脆还是遍历一下,影响不大

------------------ 原始邮件 ------------------ 发件人: "aeo123/upacker" @.>; 发送时间: 2021年3月13日(星期六) 下午5:42 @.>; @.***>; 主题: [aeo123/upacker] 数据包超长得情况下丢包有问题 (#3)

else if (packer->state == 2) { //长度信息 packer->flen |= (uint16_t)d << 8; packer->calc ^= d & 0x3F; //数据包超长得情况下直接丢包 if ((packer->flen & 0x3FFF) > MAX_PACK_SIZE) { packer->state = 0; } packer->state = 3; packer->cnt = 0; }
问题: 当数据包超长时并不会直接丢包因为底下又重新给 state 赋值导致state = 0被覆盖。 解决方法: 在 packer->state = 0; 的下一行直接return 0;您看这样改是否妥当。

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or unsubscribe.

aeo123 avatar Mar 13 '21 09:03 aeo123

最后怎么解决的啊?我也发现特别长以后丢包

hcb900330 avatar Aug 05 '21 07:08 hcb900330

数据报不到4 k 就经常有 传输不到的数据包

hcb900330 avatar Aug 05 '21 08:08 hcb900330

如果是数据丢包,考虑下串口硬件和bsp得问题

------------------ 原始邮件 ------------------ 发件人: @.>; 发送时间: 2021年8月5日(星期四) 下午4:33 收件人: @.>; 抄送: @.>; @.>; 主题: Re: [aeo123/upacker] 数据包超长得情况下丢包有问题 (#3)

数据报不到4 k 就经常有 传输不到的数据包

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or unsubscribe. Triage notifications on the go with GitHub Mobile for iOS or Android.

aeo123 avatar Aug 05 '21 08:08 aeo123

短包发送每个都可以,长包一旦出问题 ,后面短包也会出问题 ,我用的市QT 与RT thread系统的设备通讯

hcb900330 avatar Aug 05 '21 08:08 hcb900330

小于1k的数据没啥问题 大了以后汇有概率的接收不到 我用的cjson字符串传输

hcb900330 avatar Aug 05 '21 09:08 hcb900330

MAX_PACK_SIZE你设置的是多大?

发件人: hcb900330 发送时间: 2021-08-05 17:19 收件人: aeo123/upacker 抄送: Sunxiaowei123370; Author 主题: Re: [aeo123/upacker] 数据包超长得情况下丢包有问题 (#3) 小于1k的数据没啥问题 大了以后汇有概率的接收不到 我用的cjson字符串传输 — You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe. Triage notifications on the go with GitHub Mobile for iOS or Android.

Sunxiaowei123370 avatar Aug 06 '21 02:08 Sunxiaowei123370

1k到8k都有使用。最近用的是4k,我一般是配合dma来收发处理,在stm32和esp32上面使用

------------------ 原始邮件 ------------------ 发件人: @.>; 发送时间: 2021年8月6日(星期五) 上午10:56 收件人: @.>; 抄送: @.>; @.>; 主题: Re: [aeo123/upacker] 数据包超长得情况下丢包有问题 (#3)

MAX_PACK_SIZE你设置的是多大?

发件人: hcb900330 发送时间: 2021-08-05 17:19 收件人: aeo123/upacker 抄送: Sunxiaowei123370; Author 主题: Re: [aeo123/upacker] 数据包超长得情况下丢包有问题 (#3) 小于1k的数据没啥问题 大了以后汇有概率的接收不到 我用的cjson字符串传输 — You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe. Triage notifications on the go with GitHub Mobile for iOS or Android.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or unsubscribe. Triage notifications on the go with GitHub Mobile for iOS or Android.

aeo123 avatar Aug 06 '21 02:08 aeo123

软件包里面有一个宏,默认是1K,这个你有没有改

发件人: aeo123 发送时间: 2021-08-06 10:59 收件人: aeo123/upacker 抄送: Sunxiaowei123370; Author 主题: Re: [aeo123/upacker] 数据包超长得情况下丢包有问题 (#3) 1k到8k都有使用。最近用的是4k,我一般是配合dma来收发处理,在stm32和esp32上面使用

------------------ 原始邮件 ------------------ 发件人: @.>; 发送时间: 2021年8月6日(星期五) 上午10:56 收件人: @.>; 抄送: @.>; @.>; 主题: Re: [aeo123/upacker] 数据包超长得情况下丢包有问题 (#3)

MAX_PACK_SIZE你设置的是多大?

发件人: hcb900330 发送时间: 2021-08-05 17:19 收件人: aeo123/upacker 抄送: Sunxiaowei123370; Author 主题: Re: [aeo123/upacker] 数据包超长得情况下丢包有问题 (#3) 小于1k的数据没啥问题 大了以后汇有概率的接收不到 我用的cjson字符串传输 — You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe. Triage notifications on the go with GitHub Mobile for iOS or Android.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or unsubscribe. Triage notifications on the go with GitHub Mobile for iOS or Android. — You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe. Triage notifications on the go with GitHub Mobile for iOS or Android.

Sunxiaowei123370 avatar Aug 06 '21 03:08 Sunxiaowei123370

默认的最大的配置我改了16384

hcb900330 avatar Aug 08 '21 11:08 hcb900330

static int uart_dma_sample(int argc, char *argv[]) { rt_err_t ret = RT_EOK; char uart_name[RT_NAME_MAX]; //static char msg_pool[256]; char str[] = "hello RT-Thread!\r\n";

if (argc == 2)
{
    rt_strncpy(uart_name, argv[1], RT_NAME_MAX);
}
else
{
    rt_strncpy(uart_name, SAMPLE_UART_NAME, RT_NAME_MAX);
}
	
	

/* 查找串口设备 */
serial = rt_device_find(uart_name);
	
	
if (!serial)
{
    rt_kprintf("find %s failed!\n", uart_name);
    return RT_ERROR;
}


	
	
	rx_mq=rt_mq_create("rx_mq",sizeof(struct rx_msg),1025,RT_IPC_FLAG_FIFO);


/* 以 DMA 接收及轮询发送方式打开串口设备 */
rt_device_open(serial, RT_DEVICE_FLAG_DMA_RX);
/* 设置接收回调函数 */
rt_device_set_rx_indicate(serial, uart_input);
	//    /* 发送字符串 */
	//    rt_device_write(serial, 0, str, (sizeof(str) - 1));

/* 创建 serial 线程 */
rt_thread_t thread = rt_thread_create("serial", serial_thread_entry, RT_NULL, 1024, 25, 10);
/* 创建成功则启动线程 */
if (thread != RT_NULL)
{
    rt_thread_startup(thread);
}
else
{
    ret = RT_ERROR;
}
	
	
	upacker_init(&msg_packer, handle_cb, uart_send);
	

return ret;

} /* 导出到 msh 命令列表中 */ MSH_CMD_EXPORT(uart_dma_sample, uart device dma sample);

hcb900330 avatar Aug 08 '21 11:08 hcb900330

/* 接收数据回调函数 */ static rt_err_t uart_input(rt_device_t dev, rt_size_t size) { struct rx_msg msg; rt_err_t result; msg.dev = dev; msg.size = size;

result = rt_mq_send(rx_mq, &msg, sizeof(msg));
if ( result == -RT_EFULL)
{
    /* 消息队列满 */
    rt_kprintf("message queue full!\n");
}
return result;

}

static void serial_thread_entry(void *parameter) { struct rx_msg msg; rt_err_t result; rt_uint32_t rx_length; static char rx_buffer[RT_SERIAL_RB_BUFSZ + 1];

while (1)
{
    rt_memset(&msg, 0, sizeof(msg));
    /* 从消息队列中读取消息*/
    result = rt_mq_recv(rx_mq, &msg, sizeof(msg),100);
    if (result == RT_EOK)
    {
        /* 从串口读取数据*/
        rx_length = rt_device_read(msg.dev, 0, rx_buffer, msg.size);
        //丢到packer解析,成功了调用callback
        upacker_unpack(&msg_packer, (uint8_t *)rx_buffer, msg.size);
        /* 打印数据 */
       //rt_kprintf("%s\n",rx_buffer);
    }
}

}

hcb900330 avatar Aug 08 '21 11:08 hcb900330

static void handle_cb(uint8_t *d, uint16_t size) { //接收到payload //rt_kprintf("pack len%d", size); char *p=rt_malloc(size); rt_kprintf("size:%d\r\n",size); rt_memcpy(p,d,size); rt_kprintf("%s\r\n",p); rt_free(p);

}

hcb900330 avatar Aug 08 '21 11:08 hcb900330

//使用动态内存 #define USE_DYNAMIC_MEM 1

#if USE_DYNAMIC_MEM #define UP_MALLOC rt_malloc #define UP_FREE rt_free #endif

#define MAX_PACK_SIZE 16384 //最长消息长度,最大可用14位即16384 #define STX_L 0X55 //数据包头

hcb900330 avatar Aug 08 '21 11:08 hcb900330

最后把这个改大了 RT_SERIAL_RB_BUFSZ 1024*17 好像就好多了

hcb900330 avatar Aug 09 '21 01:08 hcb900330

那就是连续 发的包太长,超过了驱动的接收缓存了。确实加长就行

------------------ 原始邮件 ------------------ 发件人: @.>; 发送时间: 2021年8月9日(星期一) 上午9:26 收件人: @.>; 抄送: @.>; @.>; 主题: Re: [aeo123/upacker] 数据包超长得情况下丢包有问题 (#3)

最后把这个改大了 RT_SERIAL_RB_BUFSZ 1024*17 好像就好多了

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or unsubscribe. Triage notifications on the go with GitHub Mobile for iOS or Android.

aeo123 avatar Aug 09 '21 01:08 aeo123