首页 > 解决方案 > 在溢出的情况下我们如何扩展数据包?

问题描述

我的帧大小固定为 7 个值(t_i,{1..7} 中的 i)。每个值存储在 2 个字节上(最大值为 = 2^(16) -1 = 65 535)并除以分辨率因子 10000,即 6.5535ms 作为最大值 (0xFFFF)。超出这些值,2 个字节不再足够,计数器溢出。

在我的测试用例中,我经常遇到溢出,尤其是当我使用非常低的频率时

例如:频率 = 400Hz 和 t_1 = 1ms => t_7 = 1 + 6 * 1/400 = 16 ms > 6.5535 ms

为了处理这个溢出,到目前为止所做的是我们允许溢出,因为它在解码过程中是可检测的。

为了检测此溢出,计算了 2 个连续 (t_i) 之间的时间增量,以查看结果是否低于最大值(0xFFFF 相当于 6.5535 ms),在这种情况下,存在溢出(计数器并根据定义增长,衰变表明异常)。

我正在考虑在溢出的情况下扩展框架(x2)。我该如何继续这样做?

绕过计数器溢出问题的现有行:

    if (timeCounterInMs > 2000000000) { // 2000000000 is the value to be substracted to  the counter to transform long into double
                timeCounterInMs -= 2000000000;
                isCounterOverFlow = true;
    } else {
                isCounterOverFlow = false;
            }

     currentIndexInBytes += 4;
     body[index++] = timeCounterInMs; // body of the frame
            
     for (int j = 0; j < 7; j++) { 
                long timeInTimeRatio = /*transform bytes to long*/;

                currentIndexInBytes += 2;
                body[index++] = timeInTimeRatio; 

                if (timeInTimeRatio == 0 && j != 0) {
                    // the end of the packet
                } else {
                    
                    if (timeInTimeRatio <= theLastValueInPacket && j != 0) { // If the time counter declay => overflow and start to zero

                        timeInTimeRatio = timeInTimeRatio + 65536; // 0xFFFF = 65536
                    }
}
                    

谢谢

标签: javaoverflowcounterpacket

解决方案


推荐阅读