19-04-2022, 09:47
Witam.
Jestem początkujący w arduino i jest to moje pierwsze pytanie, bo niestety nie mogę sobie poradzić i utknąłem w martwym punkcie.
Chcę odczytać zmienne, które są wysyłane z instalacji solarnej, aby móc je później przeliczyć i przesłać do supli. Problemem jest obróbka zmiennych.
Instalacja solarna wysyła co 30s 9 wartości zmiennych oddzielonych od siebie znakiem ';' . zmienne różnego typu (book, integer, float)
Napisałem prosty program, który odczytuje to, co wysyła sterownik solarów i wyświetlam to na LCD 2x16, ale nie wiem jak przepisać te wartości do konkretnych zmiennych. Znalazłem podobny przykład, ale w przypadku, gdy dane zawierają znak początkowy i końcowy transmisji, a u mnie nie ma znaku początkowego, jest jedynie znak końcowy, za każdym razem inny i nie do końca zrozumiały (zdjęcie w załączniku).
Format danych z ROTEX:
HA; BK; P1; P2; Tk; Tr; Ts,Tv; V
HA: ręczny/automat (1/0)
BK: Status styku blokującego palnik: wyłączony = 0; załączony = 1
P1: prędkość obrotowa pompy P1 w % (63%, 75%, 83% , 100% itd)
P2: pompa P2 włączona/wyłączona, 0=wyłączona, 1=włączona
Tk: Temperatura kolektorów [°C] - integer
Tr: Temperatura powrotu [°C] -integer
Ts: Temperatura zbiornika [°C] - integer
Tv: Temperatura zasilania [°C] - integer
V: natężenie przepływu (l / min) - float
Poniższy program byłby idealny, gdyby nie to, że nie mam początkowego znaku, a końcowy jest za każdym razem inny
const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars]; // temporary array for use when parsing variables to hold the parsed data
char messageFromPC[numChars] = {0};
boolean HA = 0;
boolean BK = 0;
int P1 = 0;
boolean P2 = 0;
int Tk = 0;
int Tr = 0;
int Ts = 0;
int Tv = 0;
float V = 0.0;
boolean newData = false;
//============
void setup() {
Serial.begin(9600);
}
//============
void loop() {
recvWithStartEndMarkers();
if (newData == true) {
strcpy(tempChars, receivedChars);
// this temporary copy is necessary to protect the original data
// because strtok() used in parseData() replaces the commas with \0
parseData();
showParsedData();
newData = false;
}
}
//============
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (Serial.available() > 0 && newData == false){
rc = Serial.read ();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
//============
void parseData() { // split the data into its parts
char * strtokIndx; // this is used by strtok() as an index
strtokIndx = strtok(tempChars, ";"); // this continues where the previous call left off
HA = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ";");
BK = atof(strtokIndx); // convert this part to a integer
strtokIndx = strtok(NULL, ";");
P1 = atoi(strtokIndx); // convert this part to a integer
strtokIndx = strtok(NULL, ";");
P2 = atoi(strtokIndx); // convert this part to a integer
strtokIndx = strtok(NULL, ";");
Tk = atoi(strtokIndx); // convert this part to a integer
strtokIndx = strtok(NULL, ";");
Tr = atoi(strtokIndx); // convert this part to a integer
strtokIndx = strtok(NULL, ";");
Ts = atoi(strtokIndx); // convert this part to a integer
strtokIndx = strtok(NULL, ";");
Tv = atoi(strtokIndx); // convert this part to a integer
strtokIndx = strtok(NULL, ";");
V = atof(strtokIndx); // convert this part to a float
}
//============
void showParsedData() {
Serial.println("Dane ROTEX: ");
Serial.print("HA: ");
Serial.println(HA);
Serial.print("BK: ");
Serial.println(BK);
Serial.print("P1: ");
Serial.println(P1);
Serial.print("P2: ");
Serial.println(P2);
Serial.print("Tk: ");
Serial.println(Tk);
Serial.print("Tr: ");
Serial.println(Tr);
Serial.print("Ts: ");
Serial.println(Ts);
Serial.print("Tv: ");
Serial.println(Tv);
Serial.print("V: ");
Serial.println(V);
}
pomóżcie proszę, bo sam sobie nie poradzę
Jestem początkujący w arduino i jest to moje pierwsze pytanie, bo niestety nie mogę sobie poradzić i utknąłem w martwym punkcie.
Chcę odczytać zmienne, które są wysyłane z instalacji solarnej, aby móc je później przeliczyć i przesłać do supli. Problemem jest obróbka zmiennych.
Instalacja solarna wysyła co 30s 9 wartości zmiennych oddzielonych od siebie znakiem ';' . zmienne różnego typu (book, integer, float)
Napisałem prosty program, który odczytuje to, co wysyła sterownik solarów i wyświetlam to na LCD 2x16, ale nie wiem jak przepisać te wartości do konkretnych zmiennych. Znalazłem podobny przykład, ale w przypadku, gdy dane zawierają znak początkowy i końcowy transmisji, a u mnie nie ma znaku początkowego, jest jedynie znak końcowy, za każdym razem inny i nie do końca zrozumiały (zdjęcie w załączniku).
Format danych z ROTEX:
HA; BK; P1; P2; Tk; Tr; Ts,Tv; V
HA: ręczny/automat (1/0)
BK: Status styku blokującego palnik: wyłączony = 0; załączony = 1
P1: prędkość obrotowa pompy P1 w % (63%, 75%, 83% , 100% itd)
P2: pompa P2 włączona/wyłączona, 0=wyłączona, 1=włączona
Tk: Temperatura kolektorów [°C] - integer
Tr: Temperatura powrotu [°C] -integer
Ts: Temperatura zbiornika [°C] - integer
Tv: Temperatura zasilania [°C] - integer
V: natężenie przepływu (l / min) - float
Poniższy program byłby idealny, gdyby nie to, że nie mam początkowego znaku, a końcowy jest za każdym razem inny
const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars]; // temporary array for use when parsing variables to hold the parsed data
char messageFromPC[numChars] = {0};
boolean HA = 0;
boolean BK = 0;
int P1 = 0;
boolean P2 = 0;
int Tk = 0;
int Tr = 0;
int Ts = 0;
int Tv = 0;
float V = 0.0;
boolean newData = false;
//============
void setup() {
Serial.begin(9600);
}
//============
void loop() {
recvWithStartEndMarkers();
if (newData == true) {
strcpy(tempChars, receivedChars);
// this temporary copy is necessary to protect the original data
// because strtok() used in parseData() replaces the commas with \0
parseData();
showParsedData();
newData = false;
}
}
//============
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (Serial.available() > 0 && newData == false){
rc = Serial.read ();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
//============
void parseData() { // split the data into its parts
char * strtokIndx; // this is used by strtok() as an index
strtokIndx = strtok(tempChars, ";"); // this continues where the previous call left off
HA = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ";");
BK = atof(strtokIndx); // convert this part to a integer
strtokIndx = strtok(NULL, ";");
P1 = atoi(strtokIndx); // convert this part to a integer
strtokIndx = strtok(NULL, ";");
P2 = atoi(strtokIndx); // convert this part to a integer
strtokIndx = strtok(NULL, ";");
Tk = atoi(strtokIndx); // convert this part to a integer
strtokIndx = strtok(NULL, ";");
Tr = atoi(strtokIndx); // convert this part to a integer
strtokIndx = strtok(NULL, ";");
Ts = atoi(strtokIndx); // convert this part to a integer
strtokIndx = strtok(NULL, ";");
Tv = atoi(strtokIndx); // convert this part to a integer
strtokIndx = strtok(NULL, ";");
V = atof(strtokIndx); // convert this part to a float
}
//============
void showParsedData() {
Serial.println("Dane ROTEX: ");
Serial.print("HA: ");
Serial.println(HA);
Serial.print("BK: ");
Serial.println(BK);
Serial.print("P1: ");
Serial.println(P1);
Serial.print("P2: ");
Serial.println(P2);
Serial.print("Tk: ");
Serial.println(Tk);
Serial.print("Tr: ");
Serial.println(Tr);
Serial.print("Ts: ");
Serial.println(Ts);
Serial.print("Tv: ");
Serial.println(Tv);
Serial.print("V: ");
Serial.println(V);
}
pomóżcie proszę, bo sam sobie nie poradzę