i'm new to the forum and i was hoping to see if someone can help me out.
i'm reading a usb stream, and each packet has a 2 byte counter.(from 0x0000 to 0xffff). this counter is circular.
i have 3 counters: oldCounter, newCounter and auxCounter.
everytime i find a counter on the stream, i store the old newCounter value to auxCounter:
then i read the 2 bytes to the newCounter:
__int16 auxCounter = newCounter;
since the stream can have random counters, i only assign the old newCounter value to oldCounter is it's a valid counter i.e. if the new counter value is greater than the oldCounter value.
newCounter = (__int16)buffer[i+1] << 8;
newCounter += buffer[i+2];
i then check if :
if(newCounter > oldCounter )
My problem is in the 3rd code snippet.
if(newCounter != oldCounter+1)
if for instance the oldCounter has the value 0xFFFF, thew next packet will have counter = 0x0000 (and it's a valid packet) but the condition if(newCounter > oldCounter ) will fail and i'll start to have bad results.
so my question is if anyone can help me out sorting a way to check if the new countr is greater than the old but in a circular way, for instance:
oldCounter = 0xfffff
newCounter = 0x0000
if(newCounter > oldCounter ) -> return true
Thanks in advance guys