mirror of
https://github.com/eddyem/BTA_utils.git
synced 2025-12-06 02:35:13 +03:00
some fixes with meteo
This commit is contained in:
parent
a00200af3c
commit
478ad21c7a
@ -82,18 +82,20 @@ static int crc_check(uint8_t *buffer, int length){
|
||||
uint8_t byte;
|
||||
uint16_t crc = 0xFFFF;
|
||||
int valid_crc;
|
||||
/*
|
||||
#ifdef EBUG
|
||||
printf("buffer: ");
|
||||
for(int i = 0; i < length; ++i) printf("%02x ", buffer[i]);
|
||||
printf("\n");
|
||||
#endif
|
||||
*/
|
||||
while (length-- > 2) {
|
||||
byte = *buffer++ ^ crc;
|
||||
crc >>= 8;
|
||||
crc ^= crc16_table[byte];
|
||||
}
|
||||
valid_crc = (crc >> 8) == buffer[1] && (crc & 0xFF) == buffer[0];
|
||||
DBG("CRC %s", valid_crc ? "OK" : "bad");
|
||||
if(!valid_crc) DBG("CRC BAD");
|
||||
return valid_crc;
|
||||
}
|
||||
|
||||
@ -150,12 +152,12 @@ params_ans check_meteo_params(){
|
||||
if(portfd < 0) return ANS_LOSTCONN;
|
||||
int n_bytes = -1, res, size = 0;
|
||||
uint8_t meteoflags = 0;
|
||||
uint16_t lastpar = 0;
|
||||
static uint16_t lastpar = 0;
|
||||
unsigned char buffer[MODBUS_MAX_PACKET_SIZE];
|
||||
struct timeval timeout;
|
||||
fd_set set;
|
||||
time_t tstart = time(NULL);
|
||||
int ctr = 50; // max 50 tries
|
||||
//time_t tstart = time(NULL);
|
||||
int ctr = 30; // max 30 tries
|
||||
while(ctr--){
|
||||
FD_ZERO(&set);
|
||||
FD_SET(portfd, &set);
|
||||
@ -173,13 +175,21 @@ params_ans check_meteo_params(){
|
||||
}
|
||||
size += n_bytes;
|
||||
if(n_bytes) continue;
|
||||
}else if(size == 0){
|
||||
//DBG("no data");
|
||||
return ANS_NODATA;
|
||||
}
|
||||
DBG("Ctr=%d, size=%d, res=%d", ctr, size, res);
|
||||
// read all or end of packet
|
||||
if(size > 0 && res == 0 && (size == REQ_LEN || size == ANS_LEN)){
|
||||
if(crc_check(buffer, size)){
|
||||
if(size == REQ_LEN){
|
||||
DBG("request");
|
||||
ctr = 30;
|
||||
lastpar = buffer[2] << 8 | buffer[3];
|
||||
}else if(size == ANS_LEN){
|
||||
ctr = 30;
|
||||
DBG("answer");
|
||||
uint16_t val = buffer[3] << 8 | buffer[4];
|
||||
int prval = 1;
|
||||
float f = (float)val / 10.f;
|
||||
@ -231,19 +241,26 @@ params_ans check_meteo_params(){
|
||||
default:
|
||||
prval = 0;
|
||||
}
|
||||
if(prval) DBG("=%.1f\n", f);
|
||||
lastpar = 0;
|
||||
if(prval){
|
||||
DBG("=%.1f\n", f);
|
||||
return ANS_OK;
|
||||
}
|
||||
}
|
||||
}
|
||||
size = 0;
|
||||
}
|
||||
/*
|
||||
if(meteoflags == ALLFLAGS){
|
||||
DBG("Got all data");
|
||||
return ANS_OK;
|
||||
}
|
||||
if(time(NULL) - tstart >= METEO_TIMEOUT) return ANS_NOTFULL;
|
||||
if(time(NULL) - tstart >= METEO_TIMEOUT){
|
||||
DBG("Timeout, not all data meteoflags=0x%02X instead of 0x%02X", meteoflags, ALLFLAGS);
|
||||
return ANS_NODATA;
|
||||
}*/
|
||||
}
|
||||
return ANS_NOTFULL;
|
||||
return ANS_NODATA;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -20,12 +20,13 @@
|
||||
#define BTA_METEO_MODBUS_H__
|
||||
|
||||
// data reading timeout (in seconds)
|
||||
#define METEO_TIMEOUT 5
|
||||
#define METEO_TIMEOUT 15
|
||||
|
||||
typedef enum{
|
||||
ANS_OK, // all OK
|
||||
ANS_LOSTCONN, // lost connection
|
||||
ANS_NOTFULL // got not full dataset (or nothing at all)
|
||||
ANS_NODATA, // no data yet
|
||||
// ANS_NOTFULL // got not full dataset (or nothing at all)
|
||||
} params_ans;
|
||||
|
||||
int connect2tty();
|
||||
|
||||
@ -114,7 +114,7 @@ int main(int argc, char *argv[]){
|
||||
return 1;
|
||||
}
|
||||
params_ans a = check_meteo_params();
|
||||
DBG("chk_meteo: %d", a);
|
||||
if(a != ANS_NODATA) DBG("chk_meteo: %d", a);
|
||||
if(a == ANS_LOSTCONN){
|
||||
LOG("Lost connection with device, reconnect!");
|
||||
clear_flags();
|
||||
|
||||
@ -79,7 +79,6 @@ static void myabort(int sig) {
|
||||
print_date(stderr);
|
||||
switch (sig) {
|
||||
default:
|
||||
|
||||
case SIGHUP :
|
||||
case SIGINT :
|
||||
fprintf(stderr,"%s: %s - Ignore .....\n",myname,ss);
|
||||
@ -102,8 +101,7 @@ static void myabort(int sig) {
|
||||
static int rxpnt;
|
||||
static double rxtime;
|
||||
|
||||
int main (int argc, char *argv[])
|
||||
{
|
||||
int main (int argc, char *argv[]){
|
||||
double tcurr, tlast, tok, twndok;
|
||||
char msg[60];
|
||||
double t0=0., t;
|
||||
@ -153,17 +151,11 @@ int main (int argc, char *argv[])
|
||||
tdata[3] = 1;
|
||||
tdata[4] = 0; tdata[5] = 50; /* 0.5s */
|
||||
for(i=0; i<8; i++) {
|
||||
if(i==2||i==3||i==5||i==6) /* T-ÚÅÒËÁÌÁ, ×ÅÔÅÒ, ×ÌÁÖÎÏÓÔØ, T ÎÁ ÍÅÔÅÏ-ÍÁÞÔÅ */
|
||||
if(i==2||i==4|i==3||i==5||i==6) /* T-ÚÅÒËÁÌÁ, ×ÅÔÅÒ, ÄÁ×ÌÅÎÉÅ, ×ÌÁÖÎÏÓÔØ, T ÎÁ ÍÅÔÅÏ-ÍÁÞÔÅ */
|
||||
continue; /* ÐÏËÁ ÎÅ ÉÓÐÏÌØÚÕÀÔÓÑ (ÚÁÍÅÎÅÎÙ) */
|
||||
if(i==4) { /*×ÒÅÍÅÎÎÏ: ÐÏËÁ áãð4 ÉÄÅÔ Ó PEP-A (ÄÁ×ÌÅÎÉÅ)*/
|
||||
pep = "A";
|
||||
idt = 0x40f; /* PEP-A */
|
||||
tdata[1] = nch = 4; /* áãð/4 */
|
||||
} else {
|
||||
pep = "RK";
|
||||
idt = 0x447; /* PEP-RK */
|
||||
tdata[1] = nch = i; /* áãð/i */
|
||||
}
|
||||
print_date(stderr);
|
||||
if(can_send_frame(idt, dlen, tdata)<=0) {
|
||||
fprintf(stderr, "Can't send command \"Start ADC%d\" to PEP-%s!\n", nch,pep);
|
||||
@ -179,7 +171,6 @@ int main (int argc, char *argv[])
|
||||
print_date(stderr);
|
||||
fprintf(stderr,"PEP-RK: ADC(0,1,7) (or PEP-A ADC4) timeout!\n");
|
||||
}
|
||||
#if 0
|
||||
MeteoMode &= ~SENSOR_T2;
|
||||
idt = 0x447;
|
||||
dlen=6;
|
||||
@ -204,7 +195,6 @@ int main (int argc, char *argv[])
|
||||
fprintf(stderr,"Send command \"Stop ADC%d\" to PEP-%s.\n",nch,pep);
|
||||
fflush(stderr);
|
||||
}
|
||||
#endif
|
||||
if(stop_prog) can_exit(0);
|
||||
start=0;
|
||||
tok = tcurr;
|
||||
@ -233,26 +223,8 @@ int main (int argc, char *argv[])
|
||||
while(can_recv_frame(&rxpnt, &rxtime, &idr, &dlen, rdata)) {
|
||||
int rcode = 0;
|
||||
t = rxtime;
|
||||
if(idr==0x447||idr==0x40f) {
|
||||
pep = (idr==0x40f)? "A" : "RK";
|
||||
if(rdata[0]==7) {
|
||||
if(rdata[2] == 0 && rdata[3] == 0)
|
||||
fprintf(stderr,"%s PEP-%s: Echo command \"Stop ADC%d\"\n", time2asc(rxtime-timezone),pep,rdata[1]);
|
||||
else
|
||||
fprintf(stderr,"%s PEP-%s: Echo command \"Start ADC%d\"\n", time2asc(rxtime-timezone),pep,rdata[1]);
|
||||
} else if(rdata[0]==8) {
|
||||
fprintf(stderr,"%s PEP-%s: Echo command \"Stop all ADC\"\n", time2asc(rxtime-timezone),pep);
|
||||
start=0;
|
||||
}
|
||||
fflush(stderr);
|
||||
} else if(idr==0x20c) { /* ×ÒÅÍÅÎÎÏ: ËÏÄ áãð4 PEP-A - ÄÁ×ÌÅÎÉÅ */
|
||||
if(dlen!=3)
|
||||
goto wrong_frame;
|
||||
idr = 0x224; /* ×ÒÅÍÅÎÎÏ: ÉÍÉÔÁÃÉÑ áãð4 PEP-RK */
|
||||
goto adc_pep_rk;
|
||||
} else if((idr&0xff8)==0x220) { /* ËÏÄ ÏÔ áãð PEP-RK */
|
||||
if((idr&0xff8)==0x220) { /* ËÏÄ ÏÔ áãð PEP-RK */
|
||||
static double T2 = 0.;
|
||||
static int ctm=0;
|
||||
static int terr=0;
|
||||
int chan, code;
|
||||
double volt,T,b,w,h;
|
||||
@ -274,7 +246,6 @@ int main (int argc, char *argv[])
|
||||
code = (unsigned int)rdata[0]<<8|rdata[1];
|
||||
volt = (double)code/4096.*5.; /* ADC 12bit 0-5V */
|
||||
if(chan == 1){ /* áãð1 - T-ÐÏÄËÕÐÏÌØÎÏÇÏ */
|
||||
ctm |= 2; /* áãð1 - T-ÐÏÄËÕÐÏÌØÎÏÇÏ */
|
||||
T = zeroT2 + (volt-zeroV)*scaleT;
|
||||
/*t2=T;*/
|
||||
if(T >= -20. && T <= 30.) {
|
||||
@ -298,11 +269,7 @@ int main (int argc, char *argv[])
|
||||
//printf("Get T=%.1f\n", T2);
|
||||
tok = t;
|
||||
}
|
||||
if(chan<3 && (ctm&0x93) == 0x93) { /* áãð-0,1,4,7 - Ok */
|
||||
tok = t;
|
||||
ctm=0;
|
||||
}
|
||||
tlast=t;
|
||||
tok = tlast = t;
|
||||
}else if(idr==0x21){ /* ÐÒÉÎÑÔ ËÏÄ RK ÏÔ PEP-ËÏÎÔÒÏÌÌÅÒÁ */
|
||||
static double off_time = 0.;
|
||||
static double last_msg_time = 0.;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user