optional prefix size and serial invert (#18717)

This commit is contained in:
gemu 2023-05-23 08:46:00 +02:00 committed by GitHub
parent c1ec35a086
commit e969bf0e0e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 43 additions and 27 deletions

View File

@ -166,7 +166,7 @@ class SML_ESP32_SERIAL : public Stream {
public:
SML_ESP32_SERIAL(uint32_t uart_index);
virtual ~SML_ESP32_SERIAL();
bool begin(uint32_t speed, uint32_t smode, int32_t recpin, int32_t trxpin);
bool begin(uint32_t speed, uint32_t smode, int32_t recpin, int32_t trxpin, int32_t invert);
int32_t peek(void);
int read(void) override;
size_t write(uint8_t byte) override;
@ -226,7 +226,7 @@ void SML_ESP32_SERIAL::end(void) {
}
}
bool SML_ESP32_SERIAL::begin(uint32_t speed, uint32_t smode, int32_t recpin, int32_t trxpin) {
bool SML_ESP32_SERIAL::begin(uint32_t speed, uint32_t smode, int32_t recpin, int32_t trxpin, int invert) {
if (!m_valid) { return false; }
m_buffer = 0;
@ -246,7 +246,7 @@ bool SML_ESP32_SERIAL::begin(uint32_t speed, uint32_t smode, int32_t recpin, int
m_tx_pin = trxpin;
hws = new HardwareSerial(uart_index);
if (hws) {
hws->begin(speed, cfgmode, m_rx_pin, m_tx_pin);
hws->begin(speed, cfgmode, m_rx_pin, m_tx_pin, invert);
}
}
return true;
@ -383,6 +383,15 @@ typedef union {
};
} TRX_EN_TYPE;
typedef union {
uint8_t data;
struct {
uint8_t SO_DWS74_BUG : 1;
uint8_t SO_OBIS_LINE : 1;
uint8_t SO_TRX_INVERT : 1;
};
} SO_FLAGS;
#ifndef TMSBSIZ
#define TMSBSIZ 256
#endif
@ -391,19 +400,20 @@ typedef union {
#define SML_STIMEOUT 1000
#endif
#define SO_DWS74_BUG 1
#define SO_OBIS_LINE 2
#define METER_ID_SIZE 24
#define SML_CRYPT_SIZE 16
#ifndef SML_PREFIX_SIZE
#define SML_PREFIX_SIZE 8
#endif
struct METER_DESC {
int8_t srcpin;
uint8_t type;
uint16_t flag;
int32_t params;
char prefix[8];
char prefix[SML_PREFIX_SIZE];
int8_t trxpin;
uint8_t tsecs;
char *txmem;
@ -419,7 +429,7 @@ struct METER_DESC {
uint16_t sibsiz;
uint32_t lastms;
uint16_t tout_ms;
uint8_t so_flags;
SO_FLAGS so_flags;
char meter_id[METER_ID_SIZE];
#ifdef USE_SML_SPECOPT
uint32_t so_obis1;
@ -1018,7 +1028,7 @@ double dval;
break;
case 2:
// signed 16 bit
if (meter_desc[index].so_flags & SO_DWS74_BUG) {
if (meter_desc[index].so_flags.SO_DWS74_BUG) {
if (scaler == -2) {
value = (uint32_t)uvalue;
} else {
@ -1294,7 +1304,7 @@ void sml_shift_in(uint32_t meters, uint32_t shard) {
switch (mp->type) {
case 'o':
// asci obis
if (!(mp->so_flags & SO_OBIS_LINE)) {
if (!(mp->so_flags.SO_OBIS_LINE)) {
mp->sbuff[mp->sbsiz - 1] = iob & 0x7f;
} else {
iob &= 0x7f;
@ -2619,7 +2629,7 @@ struct METER_DESC *mp = &meter_desc[mnum];
break;
case '2':
cp += 2;
mp->so_flags = strtol(cp, &cp, 16);
mp->so_flags.data = strtol(cp, &cp, 16);
break;
case '3':
cp += 2;
@ -2720,14 +2730,14 @@ void reset_sml_vars(uint16_t maxmeters) {
mp->so_obis1 = 0;
mp->so_obis2 = 0;
#endif
mp->so_flags = 0;
mp->so_flags.data = 0;
// addresses a bug in meter DWS74
#ifdef DWS74_BUG
mp->so_flags |= SO_DWS74_BUG;
mp->so_flags.SO_DWS74_BUG = 1;
#endif
#ifdef SML_OBIS_LINE
mp->so_flags |= SO_OBIS_LINE;
mp->so_flags.SO_OBIS_LINE = 1;
#endif
if (mp->txmem) {
free(mp->txmem);
@ -2931,8 +2941,8 @@ dddef_exit:
mmp->params = strtol(lp, &lp, 10);
if (*lp != ',') goto next_line;
lp++;
mmp->prefix[7] = 0;
for (uint32_t cnt = 0; cnt < 8; cnt++) {
mmp->prefix[SML_PREFIX_SIZE - 1] = 0;
for (uint32_t cnt = 0; cnt < SML_PREFIX_SIZE; cnt++) {
if (*lp == SCRIPT_EOL || *lp == ',') {
mmp->prefix[cnt] = 0;
break;
@ -3208,12 +3218,14 @@ next_line:
if (mp->meter_ss->hardwareSerial()) {
Serial.begin(mp->params, (SerialConfig)smode);
ClaimSerial();
//Serial.setRxBufferSize(512);
if (mp->so_flags.SO_TRX_INVERT) {
U0C0 = U0C0 | BIT(UCRXI) | BIT(UCTXI); // Inverse RX, TX
}
}
#endif // ESP8266
#ifdef ESP32
mp->meter_ss->begin(mp->params, smode, mp->srcpin, mp->trxpin);
mp->meter_ss->begin(mp->params, smode, mp->srcpin, mp->trxpin, mp->so_flags.SO_TRX_INVERT);
#ifdef USE_ESP32_SW_SERIAL
mp->meter_ss->setRxBufferSize(mp->sibsiz);
#endif
@ -3244,7 +3256,7 @@ next_line:
struct METER_DESC *mp = &meter_desc[meters];
char type = mp->type;
if (!(mp->so_flags & SO_OBIS_LINE)) {
if (!(mp->so_flags.SO_OBIS_LINE)) {
mp->shift_mode = (type != 'e' && type != 'k' && type != 'm' && type != 'M' && type != 'p' && type != 'R' && type != 'v');
} else {
mp->shift_mode = (type != 'o' && type != 'e' && type != 'k' && type != 'm' && type != 'M' && type != 'p' && type != 'R' && type != 'v');
@ -3337,7 +3349,7 @@ uint32_t SML_Write(int32_t meter, char *hstr) {
#ifdef ESP8266
Serial.begin(baud, (SerialConfig)smode);
#else
meter_desc[meter].meter_ss->begin(baud, smode, sml_globs.mp[meter].srcpin, sml_globs.mp[meter].trxpin);
meter_desc[meter].meter_ss->begin(baud, smode, sml_globs.mp[meter].srcpin, sml_globs.mp[meter].trxpin, sml_globs.mp[meter].so_flags.SO_TRX_INVERT);
#endif
}
return 1;
@ -3616,7 +3628,9 @@ uint16_t sml_swap(uint16_t in) {
void sml_tcp_send(uint32_t meter, uint8_t *sbuff, uint16_t slen) {
MODBUS_TCP_HEADER tcph;
tcph.T_ID = sml_swap(0x1234);
//tcph.T_ID = sml_swap(0x1234);
tcph.T_ID = random(0xffff);
tcph.P_ID = 0;
tcph.SIZE = sml_swap(6);
tcph.U_ID = *sbuff;
@ -3990,12 +4004,14 @@ bool Xsns53(uint32_t function) {
SML_Init();
break;
case FUNC_LOOP:
if (sml_globs.ready) {
SML_Counter_Poll();
if (sml_globs.dump2log) {
dump2log();
} else {
SML_Poll();
if (bitRead(Settings->rule_enabled, 0)) {
if (sml_globs.ready) {
SML_Counter_Poll();
if (sml_globs.dump2log) {
dump2log();
} else {
SML_Poll();
}
}
}
break;