How can I access the digital I/Os on the "netFIELD Compact X8MC Next" device?

Q

How can I access the digital I/Os on the "netFIELD Compact X8MC Next" device?

A

General information

The device "netFIELD Compact X8MC Next" supports four 24-volt compatible digital inputs and four digital outputs.

The GPIO sysfs interface for userspace access to the 4DI/4DO (symbolically "ied_di4_o4") is configured in the device tree bindings to be exposed under "/var/platform/".

The following GPIOs are exposed under "/var/platform/" (use "ls /var/platform/" to just list the subfolders)

DIs:

  ied-di4o4_i0

  ied-di4o4_i1

  ied-di4o4_i2

  ied-di4o4_i3

DOs:

  ied-di4o4_o0

  ied-di4o4_o1

  ied-di4o4_o2

  ied-di4o4_o3

Read/Write access

Standard file access functions are used to access the sysfs interface.

Example in C using stdio functions

The fopen() function binds a named GPIO to an access stream. 
Read access:

  fp = fopen("/var/platform/ied-di4o4_i0", "rb")

Write access:

  fp = fopen("/var/platform/ied-di4o4_d0", "wb")

The fread() function reads the current state of a DI signal

  char buffer[32];
bytesread = fread(buffer, sizeof(char), sizeof(buffer)-1, fp);
fclose(fp);
if (!bytesread){
/*error*/
} else {
/* success evaluate bytesread[xxx]*/
}  

The fwrite() function writes the state "1" to a DO signal

  byteswrite = fwrite("1", sizeof("1"), fp);
fclose(fp);
if (!byteswrite){
/*error*/
} else {
/* success */
}  

Example as command line

Writing the state "1" = on to a DO signal

  echo 1 | sudo tee /var/platform/ied-di4o4_o2

Reading and outputting the current state of a DI signal

  cat /var/platform/ied-di4o4_i1