User Tools

Site Tools


cfnet-fs:digital_input:index

cfnet-fs Digital Input Module

Each digital input module is represented under the cfnet-fs mount point by a directory at {mount-point}/digital-input/{module_address}/

$ tree -L 2 /tmp/cfnet-fs/digital-input/
/tmp/cfnet-fs/digital-input/
├── 0
│   ├── channel
│   ├── state.bin
│   └── state.txt
├── 1
│   ├── channel
│   ├── state.bin
│   └── state.txt
...

Module `state` Files

Each digital input module has 2 state files:

  • state.txt: The state of all 16 channels as an ASCII string of 1s and 0s.
  • state.bin: The state of all 16 channels as a 16-bit, unsigned binary number.

Reading from either of the state files will return the state of all digital input channels.

Text files are convenient when the state is directly obtained from or displayed to users, for example, in shell scripts. Binary files are more convenient when the state requires additional processing such as math and logic.

Example: Read all channels of digital input module 0

Shell Script

$ cat /tmp/cfnet-fs/digital-input/0/state.txt

C#

const string DIGITAL_INPUT_0 = "/tmp/cfnet-fs/digital-input/0/state.txt";
 
string state = File.ReadAllText(DIGITAL_INPUT_0).Trim();
Console.WriteLine(state);
const string DIGITAL_INPUT_0 = "/tmp/cfnet-fs/digital-input/0/state.bin";
 
var bytes = File.ReadAllBytes(DIGITAL_INPUT_0);
var state = BitConverter.ToUInt16(bytes, 0);
Console.WriteLine(state);

Python

DIGITAL_INPUT_0 = "/tmp/cfnet-fs/digital-input/0/state.txt"
 
state = open(DIGITAL_OUTPUT_0).read().strip()
print(state)
DIGITAL_OUTPUT_0 = "/tmp/cfnet-fs/digital-input/0/state.bin"
 
with open(DIGITAL_INPUT_0, "rb") as f:
    data = f.read(2)
    state = (data[1] << 8) | data[0]    # little-endian
 
print(state)

C

#include <stdio.h>
 
#define DIGITAL_INPUT_0 "/tmp/cfnet-fs/digital-input/0/state.txt"
 
int main(void)
{
    FILE *f = fopen(DIGITAL_INPUT_0, "r");
 
    // 16 plus the null-terminating character
    char buffer[17] = {0};
    fgets(buffer, sizeof(buffer) - 1, f);
    printf("%s\n", buffer);
 
    fclose(f);
    return 0;
}
#include <stdio.h>
 
#define DIGITAL_INPUT_0"/tmp/cfnet-fs/digital-input/0/state.bin"
 
int main(void)
{
    FILE *f = fopen(DIGITAL_INPUT_0, "rb");
 
    uint16_t state;
    fread(&state, sizeof(uint16_t), 1, f);
    printf("%u\n", state);
 
    fclose(f);
    return 0;
}

`channel` Subdirectory

Each digital input channel is represented under the cfnet-fs mount point by a directory at {mount-point}/digital-input/{module_address}/channel/{channel_address}/

$ tree /tmp/cfnet-fs/digital-input/0/
/tmp/cfnet-fs/digital-input/0/
├── channel
│   ├── 0
│   │   ├── state.bin
│   │   └── state.txt
│   ├── 1
│   │   ├── state.bin
│   │   └── state.txt
│   ...

Channel `state` Files

Each digital input channel has 2 state files:

  • state.txt: The state of the digital input channel as an ASCII 1 or 0.
  • state.bin: The state of the digital input channel as a single binary byte: 0: Off. Non-Zero: On.

Reading to either of the state files will return the state of the digital input channel.

Text files are convenient when the state is directly obtained from or displayed to users, for example, in shell scripts. Binary files are more convenient when the state requires additional processing such as math and logic.

Example: Read digital input 0 channel 3

Shell Script

$ cat /tmp/cfnet-fs/digital-output/0/channel/3/state.txt

C#

const string DIGITAL_INPUT_0_CHANNEL_3 = "/tmp/cfnet-fs/digital-input/0/channel/3/state.txt";
 
string state = File.ReadAllText(DIGITAL_INPUT_0_CHANNEL_3).Trim();
Console.WriteLine(state);
const string DIGITAL_INPUT_0_CHANNEL_3 = "/tmp/cfnet-fs/digital-input/0/channel/3/state.bin";
 
var bytes = File.ReadAllBytes(DIGITAL_INPUT_0_CHANNEL_3);
var state = BitConverter.ToBoolean(bytes, 0);
Console.WriteLine(state);

Python

DIGITAL_INPUT_0_CHANNEL_3 = "/tmp/cfnet-fs/digital-input/0/channel/3/state.txt"
 
with open(DIGITAL_INPUT_0_CHANNEL_3) as f:
    state = f.read().strip()
 
print(state)
DIGITAL_INPUT_0_CHANNEL_3 = "/tmp/cfnet-fs/digital-input/0/channel/3/state.bin"
 
with open(DIGITAL_INPUT_0_CHANNEL_3, "rb") as f:
    bytes_data = f.read(1)
    state = bool(bytes_data[0])
 
print(state)

C

#include <stdio.h>
 
#define DIGITAL_INPUT_0_CHANNEL_3 "/tmp/cfnet-fs/digital-input/0/channel/3/state.txt"
 
int main(void)
{
    FILE *f = fopen(DIGITAL_INPUT_0_CHANNEL_3, "r");
 
    char buffer[3] = {0};
    fgets(buffer, sizeof(buffer), f);
    printf("%s", buffer);
 
    fclose(f);
    return 0;
}
#include <stdio.h>
 
#define DIGITAL_INPUT_0_CHANNEL_3 "/tmp/cfnet-fs/digital-input/0/channel/3/state.bin"
 
int main(void)
{
    FILE *f = fopen(DIGITAL_INPUT_0_CHANNEL_3, "rb");
 
    unsigned char state;
    fread(&state, sizeof(unsigned char), 1, f);
    printf("%u\n", state);
 
    fclose(f);
    return 0;
}
cfnet-fs/digital_input/index.txt · Last modified: by 127.0.0.1