“Krupa” <seto@vsnl.com> wrote in message news:b8l97b$2o4$1@inn.qnx.com…
Hi,
At present I am working on development of a serial communication
program. The compiler utilised by me is Watcom C 10.5 with QNX 4.25
The protocol used by me is proprietory protocol that allows transfer
of
data in the form of packets consisting of hexadecimal values. Packet
header
is identifed with the help of 4 hexadecimal values (0xFF, 0xAA, 0x55,
0x00).
I want to detect valid start of packet only if these 4 values are received
on the port after a silent duration. If these 4 bytes arrive without any
gaps in data stream, they need to be included as part of packet instead of
being identified as packet header.
For achieving this, I need to develop a program that comes out of
blocked state when at least one byte is available or if no bytes are
received for a particular duration. I was planning to use dev_read with
m,T,t set to 0,1,0. With this selection my program will return with upto n
bytes when at least one byte is available or 100 millisec has expired. For
parameter ‘T’ I am setting minimum possible value,i.e.1,. My program will
return only after 100millisecs if no bytes are received. How to make my
program return if no bytes are received for 10 millisec? I tried to look
at
various combinations of m,T,t but could not find a way of detecting a
silent
duration less than 100 millisec.
You cannot, here is to code for a version of dev_read that supports better
timer resolution.
/*******************************************************************
Description:
pdev_read : Precision dev_read. Replaces dev_read with more
accurate timeout values (multiple of 10 msecs rather than
100 msecs
Arguments:
(see dev_read documentation)
int fd :
void *buf :
unsigned n :
unsigned min :
unsigned time :
unsigned timemout :
pid_t proxy :
int *armed :
Remarks:
Currently supports only the (M, 0, t) combination, all others are sent
directlyto dev_read (see dev_read documentation for details of combinations)
WARNING: NOT thread safe
Return value:
(see dev_read documentation)
int pdev_read :
*******************************************************************/
int pdev_read (int fd, void *buf, unsigned n, unsigned min, unsigned tim,
unsigned timeout, pid_t proxy, int *armed, unsigned short TimeTick)
{
int NbBytes = 0;
int retval;
unsigned int NbTick = 0;
struct itimerspec timer;
struct sigevent event;
static timer_t id = -1;
static pid_t prox = -1;
char *ptrbuf;
unsigned short tick_size = TickSize();
// int flag = FALSE;
unsigned short Denom;
// HPClock_c ReadTimer;
// double duration;
if (min != 0 && timeout != 0 && tim == 0)
{
// if (timeout < 10)
// flag = TRUE;
// if (flag)
// printf (“tick:%d timetick:%d\n”, tick_size, TimeTick);
if (tick_size > TimeTick)
{
Denom = (unsigned short)(tick_size / TimeTick);
// if (flag)
// printf (" > Denom:%d timeout:%d\n", Denom, timeout);
timeout /= Denom;
timeout++;
// if (flag)
// printf(“new timeout:%d\n”, timeout);
}
else if (tick_size < TimeTick)
{
Denom = (unsigned short)(TimeTick / tick_size);
// if (flag)
// printf (" < Denom:%d timeout:%d\n", Denom, timeout);
timeout++;
timeout *= Denom;
// if (flag)
// printf(“new timeout:%d\n”, timeout);
}
if (prox == -1)
{
prox = qnx_proxy_attach (0, 0, 0, -1);
if (prox == -1)
{
dprintf (“pdev_read: unable to attach proxy\n”);
return -1;
}
}
event.sigev_signo = -prox;
if (id == -1)
{
id = timer_create (CLOCK_REALTIME, &event);
if (id == -1)
{
dprintf (“pdev_read: unable to create timer\n”);
return -1;
}
}
timer.it_value.tv_sec = 0L;
timer.it_value.tv_nsec = (unsigned long)(tick_size*1000L);
timer.it_interval.tv_sec = 0L;
timer.it_interval.tv_nsec = (unsigned long)(tick_size*1000L);
ptrbuf = (char *)buf;
// ReadTimer.start();
NbBytes = dev_read(fd, ptrbuf, n-(unsigned)NbBytes, 0, 0, 0, 0, NULL);
if ( timer_settime (id, 0, &timer, NULL) != EOK ) {
dprintf(“pdev_read: unable to set timer\n”);
return -1;
}
while (NbBytes < (int)min)
{
while ( Receive(prox, 0, 0) != prox ) {
; // do nothing
}
NbTick++;
retval = dev_read(fd, &ptrbuf[NbBytes], n-(unsigned)NbBytes, 0, 0, 0, 0,
NULL);
if (retval == -1)
{
dprintf (“pdev_read : error in dev_read\n”);
return -1;
}
NbBytes += retval;
// if (flag)
// printf (“tick! %d timeout:%d nbbutes:%d min:%d\n”, NbTick, timeout,
NbBytes, min);
if (NbTick == timeout)
{
break;
}
}
// duration = ReadTimer.elapse();
// printf (“read %d bytes after %d ticks (%0.6f)\n”, NbBytes, timeout,
duration);
timer.it_value.tv_sec = 0L;
timer.it_value.tv_nsec = 0L;
timer.it_interval.tv_sec = 0L;
timer.it_interval.tv_nsec = 0L;
if ( timer_settime (id, 0, &timer, NULL) != EOK ) {
dprintf(“pdev_read: unable to set timer\n”);
return -1;
}
// clear pending proxies
while (Creceive(prox, NULL, 0) == prox) {
;
}
return (int)NbBytes;
}
else
{
return dev_read (fd, buf, n, min, tim, timeout, proxy, armed);
}
}
Please help…
Thanks,
Krupa
\