MCPcopy Create free account
hub / github.com/PowerBroker2/ELMduino / parseMultiLineResponse

Method parseMultiLineResponse

src/ELMduino.cpp:2609–2694  ·  view source on GitHub ↗

void ELM327::parseMultilineResponse() Description: ------------ * Parses a buffered multiline response into a single line with the specified data * Modifies the value of payload for further processing and removes the '\r' chars Inputs: ------- * void Return: ------- * void */

Source from the content-addressed store, hash-verified

2607 * void
2608*/
2609void ELM327::parseMultiLineResponse() {
2610 uint8_t totalBytes = 0;
2611 uint8_t bytesReceived = 0;
2612 char newResponse[PAYLOAD_LEN];
2613 memset(newResponse, 0, PAYLOAD_LEN * sizeof(char)); // Initialize newResponse to empty string
2614 char line[256] = "";
2615 char* start = payload;
2616 char* end = strchr(start, '\r');
2617
2618 do
2619 { //Step 1: Get a line from the response
2620 memset(line, '\0', 256);
2621 if (end != NULL) {
2622 strncpy(line, start, end - start);
2623 line[end - start] = '\0';
2624 } else {
2625 strncpy(line, start, strlen(start));
2626 line[strlen(start)] = '\0';
2627
2628 // Exit when there's no more data
2629 if (strlen(line) == 0) break;
2630 }
2631
2632 if (debugMode) {
2633 Serial.print(F("Found line in response: "));
2634 Serial.println(line);
2635 }
2636 // Step 2: Check if this is the first line of the response
2637 if (0 == totalBytes)
2638 // Some devices return the response header in the first line instead of the data length, ignore this line
2639 // Line containing totalBytes indicator is 3 hex chars only, longer first line will be a header.
2640 {
2641 if (strlen(line) > 3) {
2642 if (debugMode)
2643 {
2644 Serial.print(F("Found header in response line: "));
2645 Serial.println(line);
2646 }
2647 }
2648 else {
2649 if (strlen(line) > 0) {
2650 totalBytes = strtol(line, NULL, 16) * 2;
2651 if (debugMode) {
2652 Serial.print(F("totalBytes = "));
2653 Serial.println(totalBytes);
2654 }
2655 }
2656 }
2657 }
2658 // Step 3: Process data response lines
2659 else {
2660 if (strchr(line, ':')) {
2661 char* dataStart = strchr(line, ':') + 1;
2662 uint8_t dataLength = strlen(dataStart);
2663 uint8_t bytesToCopy = (bytesReceived + dataLength > totalBytes) ? (totalBytes - bytesReceived) : dataLength;
2664 if (bytesReceived + bytesToCopy > PAYLOAD_LEN - 1) {
2665 bytesToCopy = (PAYLOAD_LEN - 1) - bytesReceived;
2666 }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected