User Tools

Site Tools


cfnet-fs:analog_input:index

cfnet-fs Analog Input Module

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

$ tree -L 2 /tmp/cfnet-fs/analog-input/
/tmp/cfnet-fs/analog-input/
├── 0
│   └── channel
├── 1
│   └── channel
...

`channel` Subdirectory

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

$ tree /tmp/cfnet-fs/analog-input/0/
/tmp/cfnet-fs/analog-input/0/
└── channel
    ├── 0
    │   ├── amps.bin
    │   ├── amps.txt
    │   ├── raw.bin
    │   ├── raw.txt
    │   ├── volts.bin
    │   └── volts.txt
    ├── 1
    │   ├── amps.bin
    │   ├── amps.txt
    │   ├── raw.bin
    │   ├── raw.txt
    │   ├── volts.bin
    │   └── volts.txt
    ...

Channel Files

Each analog input channel has 6 files:

  • amps.txt: The amperage reading of the analog input channel as an ASCII text string.
  • amps.bin: The amperage reading of the analog input channel as a 16-bit floating point value.
  • raw.txt: The raw digital reading of the analog input channel as an ASCII text string.
  • raw.bin: The raw digital reading of the analog input channel as a 16-bit unsigned integer.
  • volts.txt: The voltage reading of the analog input channel as an ASCII text string out to 4 decimal places.
  • volts.bin: The voltage reading of the analog input channel as a 32-bit floating point value.

Reading any of the channel's files will return the state of the analog 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 analog input 0 channel 3's voltage

Shell Script

$ cat /tmp/cfnet-fs/analog-input/0/channel/3/volts.txt

C#

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

Python

ANALOG_INPUT_0_CHANNEL_3 = "/tmp/cfnet-fs/analog-input/0/channel/3/volts.txt"
 
with open(ANALOG_INPUT_0_CHANNEL_3, "r") as f:
    state = f.read().strip()
print(state)
import struct
 
ANALOG_INPUT_0_CHANNEL_3 = "/tmp/cfnet-fs/analog-input/0/channel/3/volts.bin"
 
with open(ANALOG_INPUT_0_CHANNEL_3, "rb") as f:
    bytes_data = f.read(4)
    state = struct.unpack("<f", bytes_data)[0]
 
print(state)

C

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