diff --git a/Software/Software.ino b/Software/Software.ino index 1994978f..79e99bf8 100644 --- a/Software/Software.ino +++ b/Software/Software.ino @@ -642,44 +642,38 @@ void print_can_frame(CAN_frame frame, frameDirection msgDir) { #endif //#DEBUG_CAN_DATA if (datalayer.system.info.can_logging_active) { // If user clicked on CAN Logging page in webserver, start recording + char* message_string = datalayer.system.info.logged_can_messages; + int offset = datalayer.system.info.logged_can_messages_offset; // Keeps track of the current position in the buffer + size_t message_string_size = sizeof(datalayer.system.info.logged_can_messages); - char message_string[128]; // Buffer to hold the message string - int offset = 0; // Keeps track of the current position in the buffer - + if (offset + 128 > sizeof(datalayer.system.info.logged_can_messages)) { + // Not enough space, reset and start from the beginning + offset = 0; + } + unsigned long currentTime = millis(); // Add timestamp - offset += snprintf(message_string + offset, sizeof(message_string) - offset, "%lu ", millis()); + offset += snprintf(message_string + offset, message_string_size - offset, "(%lu.%03lu) ", currentTime / 1000, + currentTime % 1000); - // Add direction + // Add direction. The 0 and 1 after RX and TX ensures that SavvyCAN puts TX and RX in a different bus. offset += - snprintf(message_string + offset, sizeof(message_string) - offset, "%s ", (msgDir == MSG_RX) ? "RX" : "TX"); + snprintf(message_string + offset, message_string_size - offset, "%s ", (msgDir == MSG_RX) ? "RX0" : "TX1"); // Add ID and DLC - offset += snprintf(message_string + offset, sizeof(message_string) - offset, "%X %u ", frame.ID, frame.DLC); + offset += snprintf(message_string + offset, message_string_size - offset, "%X [%u] ", frame.ID, frame.DLC); // Add data bytes for (uint8_t i = 0; i < frame.DLC; i++) { - offset += snprintf(message_string + offset, sizeof(message_string) - offset, "%s%X ", - frame.data.u8[i] < 16 ? "0" : "", frame.data.u8[i]); + if (i < frame.DLC - 1) { + offset += snprintf(message_string + offset, message_string_size - offset, "%02X ", frame.data.u8[i]); + } else { + offset += snprintf(message_string + offset, message_string_size - offset, "%02X", frame.data.u8[i]); + } } // Add linebreak - offset += snprintf(message_string + offset, sizeof(message_string) - offset, "\n"); - - // Ensure the string is null-terminated - message_string[sizeof(message_string) - 1] = '\0'; - - // Append the message string to the system info structure - size_t current_len = - strnlen(datalayer.system.info.logged_can_messages, sizeof(datalayer.system.info.logged_can_messages)); - size_t available_space = - sizeof(datalayer.system.info.logged_can_messages) - current_len - 1; // Space left for new data - - if (available_space < strlen(message_string) + 1) { - // Not enough space, reset and start from the beginning - current_len = 0; - datalayer.system.info.logged_can_messages[0] = '\0'; - } + offset += snprintf(message_string + offset, message_string_size - offset, "\n"); - strncat(datalayer.system.info.logged_can_messages, message_string, available_space); + datalayer.system.info.logged_can_messages_offset = offset; // Update offset in buffer } } diff --git a/Software/src/datalayer/datalayer.h b/Software/src/datalayer/datalayer.h index 81814336..e6de68ce 100644 --- a/Software/src/datalayer/datalayer.h +++ b/Software/src/datalayer/datalayer.h @@ -133,6 +133,7 @@ typedef struct { char inverter_protocol[64] = {0}; /** array with incoming CAN messages, for displaying on webserver */ char logged_can_messages[15000] = {0}; + size_t logged_can_messages_offset = 0; /** bool, determines if CAN messages should be logged for webserver */ bool can_logging_active = false;