Update modb_orno3.c

W trybie zwiększonej fluktuacji przyspieszamy wyłącznie odczyt napięc.
Poprawka wygenerowana przez qwen3_coder
This commit is contained in:
ms
2026-02-17 13:12:52 +01:00
parent 3078bbcf3f
commit fd8dce7535

View File

@@ -71,13 +71,13 @@ typedef struct { float Freq; } s_frequency;
/*
'Model' : 'ID'
'addr' : '30000',
'registers' : 15,
'name' : 'Model',
'scale' : 1,
'type' : 'str',
'units' : '' ,
'use' : 'info',
'addr' : '30000',
'registers' : 15,
'name' : 'Model',
'scale' : 1,
'type' : 'str',
'units' : '' ,
'use' : 'info',
'method' : 'hold'
*/
enum regtype
@@ -297,7 +297,7 @@ int influx_send_post(char *data)
char header[1024];
/* Construct HTTP POST request for InfluxDB v2 Write API */
snprintf(header, sizeof(header),
snprintf(header, sizeof(header),
"POST /api/v2/write?org=%s&bucket=%s&precision=s HTTP/1.1\r\n"
"Host: %s:%d\r\n"
"Authorization: Token %s\r\n"
@@ -536,6 +536,24 @@ int mosq_test()
}
}
/* Function to read only voltage from ORNO device */
int read_orno_voltage_only(modbus_t *ctx, s_voltage *voltage) {
uint16_t reg[6];
int num;
// READ ONLY VOLTAGES
num = modbus_read_timed(ctx, 0xe, 6, reg);
if (num == 6) {
voltage->U1 = modbus_get_float_abcd(&reg[0]);
voltage->U2 = modbus_get_float_abcd(&reg[2]);
voltage->U3 = modbus_get_float_abcd(&reg[4]);
return 0; // success
} else {
printf("ORNO: Failed to read voltages: %s\n", modbus_strerror(errno));
return -1; // error
}
}
int main(int argc, char *argv[])
{
/* Parse command line arguments */
@@ -596,9 +614,71 @@ int main(int argc, char *argv[])
do
{
int fast_voltage_mode = (time(NULL) < high_frequency_mode_end_time);
if (do_orno)
{
//Create a new RTU context
if (fast_voltage_mode) {
// FAST MODE - ONLY READ VOLTAGE
printf("ORNO: Fast voltage reading mode active\n");
//Create a new RTU context
modbus_t *ctx = modbus_new_rtu(USB_DEV, 9600, 'E', 8, 1);
if (!ctx)
{
fprintf(stderr, "ORNO: Failed to create the context: %s\n", modbus_strerror(errno));
exit(1);
}
//Set the Modbus address of the remote slave
modbus_set_slave(ctx, ORNO_SLAVE);
if (modbus_connect(ctx) == -1)
{
fprintf(stderr, "ORNO: Unable to connect: %s\n", modbus_strerror(errno));
modbus_free(ctx);
exit(1);
}
modbus_rtu_set_rts_delay(ctx, ORNO_RTS_DELAY);
modbus_set_response_timeout(ctx, 0, 900000); /* 0.9s */
modbus_set_byte_timeout(ctx, 0, ORNO_BYTE_TIMEOUT);
printf("ORNO: RTS Delay and Timeouts configured.\n");
// Read only voltage in fast mode
s_voltage fast_voltage;
if (read_orno_voltage_only(ctx, &fast_voltage) == 0) {
printf("ORNO: Fast mode voltages: L1=%.1f V, L2=%.1f V, L3=%.1f V\n",
fast_voltage.U1, fast_voltage.U2, fast_voltage.U3);
// Send voltage data immediately
if (is_valid_float(fast_voltage.U1, 150.0, 280.0) &&
is_valid_float(fast_voltage.U2, 150.0, 280.0) &&
is_valid_float(fast_voltage.U3, 150.0, 280.0)) {
mqtt_send_U(fast_voltage.U1, fast_voltage.U2, fast_voltage.U3);
printf("ORNO: MQTT: Published fast voltage readings\n");
// Update voltage buffer for fluctuation detection
voltage_buffer_L1[voltage_buffer_index] = fast_voltage.U1;
voltage_buffer_L2[voltage_buffer_index] = fast_voltage.U2;
voltage_buffer_L3[voltage_buffer_index] = fast_voltage.U3;
voltage_buffer_index = (voltage_buffer_index + 1) % VOLTAGE_BUFFER_SIZE;
if (voltage_buffer_items < VOLTAGE_BUFFER_SIZE) {
voltage_buffer_items++;
}
}
}
modbus_close(ctx);
modbus_free(ctx);
// Small delay before next fast read
usleep(10000); // 10ms
continue; // Skip other readings in fast mode
}
// NORMAL MODE - FULL READING
//Create a new RTU context
modbus_t *ctx = modbus_new_rtu(USB_DEV, 9600, 'E', 8, 1);
if (!ctx)
{
@@ -625,7 +705,7 @@ int main(int argc, char *argv[])
int num;
// ---- READ ALL ORNO VALUES ----
// VOLTAGES
num = modbus_read_timed(ctx, 0xe, 6, reg);
if (num == 6) {
@@ -649,7 +729,7 @@ int main(int argc, char *argv[])
current_current = prev_current;
}
usleep(100000);
// POWER
num = modbus_read_timed(ctx, 0x1c, 8, reg);
if (num == 8) {
@@ -662,7 +742,7 @@ int main(int argc, char *argv[])
current_power = prev_power;
}
usleep(100000);
// ENERGY
num = modbus_read_timed(ctx, 0x100, 8, reg);
if (num == 8) {
@@ -675,7 +755,7 @@ int main(int argc, char *argv[])
current_energy = prev_energy;
}
usleep(10000);
// FREQUENCY
num = modbus_read_timed(ctx, 0x14, 2, reg);
if (num == 2) {
@@ -687,7 +767,7 @@ int main(int argc, char *argv[])
modbus_close(ctx);
modbus_free(ctx);
// --- VOLTAGE FLUCTUATION DETECTION (uses fresh, unfiltered values) ---
voltage_buffer_L1[voltage_buffer_index] = current_voltage.U1;
voltage_buffer_L2[voltage_buffer_index] = current_voltage.U2;
@@ -764,7 +844,7 @@ int main(int argc, char *argv[])
}
// Always update prev_current to prevent getting stuck on old values
prev_current = current_current;
// POWER check
int spike_P = 0;
if (prev_power.P_Tot != 0 && (fabs(current_power.P_Tot - prev_power.P_Tot) > fabs(prev_power.P_Tot) * 0.8)) spike_P = 1;
@@ -803,7 +883,7 @@ int main(int argc, char *argv[])
}
// --- Now send the selected values ---
int valid_U = 0, valid_I = 0, valid_P = 0, valid_W = 0, valid_F = 0;
printf("ORNO: Voltages: L1=%.1f V, L2=%.1f V, L3=%.1f V\n", send_voltage.U1, send_voltage.U2, send_voltage.U3);
@@ -858,6 +938,8 @@ int main(int argc, char *argv[])
&send_freq, valid_F);
}
}
// Always execute SUN2K reading regardless of fast voltage mode
if (do_sun2k)
{
/* Delay between ORNO and SUN2K as configured */
@@ -1085,6 +1167,7 @@ int main(int argc, char *argv[])
modbus_close(ctx);
modbus_free(ctx);
}
if (READ_LOOP) {
if (time(NULL) < high_frequency_mode_end_time) {
usleep(10000); // 10ms