summaryrefslogtreecommitdiff
path: root/tools/solarmeter/solarmeter.c
blob: 64bca8f3cbb3bc6f7ba347e9cc9ab85ae4eefdd0 (plain) (blame)
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
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>

// data via stdin
// begin = 255 followed by 9 byte  8 mal i + 1 mal u
// u = input * 5/256  // u in V
// i = input / 2.099  // i in mA

#define UFAC 5.0f/256.0f*5.6f/1.89f

int main (int argc, char* argv[])
{
    unsigned char tmp = 0;
    unsigned char buffer[9];
    float pow;
    float pow_sum;
    int i;
    int n=0;
    unsigned char *outbuffer;
    int outfile_fd = -1;

    if (argc > 1)
    {
      outfile_fd = open(argv[1], O_WRONLY | O_CREAT | O_APPEND, 0666);
    }

    while (read (STDIN_FILENO, &tmp, 1) == 1)
    {
        if (tmp == 255)
        {
            if (read (STDIN_FILENO, &buffer, 9) != 9)
            {
                printf ("read error\n");
                return 0;
            }
            printf ("%5.2fV", buffer[8] * UFAC);
            pow_sum = 0.0f;
            for (i = 0; i<8; i++)
            {
                pow = (buffer[i] / 2.099f) * (buffer[8] * UFAC);
                printf (" %6.2fmW", pow);
                pow_sum += pow;
                if (outfile_fd > 0)
                {
                  n = asprintf(&outbuffer, "%d %6.2f %6.2f\n", i, buffer[8]*UFAC, buffer[i]/2.099f);
                  write(outfile_fd, outbuffer , n);
                  free(outbuffer);
                }
            }
            if (outfile_fd > 0)
              write(outfile_fd, "\n" , 1);
            printf (" %6.2fmW", pow_sum);
            printf ("\r");
        }
    }
    printf ("\n");
    if (outfile_fd > 0)
      close(outfile_fd);
    return 0;
}