DoIO()
to perform the read operation
CreateTimerIOReq()
and DeleteTimerIOReq()
. This example shows how to communicate with devices in the Portfolio environment. Note that the SampleSystemTime()
and SampleSystemTimeTV()
calls provide a more accurate reading of the system time than the calls used in this example.Example 1: Reading the current time (timerread.c).
#include "types.h"
#include "string.h"
#include "io.h"
#include "device.h"
#include "item.h"
#include "time.h"
#include "stdio.h"
#include "operror.h"
int main(int32 argc, char **argv)
{
Item deviceItem;
Item ioreqItem;
IOReq *ior;
IOInfo ioInfo;
TimeVal tv;
Err err;
deviceItem = OpenNamedDevice("timer",0);
if (deviceItem >= 0)
{
ioreqItem = CreateIOReq(0,0,deviceItem,0);
if (ioreqItem >= 0)
{
ior = (IOReq *)LookupItem(ioreqItem);
memset(&ioInfo,0,sizeof(ioInfo));
ioInfo.ioi_Command = CMD_READ;
ioInfo.ioi_Unit = TIMER_UNIT_USEC;
ioInfo.ioi_Recv.iob_Buffer = &tv;
ioInfo.ioi_Recv.iob_Len = sizeof(tv);
err = DoIO(ioreqItem,&ioInfo);
if (err >= 0)
{
printf("Seconds %u, microseconds
%u\n",tv.tv_Seconds,tv.tv_Microseconds);
}
else
{
printf("DoIO() failed: ");
PrintfSysErr(err);
}
DeleteIOReq(ioreqItem);
}
else
{
printf("CreateIOReq() failed: ");
PrintfSysErr(ioreqItem);
}
CloseNamedDevice(deviceItem);
}
else
{
printf("OpenNamedDevice() failed: ");
PrintfSysErr(deviceItem);
}
return 0;
}
DoIO()
to perform the wait operation
CreateTimerIOReq()
, DeleteTimerIOReq()
, and WaitTime()
. This example shows how to communicate with devices in the Portfolio environment. Example 2: Waiting for a specified time (timersleep.c).
#include "types.h"
#include "string.h"
#include "io.h"
#include "device.h"
#include "item.h"
#include "time.h"
#include "stdio.h"
#include "operror.h"
int main(int32 argc, char **argv)
{
Item deviceItem;
Item ioreqItem;
IOReq *ior;
IOInfo ioInfo;
TimeVal tv;
Err err;
if (argc == 3)
{
tv.tv_Seconds = strtoul(argv[1],0,0);
tv.tv_Microseconds = strtoul(argv[2],0,0);
deviceItem = OpenNamedDevice("timer",0);
if (deviceItem >= 0)
{
ioreqItem = CreateIOReq(0,0,deviceItem,0);
if (ioreqItem >= 0)
{
ior = (IOReq *)LookupItem(ioreqItem);
memset(&ioInfo,0,sizeof(ioInfo));
ioInfo.ioi_Command = TIMERCMD_DELAY;
ioInfo.ioi_Unit = TIMER_UNIT_USEC;
ioInfo.ioi_Send.iob_Buffer = &tv;
ioInfo.ioi_Send.iob_Len = sizeof(tv);
err = DoIO(ioreqItem,&ioInfo);
if (err >= 0)
{
printf("slept\n");
}
else
{
printf("DoIO() failed: ");
PrintfSysErr(err);
}
DeleteIOReq(ioreqItem);
}
else
{
printf("CreateIOReq() failed: ");
PrintfSysErr(ioreqItem);
}
CloseNamedDevice(deviceItem);
}
else
{
printf("OpenNamedDevice() failed: ");
PrintfSysErr(deviceItem);
}
}
else
{
printf("Usage: timersleep <num seconds> <num microseconds>\n");
}
return 0;
}