mirror of
https://github.com/UtilitechAS/amsreader-firmware.git
synced 2026-05-04 15:16:44 +00:00
Fixed autodetect
This commit is contained in:
@@ -35,6 +35,9 @@ void PassiveMeterCommunicator::configure(MeterConfig& meterConfig, Timezone* tz)
|
|||||||
bool PassiveMeterCommunicator::loop() {
|
bool PassiveMeterCommunicator::loop() {
|
||||||
if(hanBufferSize == 0) return false;
|
if(hanBufferSize == 0) return false;
|
||||||
|
|
||||||
|
unsigned long now = millis();
|
||||||
|
if(autodetect) handleAutodetect(now);
|
||||||
|
|
||||||
unsigned long start, end;
|
unsigned long start, end;
|
||||||
if(!hanSerial->available()) {
|
if(!hanSerial->available()) {
|
||||||
return false;
|
return false;
|
||||||
@@ -47,9 +50,6 @@ bool PassiveMeterCommunicator::loop() {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned long now = millis();
|
|
||||||
if(autodetect) handleAutodetect(now);
|
|
||||||
|
|
||||||
dataAvailable = false;
|
dataAvailable = false;
|
||||||
ctx = {0,0,0,0};
|
ctx = {0,0,0,0};
|
||||||
memset(ctx.system_title, 0, 8);
|
memset(ctx.system_title, 0, 8);
|
||||||
@@ -696,17 +696,15 @@ void PassiveMeterCommunicator::handleAutodetect(unsigned long now) {
|
|||||||
if(!validDataReceived) {
|
if(!validDataReceived) {
|
||||||
if(now - meterAutodetectLastChange > 20000 && (meterConfig.baud == 0 || meterConfig.parity == 0)) {
|
if(now - meterAutodetectLastChange > 20000 && (meterConfig.baud == 0 || meterConfig.parity == 0)) {
|
||||||
autodetect = true;
|
autodetect = true;
|
||||||
pinMode(meterConfig.rxPin, INPUT);
|
if(autodetectCount == 2) {
|
||||||
digitalWrite (meterConfig.rxPin, HIGH);
|
autodetectInvert = !autodetectInvert;
|
||||||
autodetectBaud = detectBaudRate(meterConfig.rxPin);
|
autodetectCount = 0;
|
||||||
|
}
|
||||||
|
autodetectBaud = AUTO_BAUD_RATES[autodetectCount++];
|
||||||
if (debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("Meter serial autodetect, swapping to: %d, %d, %s\n"), autodetectBaud, autodetectParity, autodetectInvert ? "true" : "false");
|
if (debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("Meter serial autodetect, swapping to: %d, %d, %s\n"), autodetectBaud, autodetectParity, autodetectInvert ? "true" : "false");
|
||||||
meterConfig.bufferSize = max((uint32_t) 1, autodetectBaud / 14400);
|
meterConfig.bufferSize = max((uint32_t) 1, autodetectBaud / 14400);
|
||||||
setupHanPort(autodetectBaud, autodetectParity, autodetectInvert);
|
setupHanPort(autodetectBaud, autodetectParity, autodetectInvert);
|
||||||
meterAutodetectLastChange = now;
|
meterAutodetectLastChange = now;
|
||||||
if(autodetectCount++ == 5) {
|
|
||||||
autodetectInvert = !autodetectInvert;
|
|
||||||
autodetectCount = 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else if(autodetect) {
|
} else if(autodetect) {
|
||||||
if (debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("Meter serial autodetected, saving: %d, %d, %s\n"), autodetectBaud, autodetectParity, autodetectInvert ? "true" : "false");
|
if (debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("Meter serial autodetected, saving: %d, %d, %s\n"), autodetectBaud, autodetectParity, autodetectInvert ? "true" : "false");
|
||||||
@@ -718,37 +716,3 @@ void PassiveMeterCommunicator::handleAutodetect(unsigned long now) {
|
|||||||
setupHanPort(meterConfig.baud, meterConfig.parity, meterConfig.invert);
|
setupHanPort(meterConfig.baud, meterConfig.parity, meterConfig.invert);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t PassiveMeterCommunicator::detectBaudRate(uint8_t pin) {
|
|
||||||
long x;
|
|
||||||
for (int i = 0; i < 5; i++){
|
|
||||||
while(digitalRead(pin) == 1){} // wait for low bit to start
|
|
||||||
x = pulseIn(pin, LOW); // measure the next zero bit width
|
|
||||||
rate = x < rate ? x : rate;
|
|
||||||
}
|
|
||||||
if (rate < 12)
|
|
||||||
return 115200;
|
|
||||||
else if (rate < 20)
|
|
||||||
return 57600;
|
|
||||||
else if (rate < 29)
|
|
||||||
return 38400;
|
|
||||||
else if (rate < 40)
|
|
||||||
return 28800;
|
|
||||||
else if (rate < 60)
|
|
||||||
return 19200;
|
|
||||||
else if (rate < 80)
|
|
||||||
return 14400;
|
|
||||||
else if (rate < 150)
|
|
||||||
return 9600;
|
|
||||||
else if (rate < 300)
|
|
||||||
return 4800;
|
|
||||||
else if (rate < 600)
|
|
||||||
return 2400;
|
|
||||||
else if (rate < 1200)
|
|
||||||
return 1200;
|
|
||||||
else if (rate < 2400)
|
|
||||||
return 600;
|
|
||||||
else if (rate < 4800)
|
|
||||||
return 300;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -18,6 +18,8 @@
|
|||||||
#include "SoftwareSerial.h"
|
#include "SoftwareSerial.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
const uint32_t AUTO_BAUD_RATES[] = { 2400, 115200 };
|
||||||
|
|
||||||
class PassiveMeterCommunicator : public MeterCommunicator {
|
class PassiveMeterCommunicator : public MeterCommunicator {
|
||||||
public:
|
public:
|
||||||
PassiveMeterCommunicator(RemoteDebug* debugger);
|
PassiveMeterCommunicator(RemoteDebug* debugger);
|
||||||
@@ -79,7 +81,6 @@ protected:
|
|||||||
void debugPrint(byte *buffer, int start, int length);
|
void debugPrint(byte *buffer, int start, int length);
|
||||||
void printHanReadError(int pos);
|
void printHanReadError(int pos);
|
||||||
void handleAutodetect(unsigned long now);
|
void handleAutodetect(unsigned long now);
|
||||||
uint32_t detectBaudRate(uint8_t pin);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user