Skip to content

Commit

Permalink
fixing query function
Browse files Browse the repository at this point in the history
  • Loading branch information
chadek committed Nov 15, 2023
1 parent b981c7b commit b1336e2
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 39 deletions.
70 changes: 38 additions & 32 deletions inverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ cInverter::cInverter(std::string devicename) {
status1[0] = 0;
status2[0] = 0;
warnings[0] = 0;
mode = 0;
mode = 0;
}

string *cInverter::GetQpigsStatus() {
Expand Down Expand Up @@ -63,10 +63,10 @@ int cInverter::GetMode() {
return result;
}

bool cInverter::query(const char *cmd, int max_replysize) {
bool cInverter::query(const char *cmd) {
time_t started;
int fd;
int i=0, n;
int i=0, n, replysize;

fd = open(this->device.data(), O_RDWR | O_NONBLOCK);
if (fd == -1) {
Expand Down Expand Up @@ -110,48 +110,55 @@ bool cInverter::query(const char *cmd, int max_replysize) {
write(fd, &buf, n);
time(&started);

const int READ_BUFFER_SIZE = 60;
bool exit_loop = false;
const int READ_BUFFER_SIZE = 15;
bool reading = true;
bool timeout = false;
do {
n = read(fd, (void*)buf+i, READ_BUFFER_SIZE);
for (int j=i+n; j>i; j--) {
if (buf[j] == 0x0d){
exit_loop = true;
}
}
if (n < 0) {
if (time(NULL) - started > 2) {
timeout = true;
lprintf("INVERTER: %s read timeout", cmd);
break;
} else {
usleep(5);
usleep(2000);
continue;
}
} else {
for (int j=i; j<i+n; j++) {
if (buf[j] == 0x0d){
reading = false;
replysize = j+1;
printf("INVERTER: stop byte detected, buffersize might be %d for %s ", replysize, cmd);
break;
}
}

}
usleep(2000);

i += n;
} while (i<max_replysize && !exit_loop);
} while (reading);
close(fd);
i++;

if (buf[0]!='(') {
lprintf("INVERTER: %s: incorrect start bytes. Buffer: %s", cmd, buf);
if (timeout) {
lprintf("INVERTER: %s command timeout, or couldn't find stop byte. Byte read (%d bytes)", cmd, i);
return false;
}

if (i==max_replysize && !exit_loop){
lprintf("INVERTER: stop byte not found in buffer Buffer: %s", buf);
return false;
}
lprintf("INVERTER: %s reply size (%d bytes)", cmd, i);

lprintf("INVERTER: %s reply size (%d bytes)", cmd, replysize);

if (!(CheckCRC(buf, i))) {
lprintf("INVERTER: %s: CRC Failed! Reply size: %d Buffer: %s", cmd, i, buf);
if (buf[0]!='(') {
lprintf("INVERTER: %s: incorrect start bytes. Buffer: %s ", cmd, buf);
return false;
}
if (!(CheckCRC(buf, replysize))) {
lprintf("INVERTER: %s: CRC Failed! Reply size: %d Buffer: %s ", cmd, replysize, buf);
return false;
}

buf[i-3] = '\0'; //nullterminating on first CRC byte
lprintf("INVERTER: %s: %d bytes read: %s", cmd, i, buf);
buf[replysize-3] = '\0'; //nullterminating on first CRC byte
lprintf("INVERTER: %s: %d bytes read: %s ", cmd, replysize, buf);

lprintf("INVERTER: %s query finished", cmd);
return true;
Expand All @@ -160,21 +167,20 @@ bool cInverter::query(const char *cmd, int max_replysize) {
void cInverter::poll() {
int n,j;
extern const bool runOnce;
extern const int max_replylen;

while (true) {

// Reading mode
if (!ups_qmod_changed) {
if (query("QMOD", max_replylen)) {
if (query("QMOD")) {
SetMode(buf[1]);
ups_qmod_changed = true;
}
}

// reading status (QPIGS)
if (!ups_qpigs_changed) {
if (query("QPIGS", max_replylen)) {
if (query("QPIGS")) {
m.lock();
strcpy(status1, (const char*)buf+1);
m.unlock();
Expand All @@ -184,7 +190,7 @@ void cInverter::poll() {

// Reading QPIRI status
if (!ups_qpiri_changed) {
if (query("QPIRI", max_replylen)) {
if (query("QPIRI")) {
m.lock();
strcpy(status2, (const char*)buf+1);
m.unlock();
Expand All @@ -194,7 +200,7 @@ void cInverter::poll() {

// Get any device warnings...
if (!ups_qpiws_changed) {
if (query("QPIWS", max_replylen)) {
if (query("QPIWS")) {
m.lock();
strcpy(warnings, (const char*)buf+1);
m.unlock();
Expand All @@ -211,9 +217,9 @@ void cInverter::poll() {
}
}

void cInverter::ExecuteCmd(const string cmd, int max_replylen) {
void cInverter::ExecuteCmd(const string cmd) {
// Sending any command raw
if (query(cmd.data(), max_replylen)) {
if (query(cmd.data())) {
m.lock();
strcpy(status2, (const char*)buf+1);
m.unlock();
Expand Down
4 changes: 2 additions & 2 deletions inverter.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class cInverter {

void SetMode(char newmode);
bool CheckCRC(unsigned char *buff, int len);
bool query(const char *cmd, int replysize);
bool query(const char *cmd);
uint16_t cal_crc_half(uint8_t *pin, uint8_t len);

public:
Expand All @@ -41,7 +41,7 @@ class cInverter {
string *GetWarnings();

int GetMode();
void ExecuteCmd(const std::string cmd, int);
void ExecuteCmd(const std::string cmd);
};

#endif // ___INVERTER_H
21 changes: 16 additions & 5 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ string devicename;
int runinterval;
float ampfactor;
float wattfactor;
int max_replylen = 255;
int qpiri = 98;
int qpiws = 36;
int qmod = 5;
int qpigs = 110;

// ---------------------------------------

Expand Down Expand Up @@ -90,6 +93,14 @@ void getSettingsFile(string filename) {
attemptAddSetting(&ampfactor, linepart2);
else if(linepart1 == "watt_factor")
attemptAddSetting(&wattfactor, linepart2);
else if(linepart1 == "qpiri")
attemptAddSetting(&qpiri, linepart2);
else if(linepart1 == "qpiws")
attemptAddSetting(&qpiws, linepart2);
else if(linepart1 == "qmod")
attemptAddSetting(&qmod, linepart2);
else if(linepart1 == "qpigs")
attemptAddSetting(&qpigs, linepart2);
else
continue;
}
Expand Down Expand Up @@ -151,7 +162,8 @@ int main(int argc, char* argv[]) {
// Get command flag settings from the arguments (if any)
InputParser cmdArgs(argc, argv);
const string &rawcmd = cmdArgs.getCmdOption("-r");
sscanf(cmdArgs.getCmdOption("-l").c_str(), "%d", &max_replylen);
int replylen = 7;
sscanf(cmdArgs.getCmdOption("-l").c_str(), "%d", &replylen);

if(cmdArgs.cmdOptionExists("-h") || cmdArgs.cmdOptionExists("--help")) {
return print_help();
Expand Down Expand Up @@ -180,7 +192,7 @@ int main(int argc, char* argv[]) {

// Logic to send 'raw commands' to the inverter..
if (!rawcmd.empty()) {
ups->ExecuteCmd(rawcmd, max_replylen);
ups->ExecuteCmd(rawcmd);
// We're piggybacking off the qpri status response...
printf("Reply: %s\n", ups->GetQpiriStatus()->c_str());
exit(0);
Expand Down Expand Up @@ -213,7 +225,7 @@ int main(int argc, char* argv[]) {

// Parse and display values
sscanf(reply1->c_str(), "%f %f %f %f %d %d %d %d %f %d %d %d %f %f %f %d %s", &voltage_grid, &freq_grid, &voltage_out, &freq_out, &load_va, &load_watt, &load_percent, &voltage_bus, &voltage_batt, &batt_charge_current, &batt_capacity, &temp_heatsink, &pv_input_current, &pv_input_voltage, &scc_voltage, &batt_discharge_current, &device_status);
sscanf(reply2->c_str(), "%f %f %f %f %f %d %d %f %f %f %f %f %d %d %d %d %d %d - %d %d %d %f", &grid_voltage_rating, &grid_current_rating, &out_voltage_rating, &out_freq_rating, &out_current_rating, &out_va_rating, &out_watt_rating, &batt_rating, &batt_recharge_voltage, &batt_under_voltage, &batt_bulk_voltage, &batt_float_voltage, &batt_type, &max_grid_charge_current, &max_charge_current, &in_voltage_range, &out_source_priority, &charger_source_priority, &machine_type, &topology, &out_mode, &batt_redischarge_voltage);
sscanf(reply2->c_str(), "%f %f %f %f %f %d %d %f %f %f %f %f %d %d %d %d %d %d %d %d %d %f", &grid_voltage_rating, &grid_current_rating, &out_voltage_rating, &out_freq_rating, &out_current_rating, &out_va_rating, &out_watt_rating, &batt_rating, &batt_recharge_voltage, &batt_under_voltage, &batt_bulk_voltage, &batt_float_voltage, &batt_type, &max_grid_charge_current, &max_charge_current, &in_voltage_range, &out_source_priority, &charger_source_priority, &machine_type, &topology, &out_mode, &batt_redischarge_voltage);

// There appears to be a discrepancy in actual DMM measured current vs what the meter is
// telling me it's getting, so lets add a variable we can multiply/divide by to adjust if
Expand Down Expand Up @@ -277,7 +289,6 @@ int main(int argc, char* argv[]) {
delete reply1;
delete reply2;
}

} else if (ups_leave) {
ups->terminateThread();
// Do once and exit instead of loop endlessly
Expand Down

0 comments on commit b1336e2

Please sign in to comment.