In Betrieb nehmen
7.5.5
DXP-Kanäle über C oder C++ programmieren
Das folgende Beispiel zeigt die Programmierung der digitalen I/O-Kanäle mit Ansi C/C++.
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
// initialize function (use extern for C++)
int access(const char *pfad, int modus);
int main(void) {
//choose DXP / GPIO for connection
char GPIO_IN_FILE[] = "/sys/class/gpio/gpio59";
char GPIO_OUT_FILE[] = "/sys/class/gpio/gpio88";
char input[2]; FILE *fh;
/*
==============================================================
==============
READ:
==================================================================
==========*/
if( access( GPIO_IN_FILE, F_OK ) == -1 )
{
// file doesn't exist!
// export gpio...
if((fh = fopen("/sys/class/gpio/export", "w")) != 0)
{
fputs("59",fh);
fclose(fh);
}
else
{
printf("failed on export to read...\n");
printf("result: %i \n", (int)fh);
return -1;
}
}
// set direction to read...
if((fh =fopen("/sys/class/gpio/gpio59/direction", "w")) != 0)
{
fputs("in",fh);
fclose(fh);
}
else
{
printf("failed on setting direction to read...\n");
return -1;
}
// set active low to read...
if((fh = fopen("/sys/class/gpio/gpio59/active_low", "w")) !=
0)
{
58
Hans Turck GmbH & Co. KG | T +49 208 4952-0 | F +49 208 4952-264 | more@turck.com | www.turck.com