-
Notifications
You must be signed in to change notification settings - Fork 11
/
sensors_example.cpp
63 lines (55 loc) · 1.24 KB
/
sensors_example.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// SPDX-License-Identifier: MIT
/**
Example code to read sensor values from the BBAPI
Copyright (C) 2016 - 2018 Beckhoff Automation GmbH & Co. KG
Author: Patrick Bruenn <p.bruenn@beckhoff.com>
*/
#include "TcBaDevDef.h"
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main()
{
int bbapi_dev = open("/dev/bbapi", O_RDWR);
int status;
if (-1 == bbapi_dev) {
printf("Open '/dev/bbapi' failed\n");
return -1;
}
uint32_t num_sensors;
struct bbapi_struct cmd_num_sensors = {
BIOSIGRP_SYSTEM,
BIOSIOFFS_SYSTEM_COUNT_SENSORS,
NULL,
0,
&num_sensors,
sizeof(num_sensors)
};
status = ioctl(bbapi_dev, BBAPI_CMD, &cmd_num_sensors);
if (status) {
printf("Read number of sensors failed\n");
return status;
}
while (num_sensors > 0) {
char text[256];
SENSORINFO info;
struct bbapi_struct cmd_read_sensor = {
BIOSIGRP_SYSTEM,
num_sensors,
NULL,
0,
&info,
sizeof(info)
};
status = ioctl(bbapi_dev, BBAPI_CMD, &cmd_read_sensor);
if (status) {
printf("Read sensor #%u failed with 0x%x\n",
num_sensors, status);
return status;
}
SENSORINFO_snprintf(&info, text, sizeof(text));
printf("%02d: %s\n", num_sensors, text);
--num_sensors;
}
}