mirror of
https://github.com/eddyem/BTA_utils.git
synced 2025-12-06 10:45:14 +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;
|
uint8_t byte;
|
||||||
uint16_t crc = 0xFFFF;
|
uint16_t crc = 0xFFFF;
|
||||||
int valid_crc;
|
int valid_crc;
|
||||||
|
/*
|
||||||
#ifdef EBUG
|
#ifdef EBUG
|
||||||
printf("buffer: ");
|
printf("buffer: ");
|
||||||
for(int i = 0; i < length; ++i) printf("%02x ", buffer[i]);
|
for(int i = 0; i < length; ++i) printf("%02x ", buffer[i]);
|
||||||
printf("\n");
|
printf("\n");
|
||||||
#endif
|
#endif
|
||||||
|
*/
|
||||||
while (length-- > 2) {
|
while (length-- > 2) {
|
||||||
byte = *buffer++ ^ crc;
|
byte = *buffer++ ^ crc;
|
||||||
crc >>= 8;
|
crc >>= 8;
|
||||||
crc ^= crc16_table[byte];
|
crc ^= crc16_table[byte];
|
||||||
}
|
}
|
||||||
valid_crc = (crc >> 8) == buffer[1] && (crc & 0xFF) == buffer[0];
|
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;
|
return valid_crc;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -150,12 +152,12 @@ params_ans check_meteo_params(){
|
|||||||
if(portfd < 0) return ANS_LOSTCONN;
|
if(portfd < 0) return ANS_LOSTCONN;
|
||||||
int n_bytes = -1, res, size = 0;
|
int n_bytes = -1, res, size = 0;
|
||||||
uint8_t meteoflags = 0;
|
uint8_t meteoflags = 0;
|
||||||
uint16_t lastpar = 0;
|
static uint16_t lastpar = 0;
|
||||||
unsigned char buffer[MODBUS_MAX_PACKET_SIZE];
|
unsigned char buffer[MODBUS_MAX_PACKET_SIZE];
|
||||||
struct timeval timeout;
|
struct timeval timeout;
|
||||||
fd_set set;
|
fd_set set;
|
||||||
time_t tstart = time(NULL);
|
//time_t tstart = time(NULL);
|
||||||
int ctr = 50; // max 50 tries
|
int ctr = 30; // max 30 tries
|
||||||
while(ctr--){
|
while(ctr--){
|
||||||
FD_ZERO(&set);
|
FD_ZERO(&set);
|
||||||
FD_SET(portfd, &set);
|
FD_SET(portfd, &set);
|
||||||
@ -173,13 +175,21 @@ params_ans check_meteo_params(){
|
|||||||
}
|
}
|
||||||
size += n_bytes;
|
size += n_bytes;
|
||||||
if(n_bytes) continue;
|
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
|
// read all or end of packet
|
||||||
if(size > 0 && res == 0 && (size == REQ_LEN || size == ANS_LEN)){
|
if(size > 0 && res == 0 && (size == REQ_LEN || size == ANS_LEN)){
|
||||||
if(crc_check(buffer, size)){
|
if(crc_check(buffer, size)){
|
||||||
if(size == REQ_LEN){
|
if(size == REQ_LEN){
|
||||||
|
DBG("request");
|
||||||
|
ctr = 30;
|
||||||
lastpar = buffer[2] << 8 | buffer[3];
|
lastpar = buffer[2] << 8 | buffer[3];
|
||||||
}else if(size == ANS_LEN){
|
}else if(size == ANS_LEN){
|
||||||
|
ctr = 30;
|
||||||
|
DBG("answer");
|
||||||
uint16_t val = buffer[3] << 8 | buffer[4];
|
uint16_t val = buffer[3] << 8 | buffer[4];
|
||||||
int prval = 1;
|
int prval = 1;
|
||||||
float f = (float)val / 10.f;
|
float f = (float)val / 10.f;
|
||||||
@ -231,19 +241,26 @@ params_ans check_meteo_params(){
|
|||||||
default:
|
default:
|
||||||
prval = 0;
|
prval = 0;
|
||||||
}
|
}
|
||||||
if(prval) DBG("=%.1f\n", f);
|
|
||||||
lastpar = 0;
|
lastpar = 0;
|
||||||
|
if(prval){
|
||||||
|
DBG("=%.1f\n", f);
|
||||||
|
return ANS_OK;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
size = 0;
|
size = 0;
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
if(meteoflags == ALLFLAGS){
|
if(meteoflags == ALLFLAGS){
|
||||||
DBG("Got all data");
|
DBG("Got all data");
|
||||||
return ANS_OK;
|
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__
|
#define BTA_METEO_MODBUS_H__
|
||||||
|
|
||||||
// data reading timeout (in seconds)
|
// data reading timeout (in seconds)
|
||||||
#define METEO_TIMEOUT 5
|
#define METEO_TIMEOUT 15
|
||||||
|
|
||||||
typedef enum{
|
typedef enum{
|
||||||
ANS_OK, // all OK
|
ANS_OK, // all OK
|
||||||
ANS_LOSTCONN, // lost connection
|
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;
|
} params_ans;
|
||||||
|
|
||||||
int connect2tty();
|
int connect2tty();
|
||||||
|
|||||||
@ -114,7 +114,7 @@ int main(int argc, char *argv[]){
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
params_ans a = check_meteo_params();
|
params_ans a = check_meteo_params();
|
||||||
DBG("chk_meteo: %d", a);
|
if(a != ANS_NODATA) DBG("chk_meteo: %d", a);
|
||||||
if(a == ANS_LOSTCONN){
|
if(a == ANS_LOSTCONN){
|
||||||
LOG("Lost connection with device, reconnect!");
|
LOG("Lost connection with device, reconnect!");
|
||||||
clear_flags();
|
clear_flags();
|
||||||
|
|||||||
@ -79,7 +79,6 @@ static void myabort(int sig) {
|
|||||||
print_date(stderr);
|
print_date(stderr);
|
||||||
switch (sig) {
|
switch (sig) {
|
||||||
default:
|
default:
|
||||||
|
|
||||||
case SIGHUP :
|
case SIGHUP :
|
||||||
case SIGINT :
|
case SIGINT :
|
||||||
fprintf(stderr,"%s: %s - Ignore .....\n",myname,ss);
|
fprintf(stderr,"%s: %s - Ignore .....\n",myname,ss);
|
||||||
@ -102,8 +101,7 @@ static void myabort(int sig) {
|
|||||||
static int rxpnt;
|
static int rxpnt;
|
||||||
static double rxtime;
|
static double rxtime;
|
||||||
|
|
||||||
int main (int argc, char *argv[])
|
int main (int argc, char *argv[]){
|
||||||
{
|
|
||||||
double tcurr, tlast, tok, twndok;
|
double tcurr, tlast, tok, twndok;
|
||||||
char msg[60];
|
char msg[60];
|
||||||
double t0=0., t;
|
double t0=0., t;
|
||||||
@ -139,12 +137,12 @@ int main (int argc, char *argv[])
|
|||||||
t0 = tcurr = tlast = tok = twndok = dtime();
|
t0 = tcurr = tlast = tok = twndok = dtime();
|
||||||
tok -= 600.;
|
tok -= 600.;
|
||||||
|
|
||||||
while (1) {
|
while (1){
|
||||||
char *pep = "RK";
|
char *pep = "RK";
|
||||||
int nch = 0;
|
int nch = 0;
|
||||||
dsleep(0.2);
|
dsleep(0.2);
|
||||||
tcurr = dtime();
|
tcurr = dtime();
|
||||||
if(PEP_A_On && PEP_R_On) {
|
if(PEP_A_On && PEP_R_On){
|
||||||
if(!stop_prog && !start && tcurr-tok>15.) {
|
if(!stop_prog && !start && tcurr-tok>15.) {
|
||||||
idt = 0x447;
|
idt = 0x447;
|
||||||
dlen=6;
|
dlen=6;
|
||||||
@ -153,22 +151,16 @@ int main (int argc, char *argv[])
|
|||||||
tdata[3] = 1;
|
tdata[3] = 1;
|
||||||
tdata[4] = 0; tdata[5] = 50; /* 0.5s */
|
tdata[4] = 0; tdata[5] = 50; /* 0.5s */
|
||||||
for(i=0; i<8; i++) {
|
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; /* ÐÏËÁ ÎÅ ÉÓÐÏÌØÚÕÀÔÓÑ (ÚÁÍÅÎÅÎÙ) */
|
continue; /* ÐÏËÁ ÎÅ ÉÓÐÏÌØÚÕÀÔÓÑ (ÚÁÍÅÎÅÎÙ) */
|
||||||
if(i==4) { /*×ÒÅÍÅÎÎÏ: ÐÏËÁ áãð4 ÉÄÅÔ Ó PEP-A (ÄÁ×ÌÅÎÉÅ)*/
|
|
||||||
pep = "A";
|
|
||||||
idt = 0x40f; /* PEP-A */
|
|
||||||
tdata[1] = nch = 4; /* áãð/4 */
|
|
||||||
} else {
|
|
||||||
pep = "RK";
|
pep = "RK";
|
||||||
idt = 0x447; /* PEP-RK */
|
idt = 0x447; /* PEP-RK */
|
||||||
tdata[1] = nch = i; /* áãð/i */
|
tdata[1] = nch = i; /* áãð/i */
|
||||||
}
|
|
||||||
print_date(stderr);
|
print_date(stderr);
|
||||||
if(can_send_frame(idt, dlen, tdata)<=0) {
|
if(can_send_frame(idt, dlen, tdata)<=0) {
|
||||||
fprintf(stderr,"Can't send command \"Start ADC%d\" to PEP-%s!\n",nch,pep);
|
fprintf(stderr, "Can't send command \"Start ADC%d\" to PEP-%s!\n", nch,pep);
|
||||||
} else if(tcurr-tlast<70.)
|
} else if(tcurr-tlast<70.)
|
||||||
fprintf(stderr,"Send command \"Start ADC%d\" to PEP-%s.\n",nch,pep);
|
fprintf(stderr, "Send command \"Start ADC%d\" to PEP-%s.\n", nch,pep);
|
||||||
fflush(stderr);
|
fflush(stderr);
|
||||||
}
|
}
|
||||||
start=1;
|
start=1;
|
||||||
@ -179,7 +171,6 @@ int main (int argc, char *argv[])
|
|||||||
print_date(stderr);
|
print_date(stderr);
|
||||||
fprintf(stderr,"PEP-RK: ADC(0,1,7) (or PEP-A ADC4) timeout!\n");
|
fprintf(stderr,"PEP-RK: ADC(0,1,7) (or PEP-A ADC4) timeout!\n");
|
||||||
}
|
}
|
||||||
#if 0
|
|
||||||
MeteoMode &= ~SENSOR_T2;
|
MeteoMode &= ~SENSOR_T2;
|
||||||
idt = 0x447;
|
idt = 0x447;
|
||||||
dlen=6;
|
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);
|
fprintf(stderr,"Send command \"Stop ADC%d\" to PEP-%s.\n",nch,pep);
|
||||||
fflush(stderr);
|
fflush(stderr);
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
if(stop_prog) can_exit(0);
|
if(stop_prog) can_exit(0);
|
||||||
start=0;
|
start=0;
|
||||||
tok = tcurr;
|
tok = tcurr;
|
||||||
@ -233,33 +223,15 @@ int main (int argc, char *argv[])
|
|||||||
while(can_recv_frame(&rxpnt, &rxtime, &idr, &dlen, rdata)) {
|
while(can_recv_frame(&rxpnt, &rxtime, &idr, &dlen, rdata)) {
|
||||||
int rcode = 0;
|
int rcode = 0;
|
||||||
t = rxtime;
|
t = rxtime;
|
||||||
if(idr==0x447||idr==0x40f) {
|
if((idr&0xff8)==0x220) { /* ËÏÄ ÏÔ áãð PEP-RK */
|
||||||
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 */
|
|
||||||
static double T2 = 0.;
|
static double T2 = 0.;
|
||||||
static int ctm=0;
|
|
||||||
static int terr=0;
|
static int terr=0;
|
||||||
int chan, code;
|
int chan, code;
|
||||||
double volt,T,b,w,h;
|
double volt,T,b,w,h;
|
||||||
adc_pep_rk:
|
adc_pep_rk:
|
||||||
if(dlen!=3) {
|
if(dlen!=3) {
|
||||||
static double last_print = 0.;
|
static double last_print = 0.;
|
||||||
wrong_frame:
|
wrong_frame:
|
||||||
if(tcurr-last_print > 60.) {
|
if(tcurr-last_print > 60.) {
|
||||||
print_date(stderr);
|
print_date(stderr);
|
||||||
fprintf(stderr,"Wrong CAN-frame id=0x%x len=%d < ",idr,dlen);
|
fprintf(stderr,"Wrong CAN-frame id=0x%x len=%d < ",idr,dlen);
|
||||||
@ -274,7 +246,6 @@ int main (int argc, char *argv[])
|
|||||||
code = (unsigned int)rdata[0]<<8|rdata[1];
|
code = (unsigned int)rdata[0]<<8|rdata[1];
|
||||||
volt = (double)code/4096.*5.; /* ADC 12bit 0-5V */
|
volt = (double)code/4096.*5.; /* ADC 12bit 0-5V */
|
||||||
if(chan == 1){ /* áãð1 - T-ÐÏÄËÕÐÏÌØÎÏÇÏ */
|
if(chan == 1){ /* áãð1 - T-ÐÏÄËÕÐÏÌØÎÏÇÏ */
|
||||||
ctm |= 2; /* áãð1 - T-ÐÏÄËÕÐÏÌØÎÏÇÏ */
|
|
||||||
T = zeroT2 + (volt-zeroV)*scaleT;
|
T = zeroT2 + (volt-zeroV)*scaleT;
|
||||||
/*t2=T;*/
|
/*t2=T;*/
|
||||||
if(T >= -20. && T <= 30.) {
|
if(T >= -20. && T <= 30.) {
|
||||||
@ -295,14 +266,10 @@ int main (int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
if((MeteoMode & INPUT_T2)== 0 && (MeteoMode & SENSOR_T2))
|
if((MeteoMode & INPUT_T2)== 0 && (MeteoMode & SENSOR_T2))
|
||||||
val_T2 = T2;
|
val_T2 = T2;
|
||||||
//printf("Get T=%.1f\n", T2);
|
//printf("Get T=%.1f\n", T2);
|
||||||
tok = t;
|
tok = t;
|
||||||
}
|
}
|
||||||
if(chan<3 && (ctm&0x93) == 0x93) { /* áãð-0,1,4,7 - Ok */
|
tok = tlast = t;
|
||||||
tok = t;
|
|
||||||
ctm=0;
|
|
||||||
}
|
|
||||||
tlast=t;
|
|
||||||
}else if(idr==0x21){ /* ÐÒÉÎÑÔ ËÏÄ RK ÏÔ PEP-ËÏÎÔÒÏÌÌÅÒÁ */
|
}else if(idr==0x21){ /* ÐÒÉÎÑÔ ËÏÄ RK ÏÔ PEP-ËÏÎÔÒÏÌÌÅÒÁ */
|
||||||
static double off_time = 0.;
|
static double off_time = 0.;
|
||||||
static double last_msg_time = 0.;
|
static double last_msg_time = 0.;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user