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

@@ -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[]) int main(int argc, char *argv[])
{ {
/* Parse command line arguments */ /* Parse command line arguments */
@@ -596,8 +614,70 @@ int main(int argc, char *argv[])
do do
{ {
int fast_voltage_mode = (time(NULL) < high_frequency_mode_end_time);
if (do_orno) if (do_orno)
{ {
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 //Create a new RTU context
modbus_t *ctx = modbus_new_rtu(USB_DEV, 9600, 'E', 8, 1); modbus_t *ctx = modbus_new_rtu(USB_DEV, 9600, 'E', 8, 1);
if (!ctx) if (!ctx)
@@ -858,6 +938,8 @@ int main(int argc, char *argv[])
&send_freq, valid_F); &send_freq, valid_F);
} }
} }
// Always execute SUN2K reading regardless of fast voltage mode
if (do_sun2k) if (do_sun2k)
{ {
/* Delay between ORNO and SUN2K as configured */ /* Delay between ORNO and SUN2K as configured */
@@ -1085,6 +1167,7 @@ int main(int argc, char *argv[])
modbus_close(ctx); modbus_close(ctx);
modbus_free(ctx); modbus_free(ctx);
} }
if (READ_LOOP) { if (READ_LOOP) {
if (time(NULL) < high_frequency_mode_end_time) { if (time(NULL) < high_frequency_mode_end_time) {
usleep(10000); // 10ms usleep(10000); // 10ms