Skip to content

Commit

Permalink
Merge branch 'modbus-must' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
softwarecrash authored Oct 6, 2024
2 parents 98a6988 + 8490972 commit 0bdc651
Show file tree
Hide file tree
Showing 7 changed files with 745 additions and 65 deletions.
3 changes: 2 additions & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
platform = [email protected]
framework = arduino
monitor_speed = 115200
custom_prog_version = 1.1.8
custom_prog_version = 1.2.2
build_flags =
-DVERSION=${this.custom_prog_version}
-DPIO_SRC_NAM="Solar2MQTT"
Expand All @@ -30,6 +30,7 @@ lib_deps =
plerup/EspSoftwareSerial @ ^8.2.0
https://github.com/dok-net/ghostl
robtillaart/CRC@^1.0.1
4-20ma/ModbusMaster@^2.0.1

[env:d1_mini]
board = d1_mini
Expand Down
152 changes: 98 additions & 54 deletions src/PI_Serial/PI_Serial.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// #define isDEBUG
#include "ArduinoJson.h"
#include "PI_Serial.h"
SoftwareSerial myPort;

#include "CRC16.h"
#include "CRC.h"
CRC16 crc;
Expand All @@ -23,9 +23,7 @@ extern void writeLog(const char *format, ...);

PI_Serial::PI_Serial(int rx, int tx)
{
soft_rx = rx;
soft_tx = tx;
this->my_serialIntf = &myPort;
this->my_serialIntf = new SoftwareSerial(rx, tx, false); // init pins
}

bool PI_Serial::Init()
Expand All @@ -37,9 +35,17 @@ bool PI_Serial::Init()
return false;
}
autoDetect();
if (isModbus())
{
if (requestCallback)
{
modbus->callback(requestCallback);
}
return true;
}
this->my_serialIntf->setTimeout(500);
this->my_serialIntf->enableRxGPIOPullUp(true);
this->my_serialIntf->begin(serialIntfBaud, SWSERIAL_8N1, soft_rx, soft_tx, false);
this->my_serialIntf->begin(serialIntfBaud, SWSERIAL_8N1);
return true;
}

Expand All @@ -56,53 +62,68 @@ bool PI_Serial::loop()
previousTime = millis();
return true;
}
switch (requestStaticData)
if (isModbus())
{
case true:
switch (requestCounter)
{
case 0:
requestCounter = PIXX_QPIRI() ? (requestCounter + 1) : 0;
break;
case 1:
requestCounter = PIXX_QMN() ? (requestCounter + 1) : 0;
break;
case 2:
requestCounter = PIXX_QPI() ? (requestCounter + 1) : 0;
requestCounter = 0;
requestStaticData = false;
break;
}
break;
case false:
switch (requestCounter)
modbus->loop();
}
else
{
switch (requestStaticData)
{
case 0:
requestCounter = PIXX_QPIGS() ? (requestCounter + 1) : 0;
break;
case 1:
requestCounter = PIXX_QPIGS2() ? (requestCounter + 1) : 0;
break;
case 2:
requestCounter = PIXX_QMOD() ? (requestCounter + 1) : 0;
case true:
switch (requestCounter)
{
case 0:
requestCounter = PIXX_QPIRI() ? (requestCounter + 1) : 0;
break;
case 1:
requestCounter = PIXX_QMN() ? (requestCounter + 1) : 0;
break;
case 2:
requestCounter = PIXX_QPI() ? (requestCounter + 1) : 0;
requestCounter = 0;
requestStaticData = false;
break;
}
break;
case 3:
requestCounter = PIXX_Q1() ? (requestCounter + 1) : 0;
break;
case 4:
requestCounter = PIXX_QEX() ? (requestCounter + 1) : 0;
break;
case 5:
requestCounter = PIXX_QPIWS() ? (requestCounter + 1) : 0;
break;
case 6:
requestCallback();
requestCounter = 0;

case false:
switch (requestCounter)
{
case 0:
requestCounter = PIXX_QPIGS() ? (requestCounter + 1) : 0;
break;
case 1:
requestCounter = PIXX_QPIGS2() ? (requestCounter + 1) : 0;
break;
case 2:
requestCounter = PIXX_QMOD() ? (requestCounter + 1) : 0;
break;
case 3:
requestCounter = PIXX_Q1() ? (requestCounter + 1) : 0;
break;
case 4:
requestCounter = PIXX_QEX() ? (requestCounter + 1) : 0;
break;
case 5:
requestCounter = PIXX_QPIWS() ? (requestCounter + 1) : 0;
break;
case 6:
requestCallback();
requestCounter = 0;
break;
}
break;
}
break;
}
connection = (connectionCounter < 10) ? true : false;
if (isModbus())
{
connection = modbus->connection;
}
else
{
connection = (connectionCounter < 10) ? true : false;
}
previousTime = millis();
}
else
Expand Down Expand Up @@ -142,9 +163,9 @@ void PI_Serial::autoDetect() // function for autodetect the inverter type

startChar = "(";
serialIntfBaud = 2400;
this->my_serialIntf->begin(serialIntfBaud, SWSERIAL_8N1, soft_rx, soft_tx, false);
this->my_serialIntf->begin(serialIntfBaud, SWSERIAL_8N1);
String qpi = this->requestData("QPI");
writeLog("QPI:\t\t%s (Length: %d)", qpi,qpi.length());
writeLog("QPI:\t\t%s (Length: %d)", qpi, qpi.length());
if (qpi != "" && qpi.substring(0, 2) == "PI")
{
writeLog("<Autodetect> Match protocol: PI3X");
Expand All @@ -153,7 +174,7 @@ void PI_Serial::autoDetect() // function for autodetect the inverter type
break;
}
startChar = "^Dxxx";
this->my_serialIntf->begin(serialIntfBaud, SWSERIAL_8N1, soft_rx, soft_tx, false);
this->my_serialIntf->begin(serialIntfBaud, SWSERIAL_8N1);
String P005PI = this->requestData("^P005PI");
writeLog("^P005PI:\t\t%s (Length: %d)", P005PI, P005PI.length());
if (P005PI != "" && P005PI == "18")
Expand All @@ -165,20 +186,38 @@ void PI_Serial::autoDetect() // function for autodetect the inverter type
}
this->my_serialIntf->end();
}
this->my_serialIntf->end();
if (protocol == NoD)
{
modbus = new MODBUS(this->my_serialIntf);
modbus->Init();
if (modbus->autoDetect()){
protocol = MODBUS_MUST;
}
}
writeLog("----------------- End Autodetect -----------------");
}

bool PI_Serial::sendCustomCommand()
{
if (customCommandBuffer == "")
return false;
get.raw.commandAnswer = requestData(customCommandBuffer);

if (isModbus())
{
get.raw.commandAnswer = modbus->requestData(customCommandBuffer);
}
else
{
get.raw.commandAnswer = requestData(customCommandBuffer);
}
customCommandBuffer = "";
return true;
}

String PI_Serial::requestData(String command)
{

String commandBuffer = "";
uint16_t crcCalc = 0;
uint16_t crcRecive = 0;
Expand All @@ -203,7 +242,7 @@ String PI_Serial::requestData(String command)
commandBuffer.remove(commandBuffer.length() - 2); // remove the crc
commandBuffer.remove(0, strlen(startChar)); // remove the start char ( for Pi30 and ^Dxxx for Pi18

//requestOK++;
// requestOK++;
connectionCounter = 0;
}
else if (getCHK(commandBuffer.substring(0, commandBuffer.length() - 1)) + 1 == commandBuffer[commandBuffer.length() - 1] &&
Expand All @@ -216,7 +255,7 @@ String PI_Serial::requestData(String command)
commandBuffer.remove(commandBuffer.length() - 1); // remove the crc
commandBuffer.remove(0, strlen(startChar)); // remove the start char ( for Pi30 and ^Dxxx for Pi18

//requestOK++;
// requestOK++;
connectionCounter = 0;
}
else if (commandBuffer.indexOf("NAK", strlen(startChar)) > 0) // catch NAK without crc
Expand All @@ -229,7 +268,7 @@ String PI_Serial::requestData(String command)
}
else
{
writeLog("ERROR Send: >%s< Recive: >%s<", command, commandBuffer);
writeLog("ERROR Send: >%s< Recive: >%s<", command, commandBuffer);
connectionCounter++;
commandBuffer = "ERCRC";
}
Expand Down Expand Up @@ -349,4 +388,9 @@ char *PI_Serial::getModeDesc(char mode) // get the char from QMOD and make reada
break;
}
return modeString;
}
}

bool PI_Serial::isModbus()
{
return protocol == MODBUS_MUST;
}
23 changes: 14 additions & 9 deletions src/PI_Serial/PI_Serial.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#define PI_SERIAL_H
#include "vector"
#include <ArduinoJson.h>
#include <modbus/modbus.h>
extern JsonObject deviceJson;
extern JsonObject staticData;
extern JsonObject liveData;
Expand Down Expand Up @@ -86,9 +87,15 @@ class PI_Serial
void callback(std::function<void()> func);
std::function<void()> requestCallback;

private:
unsigned int soft_tx;
unsigned int soft_rx;
enum protocolType
{
NoD,
PI18,
PI30,
MODBUS_MUST
};

private:
unsigned int serialIntfBaud;

unsigned long previousTime = 0;
Expand All @@ -100,12 +107,8 @@ class PI_Serial
byte qexCounter = 0;

String customCommandBuffer;
enum protocolType
{
NoD,
PI18,
PI30,
};

MODBUS *modbus;

/**
* @brief get the crc from a string
Expand Down Expand Up @@ -150,6 +153,8 @@ class PI_Serial
bool PIXX_QPIRI();
bool PIXX_QPI();
bool PIXX_QMN();

bool isModbus();
};

#endif
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ void loop()
if (restartNow && millis() >= (RestartTimer + 500))
{
ESP.restart();
}
}
notificationLED(); // notification LED routine
}

Expand Down
Loading

0 comments on commit 0bdc651

Please sign in to comment.