Beispiel (OS NT/2000/XP)/CE
///////////////////////////////////////////////////////////////////////////////
//
// Beckhoff Automation
//
// TwinCAT® I/O sample program
//
///////////////////////////////////////////////////////////////////////////////
#include "stdio.h"
#include "conio.h"
#include "windows.h"
#include "mmsystem.h"
#include "TCatIoApi.h" // header file shipped with TwinCAT® I/O
#include "Task1ms.h" // TwinCAT® System Manager generated
#include "Task10ms.h" // TwinCAT® System Manager generated
#define IOERR_IOSTATEBUSY 0x2075
#define TASK1MS_DELAY 1 // ms
#define TASK10MS_DELAY 10 // ms
#define DELAY_IN_100NS(x) (x*10000)
PTask1ms_Outputs pT1msOut;
PTask1ms_Inputs pT1msIn;
PTask10ms_Outputs pT10msOut;
PTask10ms_Inputs pT10msIn;
///////////////////////////////////////////////////////////////////////////////
void CALLBACK TimerProc1( UINT uID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD
dw2 )
{
static int i=0;
long nError;
static __int64 nLastCpuTime=0, nActCpuTime=0;
TCatIoGetCpuTime( &nActCpuTime ); // 0.5 ms, filter extra events
if ( (nActCpuTime - nLastCpuTime) > (DELAY_IN_100NS(TASK1MS_DELAY)/2) )
{
nLastCpuTime =nActCpuTime;
// first try to get the inputs and test if input update succeeded
if ( (nError = TCatIoInputUpdate( TASK1MS_PORTNUMBER )) == 0 )
{
// do your calculation and logic
pT1msOut->AnalogOut = pT1msIn->AnalogIn;
// start the I/O update and field bus cycle
TCatIoOutputUpdate( TASK1MS_PORTNUMBER );
}
else
printf( "TCatInputUpdate(%d) %d failed with 0x%x !\n",
TASK1MS_PORTNUMBER, i++, nError );
}
}
///////////////////////////////////////////////////////////////////////////////
void CALLBACK TimerProc2( UINT uID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD
dw2 )
{
static int i=0;
long nError;
static __int64 nLastCpuTime=0, nActCpuTime=0;
TCatIoGetCpuTime( &nActCpuTime ); // 5 ms, filter extra events
if ( (nActCpuTime - nLastCpuTime) > (DELAY_IN_100NS(TASK10MS_DELAY)/2) )
{
nLastCpuTime = nActCpuTime;
// try to get the inputs and test if input update succeeded
if ( (nError = TCatIoInputUpdate( TASK10MS_PORTNUMBER )) == 0 )
{
// optionally test the device state, zero is ok.
if ( pT10msIn->IoDeviceState != 0 )
printf( "I Device Error !\n" );
else
{
// do your calculation and logic
pT10msOut->DigitalOut[0] = pT10msIn->DigitalIn[0];
pT10msOut->DigitalOut[1] = pT10msIn->DigitalIn[1];
// start the I/O update and field bus cycle
TCatIoOutputUpdate( TASK10MS_PORTNUMBER );
}
}
else
printf("TCatInputUpdate(%d) %d failed with 0x%x !\n",
TASK10MS_PORTNUMBER, i++, nError );
}
}
///////////////////////////////////////////////////////////////////////////////
void main()
{
MMRESULT idTimer1, idTimer2;
timeBeginPeriod(1);
// always call TCatIoOpen first.
if ( TCatIoOpen() == 0 )
{
// get the process image pointer
if ( TCatIoGetOutputPtr(TASK1MS_PORTNUMBER, (void**)&pT1msOut,sizeof(Task1ms_Outputs) ) == 0 &&
TCatIoGetInputPtr( TASK1MS_PORTNUMBER,(void**)&pT1msIn, sizeof(Task1ms_Inputs) ) == 0 &&
TCatIoGetOutputPtr(TASK10MS_PORTNUMBER,(void**)&pT10msOut, sizeof(Task10ms_Outputs) )== 0 &&
TCatIoGetInputPtr(TASK10MS_PORTNUMBER,(void**)&pT10msIn, sizeof(Task10ms_Inputs) )== 0 )
{
//do some other initialization
idTimer1 = timeSetEvent( TASK1MS_DELAY, 0,TimerProc1, 0, TIME_PERIODIC|TIME_CALLBACK_FUNCTION );
idTimer2 = timeSetEvent(TASK10MS_DELAY, 0, TimerProc2, 0, TIME_PERIODIC|TIME_CALLBACK_FUNCTION );
//just wait for the end
getch();
// events are no longer needed
timeKillEvent(idTimer1 );
timeKillEvent( idTimer2 );
}
// free resources
TCatIoClose();
}
timeEndPeriod(1);
}