-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmppt.c
More file actions
150 lines (112 loc) · 3.48 KB
/
mppt.c
File metadata and controls
150 lines (112 loc) · 3.48 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include "ui.h"
#include "ui_helpers.h"
#include "global.h"
int fd;
char buf[2048];
char str[2048];
int set_interface_attribs(int fd, int speed)
{
struct termios tty;
if (tcgetattr(fd, &tty) < 0) {
printf(str,"shunt: error from tcgetattr: %s\n", strerror(errno));
return -1;
}
cfsetospeed(&tty, (speed_t)speed);
cfsetispeed(&tty, (speed_t)speed);
tty.c_cflag |= CLOCAL | CREAD;
tty.c_cflag &= ~CSIZE;
tty.c_cflag |= CS8; /* 8-bit characters */
tty.c_cflag &= ~PARENB; /* no parity bit */
tty.c_cflag &= ~CSTOPB; /* only need 1 stop bit */
tty.c_cflag &= ~CRTSCTS; /* no hardware flowcontrol */
tty.c_lflag |= ICANON | ISIG; /* canonical input */
tty.c_lflag &= ~(ECHO | ECHOE | ECHONL | IEXTEN);
tty.c_iflag &= ~IGNCR; /* preserve carriage return */
tty.c_iflag &= ~INPCK;
tty.c_iflag &= ~(INLCR | ICRNL | IUCLC | IMAXBEL);
tty.c_iflag &= ~(IXON | IXOFF | IXANY); /* no SW flowcontrol */
tty.c_oflag &= ~OPOST;
tty.c_cc[VEOL] = 0;
tty.c_cc[VEOL2] = 0;
tty.c_cc[VEOF] = 0x04;
if (tcsetattr(fd, TCSANOW, &tty) != 0) {
console_send("shunt: error from tcsetattr: %s\n", strerror(errno));
return -1;
}
return 0;
}
int mppt_init(void)
{
char *portname = MPTTPORT;
fd = open(portname, O_RDWR | O_NOCTTY | O_SYNC);
if (fd < 0) {
printf("shunt: error opening %s: %s\n", portname, strerror(errno));
return -1;
}
set_interface_attribs(fd, B19200);
return 0;
}
// Funktion, um den Wert für ein bestimmtes Kürzel zu extrahieren
double extract_value(const char *buf, const char *key) {
const char *pos = buf;
while ((pos = strstr(pos, key)) != NULL) {
// Nach dem Kürzel suchen
const char *start = strchr(pos, ',');
if (start == NULL) {
break;
}
start++; // Nach dem Komma
// Den Wert bis zum nächsten Doppelkomma lesen
const char *end = strstr(start, ",,");
if (end == NULL) {
break;
}
// Länge des Wertes
size_t len = end - start;
char value_str[50];
if (len >= sizeof(value_str))
len = sizeof(value_str) - 1;
strncpy(value_str, start, len);
value_str[len] = '\0';
// Wert in double umwandeln
return atof(value_str);
}
// Falls nicht gefunden, 0 zurückgeben
return 0.0;
}
int read_mppt(void)
{
int rdlen;
char *p;
rdlen = read(fd, buf, sizeof(buf) - 1);
if (rdlen > 0) {
buf[rdlen] = 0;
for (p = buf; rdlen-- > 0; p++) {
if (*p < ' ')
*p = ','; /* replace any control chars */
}
//printf("%s\n", buf);
double P = extract_value(buf, "P");
if (P != 0)
{
lv_label_set_text_fmt(ui_Watt,"%.f W",P);
}
double I = extract_value(buf, "I");
if (I != 0)
{
lv_label_set_text_fmt(ui_Ampere,"%.2f A",I/1000);
}
} else if (rdlen < 0) {
printf("shunt: error from read: %d: %s\n", rdlen, strerror(errno));
} else { /* rdlen == 0 */
printf("shunt: nothing read from Serial. EOF?\n");
}
return 0;
}