Skip to content

Commit

Permalink
Quality patch
Browse files Browse the repository at this point in the history
  • Loading branch information
ArneTR committed Oct 20, 2023
1 parent 9aacd85 commit 0186160
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions metric_providers/psu/energy/ac/mcp/machine/source.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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;
Expand All @@ -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) {
Expand All @@ -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;
Expand Down

0 comments on commit 0186160

Please sign in to comment.