From ea6261f09e1291ac2af0fd8ef32aadad92a03d07 Mon Sep 17 00:00:00 2001 From: Lubos Petrovic Date: Sun, 20 Dec 2020 17:12:38 +0100 Subject: [PATCH] can --- CommInterface.cpp | 72 +++++++++++++++++++++++++++++++++++++++++++++++ CommInterface.h | 6 +++- CommObd2Ble4.cpp | 69 +++++---------------------------------------- CommObd2Ble4.h | 3 +- CommObd2Can.cpp | 24 +++++++++++++++- CommObd2Can.h | 1 + 6 files changed, 109 insertions(+), 66 deletions(-) diff --git a/CommInterface.cpp b/CommInterface.cpp index 58fa23a..0da5264 100644 --- a/CommInterface.cpp +++ b/CommInterface.cpp @@ -3,9 +3,81 @@ //#include "CarInterface.h" #include "LiveData.h" +/** + * + */ void CommInterface::initComm(LiveData* pLiveData, BoardInterface* pBoard) { liveData = pLiveData; board = pBoard; response = ""; } + +/** + * Main loop + */ +void CommInterface::mainLoop() { + + // Send command from TTY to OBD2 + if (Serial.available()) { + ch = Serial.read(); + if (ch == '\r' || ch == '\n') { + board->customConsoleCommand(response); + response = response + ch; + Serial.println(response); + if (liveData->bleConnected) { + executeCommand(response); + } + response = ""; + } else { + response = response + ch; + } + } + + // Can send next command from queue to OBD + if (liveData->canSendNextAtCommand) { + liveData->canSendNextAtCommand = false; + doNextQueueCommand(); + } +} + +/** + Do next AT command from queue + */ +bool CommInterface::doNextQueueCommand() { + + // Restart loop with AT commands + if (liveData->commandQueueIndex >= liveData->commandQueueCount) { + liveData->commandQueueIndex = liveData->commandQueueLoopFrom; + board->redrawScreen(); + + // log every queue loop (temp) TODO and seconds interval + liveData->params.sdcardCanNotify = true; + } + + // Send AT command to obd + liveData->commandRequest = liveData->commandQueue[liveData->commandQueueIndex]; + if (liveData->commandRequest.startsWith("ATSH")) { + liveData->currentAtshRequest = liveData->commandRequest; + } + + Serial.print(">>> "); + Serial.println(liveData->commandRequest); + liveData->responseRowMerged = ""; + executeCommand(liveData->commandRequest); + liveData->commandQueueIndex++; +} + +/** + Parse result from OBD, merge frames to sigle response + */ +bool CommInterface::parseResponse() { + + // 1 frame data + Serial.println(liveData->responseRow); + + // Merge frames 0:xxxx 1:yyyy 2:zzzz to single response xxxxyyyyzzzz string + if (liveData->responseRow.length() >= 2 && liveData->responseRow.charAt(1) == ':') { + liveData->responseRowMerged += liveData->responseRow.substring(2); + } +} diff --git a/CommInterface.h b/CommInterface.h index 7b1cd65..235fc97 100644 --- a/CommInterface.h +++ b/CommInterface.h @@ -17,5 +17,9 @@ class CommInterface { virtual void connectDevice() = 0; virtual void disconnectDevice() = 0; virtual void scanDevices() = 0; - virtual void mainLoop() = 0; + virtual void mainLoop(); + virtual void executeCommand(String cmd) = 0; + // + bool doNextQueueCommand(); + bool parseResponse(); }; diff --git a/CommObd2Ble4.cpp b/CommObd2Ble4.cpp index b85c73f..d188344 100644 --- a/CommObd2Ble4.cpp +++ b/CommObd2Ble4.cpp @@ -333,81 +333,26 @@ void CommObd2Ble4::mainLoop() { // Serve first command (ATZ) doNextAtCommand(); + } else { Serial.println("We have failed to connect to the server; there is nothing more we will do."); } } - // Send command from TTY to OBD2 - if (Serial.available()) { - ch = Serial.read(); - if (ch == '\r' || ch == '\n') { - board->customConsoleCommand(response); - response = response + ch; - Serial.println(response); - if (liveData->bleConnected) { - liveData->pRemoteCharacteristicWrite->writeValue(response.c_str(), response.length()); - } - response = ""; - } else { - response = response + ch; - } - } - - // Can send next command from queue to OBD - if (liveData->canSendNextAtCommand) { - liveData->canSendNextAtCommand = false; - doNextAtCommand(); - } + // Parent declaration + CommInterface::mainLoop(); if (board->scanDevices) { board->scanDevices = false; startBleScan(); } - } /** - Do next AT command from queue -*/ -bool CommObd2Ble4::doNextAtCommand() { + * Send command + */ +void CommObd2Ble4::executeCommand(String cmd) { - // Restart loop with AT commands - if (liveData->commandQueueIndex >= liveData->commandQueueCount) { - liveData->commandQueueIndex = liveData->commandQueueLoopFrom; - board->redrawScreen(); - // log every queue loop (temp) - liveData->params.sdcardCanNotify = true; - } - - // Send AT command to obd - liveData->commandRequest = liveData->commandQueue[liveData->commandQueueIndex]; - if (liveData->commandRequest.startsWith("ATSH")) { - liveData->currentAtshRequest = liveData->commandRequest; - } - - Serial.print(">>> "); - Serial.println(liveData->commandRequest); - String tmpStr = liveData->commandRequest + "\r"; - liveData->responseRowMerged = ""; + String tmpStr = cmd + "\r"; liveData->pRemoteCharacteristicWrite->writeValue(tmpStr.c_str(), tmpStr.length()); - liveData->commandQueueIndex++; - - return true; -} - -/** - Parse result from OBD, merge frames to sigle response -*/ -bool CommObd2Ble4::parseRow() { - - // 1 frame data - Serial.println(liveData->responseRow); - - // Merge frames 0:xxxx 1:yyyy 2:zzzz to single response xxxxyyyyzzzz string - if (liveData->responseRow.length() >= 2 && liveData->responseRow.charAt(1) == ':') { - liveData->responseRowMerged += liveData->responseRow.substring(2); - } - - return true; } diff --git a/CommObd2Ble4.h b/CommObd2Ble4.h index 2f5eca1..4d08aba 100644 --- a/CommObd2Ble4.h +++ b/CommObd2Ble4.h @@ -13,10 +13,9 @@ class CommObd2Ble4 : public CommInterface { void disconnectDevice() override; void scanDevices() override; void mainLoop() override; + void executeCommand(String cmd) override; // void startBleScan(); bool connectToServer(BLEAddress pAddress); - bool doNextAtCommand(); - bool parseRow(); // }; diff --git a/CommObd2Can.cpp b/CommObd2Can.cpp index 72d3fe0..d10025a 100644 --- a/CommObd2Can.cpp +++ b/CommObd2Can.cpp @@ -46,5 +46,27 @@ void CommObd2Can::scanDevices() { Main loop */ void CommObd2Can::mainLoop() { - + + CommInterface::mainLoop(); +} + +/** + * Send command to CAN bus + */ +void executeCommand(String cmd) { + + /** + * Serial.println("BMS 220101"); + sendPID(0x00, 0x00); + delay(delayTxRx); + byte b = receivePID(0); + if (b == 0x10) { + Serial.println("CONTINUE"); + sendPID(0x01, 0x00); + delay(10); + for (byte i = 0; i < 20; i++) { + receivePID(0); + delay(10); + } + }*/ } diff --git a/CommObd2Can.h b/CommObd2Can.h index aea03cb..a61696a 100644 --- a/CommObd2Can.h +++ b/CommObd2Can.h @@ -18,6 +18,7 @@ class CommObd2Can : public CommInterface { void disconnectDevice() override; void scanDevices() override; void mainLoop() override; + void executeCommand(String cmd) override; // /* void startBleScan(); bool connectToServer(BLEAddress pAddress);