User Tools

Site Tools

한국어

moacon:comgetinterval:index

comGetInterval

u8 comGetInterval (u8 comCh)

comCh : Channel number
returns the time interval in milliseconds between packets

Gets the time interval in milliseconds between packets of data received on channel comCh.

Often packets of data are sent wrapped in a start and stop code, and therefore, when parsing data, it is easy to know when all data has been received.

However, one problem with this approach is the start and stop codes cannot be used in the data (payload) unless they are escaped.

To get around this limitation, some protocols separate packets by intervals of time. Modbus RTU is one example of this method.

comGetInterval is needed to implement this kind of protocol. When a packet of data arrives in the receive buffer, comGetInterval will return the amount of time in milliseconds that has elapsed between when data was removed from the receive buffer and when data arrived in the receive buffer (up to 255ms). To accurately measure the time between packets, packets should be removed from the receive buffer immediately when they arrive.

#include "moacon500.h"
void cmain(void)
{
 u8 deltaT = 0;
 char c[64];
 openCom(0, 115200, C8N1);         // Channel 0, 115200 Baud,
                                   // data bits: 8, no parity, stop bits: 1
 while (1)                         // Repeat forever
 {
   if(comLen(0) >= 64)             // if data exists in the receive buffer
   {
     deltaT = comGetInterval(0);   // Time since last packet was received
     comGets(0, c, 64);            // Remove data from receive buffer
   }
 }
}

In the program above, comGets is used to remove data from the receive buffer as it arrives. comGetInterval will report the amount of time in milliseconds since the last comGets function call.

go MOACON home

moacon/comgetinterval/index.txt · Last modified: 2016/04/14 11:07 (external edit)