From 0186160f06c055395025e56c1aba660c34ef76dc Mon Sep 17 00:00:00 2001 From: Arne Tarara Date: Fri, 20 Oct 2023 14:12:19 +0200 Subject: [PATCH] Quality patch --- .../psu/energy/ac/mcp/machine/source.c | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/metric_providers/psu/energy/ac/mcp/machine/source.c b/metric_providers/psu/energy/ac/mcp/machine/source.c index 8119389ab..4029a4429 100755 --- a/metric_providers/psu/energy/ac/mcp/machine/source.c +++ b/metric_providers/psu/energy/ac/mcp/machine/source.c @@ -36,17 +36,17 @@ const unsigned char f511_read_active_power2[] = { 0x41, 0x0, 0x1a, 0x4E, 4 }; const unsigned char f511_set_accumulation_interval[] = { 0x41, 0x00, 0xA8, 0x4D, 2, 0x00, 0x00 }; +/* This variable ist just global for consitency with our other metric_provider source files */ static unsigned int msleep_time=1000; enum mcp_states { init, wait_ack, get_len, get_data, validate_checksum }; enum mcp_states mcp_state = wait_ack; -int fd; - int init_serial(const char *port, int baud) { struct termios tty; + int fd; fd = open(port, O_RDWR | O_NOCTTY | O_SYNC); if (fd < 0) { @@ -80,7 +80,7 @@ int init_serial(const char *port, int baud) if (tcsetattr(fd, TCSANOW, &tty) != 0) { return -1; } - return 0; + return fd; } int mcp_cmd(unsigned char *cmd, unsigned int cmd_length, unsigned char *reply, int fd) @@ -164,8 +164,7 @@ int f511_get_power(int *ch1, int *ch2, int fd) { int res; unsigned char reply[40]; - res = - mcp_cmd((unsigned char *)&f511_read_active_power, + res = mcp_cmd((unsigned char *)&f511_read_active_power, sizeof(f511_read_active_power), (unsigned char *)&reply, fd); if (res > 0) { *ch1 = (reply[3] << 24) + (reply[2] << 16) @@ -186,8 +185,7 @@ int f511_init(const char *port) if (init_serial(port, B115200) < 0) { return -1; } - res = - mcp_cmd((unsigned char *)f511_set_accumulation_interval, + res = mcp_cmd((unsigned char *)f511_set_accumulation_interval, sizeof(f511_set_accumulation_interval), (unsigned char *)&reply, fd); if(res < 0) return res; @@ -200,6 +198,10 @@ int main(int argc, char **argv) { int c; struct timeval now; + int fd; + int result; + int data[2]; // The MCP has two outlets where you can measure. + while ((c = getopt (argc, argv, "hi:d")) != -1) { switch (c) { @@ -219,11 +221,10 @@ int main(int argc, char **argv) { setvbuf(stdout, NULL, _IONBF, 0); - long fd = f511_init("/dev/ttyACM0"); - int data[2]; // The MCP has two outlets where you can measure. + fd = f511_init("/dev/ttyACM0"); while (1) { - int result = f511_get_power(&data[0], &data[1], fd); + result = f511_get_power(&data[0], &data[1], fd); if(result != 0) { printf("Error. Result was not 0\n"); break;