bool ELM327::initializeELM(const char& protocol, const byte& dataTimeout) Description: ------------ * Initializes ELM327 Inputs: ------- * char protocol - Protocol ID to specify the ELM327 to communicate with the ECU over * byte dataTimeout - Number of ms to wait after receiving data before the ELM327 will return the data - see https://www.elmelectro
| 85 | * --> *user adjustable |
| 86 | */ |
| 87 | bool ELM327::initializeELM(const char& protocol, |
| 88 | const byte& dataTimeout) |
| 89 | { |
| 90 | char command[10] = {'\0'}; |
| 91 | connected = false; |
| 92 | |
| 93 | sendCommand_Blocking(SET_ALL_TO_DEFAULTS); |
| 94 | delay(100); |
| 95 | |
| 96 | sendCommand_Blocking(RESET_ALL); |
| 97 | delay(100); |
| 98 | |
| 99 | sendCommand_Blocking(ECHO_OFF); |
| 100 | delay(100); |
| 101 | |
| 102 | sendCommand_Blocking(PRINTING_SPACES_OFF); |
| 103 | delay(100); |
| 104 | |
| 105 | sendCommand_Blocking(ALLOW_LONG_MESSAGES); |
| 106 | delay(100); |
| 107 | |
| 108 | // // Set data timeout |
| 109 | snprintf(command, sizeof(command), SET_TIMEOUT_TO_H_X_4MS, dataTimeout / 4); |
| 110 | sendCommand_Blocking(command); |
| 111 | delay(100); |
| 112 | |
| 113 | // Automatic searching for protocol requires setting the protocol to AUTO and then |
| 114 | // sending an OBD command to initiate the protocol search. The OBD command "0100" |
| 115 | // requests a list of supported PIDs 0x00 - 0x20 and is guaranteed to work |
| 116 | if (protocol == '0') |
| 117 | { |
| 118 | // Tell the ELM327 to do an auto protocol search. If a valid protocol is found, it will be saved to memory. |
| 119 | // Some ELM clones may not have memory enabled and thus will perform the search every time. |
| 120 | snprintf(command, sizeof(command), SET_PROTOCOL_TO_AUTO_H_SAVE, protocol); |
| 121 | if (sendCommand_Blocking(command) == ELM_SUCCESS) |
| 122 | { |
| 123 | if (strstr(payload, RESPONSE_OK) != NULL) |
| 124 | { |
| 125 | // Protocol search can take a comparatively long time. Temporarily set |
| 126 | // the timeout value to 30 seconds, then restore the previous value. |
| 127 | uint16_t prevTimeout = timeout_ms; |
| 128 | timeout_ms = 30000; |
| 129 | |
| 130 | int8_t state = sendCommand_Blocking("0100"); |
| 131 | |
| 132 | if (state == ELM_SUCCESS) |
| 133 | { |
| 134 | timeout_ms = prevTimeout; |
| 135 | connected = true; |
| 136 | return connected; |
| 137 | } |
| 138 | else if (state == ELM_BUFFER_OVERFLOW) |
| 139 | { |
| 140 | while (elm_port->available()) |
| 141 | elm_port->read(); |
| 142 | } |
| 143 | |
| 144 | timeout_ms = prevTimeout; |
nothing calls this directly
no outgoing calls
no test coverage detected