Skip to content

Commit

Permalink
Added more detailed return codes for sending to the bus, bugfix for e…
Browse files Browse the repository at this point in the history
…mpty QINF
  • Loading branch information
fredlcore committed Mar 15, 2024
1 parent 8cd5e74 commit 528a39a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 29 deletions.
33 changes: 17 additions & 16 deletions BSB_LAN/BSB_LAN.ino
Original file line number Diff line number Diff line change
Expand Up @@ -3548,7 +3548,7 @@ int set(float line // the ProgNr of the heater parameter
case VT_CUSTOM_ENUM:
{
uint8_t t=atoi(val);
bus->Send(TYPE_QUR, c, msg, tx_msg);
bus->Send(TYPE_QINF, c, msg, tx_msg);
int data_len;
if (bus->getBusType() == BUS_LPB) {
data_len=msg[bus->getLen_idx()]-14; // get packet length, then subtract
Expand Down Expand Up @@ -3599,13 +3599,13 @@ int set(float line // the ProgNr of the heater parameter
}

// Send telegram to the bus
if (!bus->Send(t // message type
if (bus->Send(t // message type
, c // command code
, msg // receive buffer
, tx_msg // xmit buffer
, param // payload
, param_len // payload length
, setcmd)) // wait_for_reply
, setcmd) < 0) // wait_for_reply
{
printFmtToDebug(PSTR("set failed\r\n"));
return 0;
Expand Down Expand Up @@ -3660,7 +3660,7 @@ int queryDefaultValue(float line, byte *msg, byte *tx_msg) {
decodedTelegram.error = 258; //not found
return 0;
} else {
if (!bus->Send(TYPE_QRV, c, msg, tx_msg)) {
if (bus->Send(TYPE_QRV, c, msg, tx_msg) < 0) {
decodedTelegram.error = 261; //query failed
return 0;
} else {
Expand Down Expand Up @@ -3713,7 +3713,7 @@ const char* printError(uint16_t error) {
char *build_pvalstr(bool extended) {
int len = 0;
outBuf[len] = 0;
if (extended && decodedTelegram.error != 257) {
if (extended && decodedTelegram.error != 257 && decodedTelegram.prognr >= 0) {
len+=sprintf_P(outBuf, PSTR("%g "), decodedTelegram.prognr);

len+=strlen(strcpy_PF(outBuf + len, decodedTelegram.catdescaddr));
Expand Down Expand Up @@ -4127,7 +4127,7 @@ void query(float line) { // line (ProgNr)
retry=QUERY_RETRIES;
while (retry) {
uint8_t query_type = TYPE_QUR;
if (bus->Send(query_type, c, msg, tx_msg)) {
if (bus->Send(query_type, c, msg, tx_msg) == 1) {
// Decode the xmit telegram and send it to the PC serial interface
if (verbose) {
printTelegram(tx_msg, line);
Expand Down Expand Up @@ -4341,7 +4341,7 @@ void SetDateTime() {
if (bus->getBusType() != BUS_PPS) {
findLine(0,0,&c);
if (c!=CMD_UNKNOWN) { // send only valid command codes
if (bus->Send(TYPE_QUR, c, rx_msg, tx_msg)) {
if (bus->Send(TYPE_QUR, c, rx_msg, tx_msg) == 1) {
if (bus->getBusType() == BUS_LPB) {
setTime(rx_msg[18], rx_msg[19], rx_msg[20], rx_msg[16], rx_msg[15], rx_msg[14]+1900);
} else {
Expand Down Expand Up @@ -5291,7 +5291,7 @@ void loop() {
flushToWebClient();

uint8_t found_ids[10] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
if (bus->Send(TYPE_QINF, 0x053D0002, msg, tx_msg, NULL, 0, false)) {
if (bus->Send(TYPE_QINF, 0x053D0002, msg, tx_msg, NULL, 0, false) == 1) {
printTelegram(tx_msg, -1);
unsigned long startquery = millis();
while (millis() - startquery < 10000) {
Expand Down Expand Up @@ -5388,7 +5388,7 @@ void loop() {
#endif
if (((dev_fam != temp_dev_fam && dev_fam != DEV_FAM(DEV_ALL)) || (dev_var != temp_dev_var && dev_var != DEV_VAR(DEV_ALL))) && c!=CMD_UNKNOWN) {
printFmtToDebug(PSTR("%02X\r\n"), c);
if (!bus->Send(TYPE_QUR, c, msg, tx_msg)) {
if (bus->Send(TYPE_QUR, c, msg, tx_msg) < 0) {
print_bus_send_failed();
} else {
if (msg[4+(bus->getBusType()*4)]!=TYPE_ERR) {
Expand Down Expand Up @@ -5432,7 +5432,7 @@ void loop() {
c = 0;
int outBufLen = strlen(outBuf);
unsigned long timeout = millis() + 3000;
while (!bus->Send(TYPE_QUR, 0x053D0001, msg, tx_msg) && (millis() < timeout)) {
while (bus->Send(TYPE_QUR, 0x053D0001, msg, tx_msg) < 0 && (millis() < timeout)) {
printTelegram(tx_msg, -1);
printTelegram(msg, -1);
delay(500);
Expand All @@ -5443,7 +5443,7 @@ void loop() {
printToWebClient(outBuf + outBufLen);
printToWebClient(PSTR("\r\n"));
timeout = millis() + 3000;
while (!bus->Send(TYPE_QUR, 0x053D0064, msg, tx_msg) && (millis() < timeout)) {
while (bus->Send(TYPE_QUR, 0x053D0064, msg, tx_msg) < 0 && (millis() < timeout)) {
printTelegram(tx_msg, -1);
printTelegram(msg, -1);
delay(500);
Expand All @@ -5455,7 +5455,7 @@ void loop() {
printToWebClient(PSTR("\r\n"));
flushToWebClient();
timeout = millis() + 3000;
while (!bus->Send(TYPE_IQ1, c, msg, tx_msg) && (millis() < timeout)) {
while (bus->Send(TYPE_IQ1, c, msg, tx_msg) < 0 && (millis() < timeout)) {
printTelegram(tx_msg, -1);
printTelegram(msg, -1);
printToWebClient(PSTR("Didn't receive matching telegram, resending...\r\n"));
Expand All @@ -5466,7 +5466,7 @@ void loop() {
int IA1_max = (msg[7+bus->getBusType()*4] << 8) + msg[8+bus->getBusType()*4];
if (msg[4+bus->getBusType()*4] == 0x13 && IA1_max > 0) {
timeout = millis() + 3000;
while (!bus->Send(TYPE_IQ2, c, msg, tx_msg) && (millis() < timeout)) {
while (bus->Send(TYPE_IQ2, c, msg, tx_msg) < 0 && (millis() < timeout)) {
printToWebClient(PSTR("Didn't receive matching telegram, resending...\r\n"));
delay(500);
}
Expand All @@ -5478,7 +5478,7 @@ void loop() {
esp_task_wdt_reset();
#endif
timeout = millis() + 3000;
while (!bus->Send(TYPE_IQ1, IA1_counter, msg, tx_msg) && (millis() < timeout)) {
while (bus->Send(TYPE_IQ1, IA1_counter, msg, tx_msg) < 0 && (millis() < timeout)) {
printToWebClient(PSTR("Didn't receive matching telegram, resending...\r\n"));
delay(500);
}
Expand All @@ -5492,7 +5492,7 @@ void loop() {
esp_task_wdt_reset();
#endif
timeout = millis() + 3000;
while (!bus->Send(TYPE_IQ2, IA2_counter, msg, tx_msg) && (millis() < timeout)) {
while (bus->Send(TYPE_IQ2, IA2_counter, msg, tx_msg) < 0 && (millis() < timeout)) {
printToWebClient(PSTR("Didn't receive matching telegram, resending...\r\n"));
delay(500);
}
Expand Down Expand Up @@ -5528,7 +5528,8 @@ void loop() {
counter = counter + 2;
}
}
if (!bus->Send(type, c, msg, tx_msg, param, param_len, true)) {
int8_t return_value = bus->Send(type, c, msg, tx_msg, param, param_len, true);
if (return_value < 0) {
print_bus_send_failed();
} else {
// Decode the xmit telegram and send it to the PC serial interface
Expand Down
23 changes: 12 additions & 11 deletions BSB_LAN/src/BSB/bsb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ uint16_t BSB::_crc_xmodem_update (uint16_t crc, uint8_t data) {
}

// Low-Level sending of message to bus
inline bool BSB::_send(byte* msg) {
inline int8_t BSB::_send(byte* msg) {
// Nun - Ein Teilnehmer will senden :
byte data, len;
if (bus_type != 2) {
Expand Down Expand Up @@ -401,7 +401,7 @@ inline bool BSB::_send(byte* msg) {
waitfree = random(1,20) + 3 + 59; // range 63 .. 82 ms, BSB mimimum delay between telegrams is 59 ms (25 for LPB -> miwi), plus duration of one full (32 bytes) telegram (3 ms), plus random amount of 1-20 ms.
{ // block begins
if(millis()-start_timer > timeoutabort){ // one second has elapsed
return false;
return -1;
}
if (bus_type != 2) {
unsigned long timeout = millis();
Expand Down Expand Up @@ -503,7 +503,7 @@ Ob damit weiterhin eine Bus-Kollisionserkennung möglich ist, muss noch geprüft
char c = readByte();
c = c;
}
return true; // In case we emulate a DC225, we are regularly sending single byte (0x17) messages, so abort loop after first byte.
return 1; // In case we emulate a DC225, we are regularly sending single byte (0x17) messages, so abort loop after first byte.
}
/*
// if ((HwSerial == true && rx_pin_read() == false) || (HwSerial == false && rx_pin_read())) { // Test RX pin (logical 1 is 0 with HardwareSerial and 1 with SoftwareSerial inverted)
Expand Down Expand Up @@ -555,10 +555,10 @@ Ob damit weiterhin eine Bus-Kollisionserkennung möglich ist, muss noch geprüft
sei();
#endif
}
return true;
return 1;
}

bool BSB::Send(uint8_t type, uint32_t cmd, byte* rx_msg, byte* tx_msg, byte* param, byte param_len, bool wait_for_reply) {
int8_t BSB::Send(uint8_t type, uint32_t cmd, byte* rx_msg, byte* tx_msg, byte* param, byte param_len, bool wait_for_reply) {
byte i;
byte offset = 0;

Expand Down Expand Up @@ -619,8 +619,9 @@ bool BSB::Send(uint8_t type, uint32_t cmd, byte* rx_msg, byte* tx_msg, byte* par
tx_msg[9+i] = param[i];
}
}
if(!_send(tx_msg)) return false;
if(!wait_for_reply) return true;
int8_t return_value = _send(tx_msg);
if(return_value =! 1) return return_value;
if(!wait_for_reply) return return_value;

i=15;

Expand All @@ -634,7 +635,7 @@ bool BSB::Send(uint8_t type, uint32_t cmd, byte* rx_msg, byte* tx_msg, byte* par
i--;
byte msg_type = rx_msg[4+(bus_type*4)];
if (rx_msg[2] == myAddr && ((type == 0x12 && msg_type == 0x13) || (type=0x14 && msg_type == 0x15))) {
return true;
return 1;
}
if (bus_type == 1) {
/* Activate for LPB systems with truncated error messages (no commandID in return telegram)
Expand All @@ -643,7 +644,7 @@ bool BSB::Send(uint8_t type, uint32_t cmd, byte* rx_msg, byte* tx_msg, byte* par
}
*/
if (rx_msg[2] == myAddr && rx_msg[9] == A2 && rx_msg[10] == A1 && rx_msg[11] == A3 && rx_msg[12] == A4) {
return true;
return 1;
} else {
#if DEBUG_LL
Serial.println(F("Message received, but not for us:"));
Expand All @@ -652,7 +653,7 @@ bool BSB::Send(uint8_t type, uint32_t cmd, byte* rx_msg, byte* tx_msg, byte* par
}
} else {
if ((rx_msg[2] == myAddr) && (rx_msg[5] == A2) && (rx_msg[6] == A1) && (rx_msg[7] == A3) && (rx_msg[8] == A4)) {
return true;
return 1;
} else {
#if DEBUG_LL
Serial.println(F("Message received, but not for us:"));
Expand All @@ -669,7 +670,7 @@ bool BSB::Send(uint8_t type, uint32_t cmd, byte* rx_msg, byte* tx_msg, byte* par
#endif
print(tx_msg);

return false;
return -2;
}

bool BSB::rx_pin_read() { // not tested if this will work on ESP32
Expand Down
4 changes: 2 additions & 2 deletions BSB_LAN/src/BSB/bsb.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class BSB
uint8_t getPl_start();
uint8_t getLen_idx();

bool Send(uint8_t type, uint32_t cmd, byte* rx_msg, byte* tx_msg, byte* param=NULL, byte param_len=0, bool wait_for_reply=true);
int8_t Send(uint8_t type, uint32_t cmd, byte* rx_msg, byte* tx_msg, byte* param=NULL, byte param_len=0, bool wait_for_reply=true);

private:
boolean HwSerial = false;
Expand All @@ -63,7 +63,7 @@ class BSB
uint8_t pl_start;
uint8_t rx_pin;
uint8_t tx_pin;
inline bool _send(byte* msg);
inline int8_t _send(byte* msg);
uint16_t CRC (byte* buffer, uint8_t length);
uint16_t CRC_LPB (byte* buffer, uint8_t length);
uint8_t CRC_PPS (byte* buffer, uint8_t length);
Expand Down

0 comments on commit 528a39a

Please sign in to comment.