osolemio / malina Goto Github PK
View Code? Open in Web Editor NEWmanagement software for uART products
License: GNU General Public License v2.0
management software for uART products
License: GNU General Public License v2.0
int read_eeprom(unsigned char *eeprom, int fd, MYSQL mysql)
{
int i;
char query[255];
tcdrain(fd);
send_command(to_read,fd,0,0xFF);
if (read_answer(fd) == 0)
{
for (i=0;i<256;i++) eeprom[i]=Buffer[i+1];
}
else
{
syslog(LOG_ERR, "Error read MAP settings"); return -1;
tcflush (fd, TCIOFLUSH);
}
send_command(to_read,fd,0x100,0xFF);
if (read_answer(fd) == 0)
{
for (i=0;i<256;i++)
eeprom[i+0x100]=Buffer[i+1];
}
else
{
syslog(LOG_ERR, "Error read MAP settings"); return -1;
tcflush (fd, TCIOFLUSH);
}
for (i=0;i<0x200;i++)
{
unsigned char cur_val;
MYSQL_RES *res;
unsigned int rows;
MYSQL_ROW row;
sprintf(query, "select value from settings where offset = %d",i);
if (mysql_query(&mysql, query) == 0)
{
res = mysql_store_result(&mysql);
if (res != NULL)
{
rows = mysql_num_rows(res);
if (rows == 0)
{ // insert
sprintf(query, "INSERT INTO settings (offset,value) VALUES (%d,%d)",i,eeprom[i]);
if (mysql_query (&mysql, query))
{
syslog(LOG_ERR,"Error adding data in MySQL. Table settings %s\n",mysql_error(&mysql));
mysql_free_result(res);
return -1;
}
}
else
{ // update
if (row = mysql_fetch_row(res))
{
cur_val = atoi(row[0]);
if (!(cur_val == eeprom[i]))
{
sprintf(query, "UPDATE settings SET value = %d where offset = %d",eeprom[i],i);
if (mysql_query (&mysql, query))
{
syslog(LOG_ERR,"Error updating data in MySQL. Table settings %s\n",mysql_error(&mysql));
mysql_free_result(res);
return -1;
}
}
}
else
{
syslog(LOG_ERR,"Error fetch row from MySQL. Table settings (%s)\n",mysql_error(&mysql));
mysql_free_result(res);
return -1;
}
}
mysql_free_result(res);
}
}
else
{
syslog(LOG_ERR,"Error read current setting for offset %d (%s)\n",i,mysql_error(&mysql));
return -1;
}
}
return 0;
}
Добрый день!
Спасибо за открытые исходники!
Вот тут
Line 878 in 54e0d8a
Возможно какая-то ошибка в индексе-смещении? Где кстати можно посмотреть детали протокола который лег в основу реализации?
#define SHM_CREATE 1
#define PORT_MAP_ALT0 "/dev/ttyUSB0"
#define PORT_MAP_ALT1 "/dev/ttyUSB1"
#define PORT_MAP_ALT2 "/dev/ttyUSB2"
#define PORT_MAP_ALT3 "/dev/ttyUSB3"
#define PORT_MAP_ALT4 "/dev/ttyUSB4"
#define BAUDRATE B19200
...
int open_port (int dev)
{
int fd;
if (dev == 1)
fd = open (PORT_MAP_ALT0, O_RDWR, O_NOCTTY);
else if (dev == 2)
fd = open (PORT_MAP_ALT1, O_RDWR, O_NOCTTY);
else if (dev == 3)
fd = open (PORT_MAP_ALT2, O_RDWR, O_NOCTTY);
else if (dev == 4)
fd = open (PORT_MAP_ALT3, O_RDWR, O_NOCTTY);
else
fd = open (PORT_MAP_ALT4, O_RDWR, O_NOCTTY);
syslog(LOG_ERR,"Opened port %d",dev-1);
if (fd < 0)
{
syslog(LOG_ERR,"Can't open serial port");
return -1;
}
struct termios port_cfg;
if (tcgetattr (fd, &port_cfg) < 0)
{
syslog(LOG_ERR, "Error getting serial config");
return -1;
}
cfmakeraw (&port_cfg); // All other settings must be after this line
port_cfg.c_cc[VMIN] = 0;
port_cfg.c_cc[VTIME] = 1;
cfsetospeed (&port_cfg, BAUDRATE);
cfsetispeed (&port_cfg, BAUDRATE);
if (tcsetattr (fd, TCSAFLUSH, &port_cfg) < 0)
{
syslog(LOG_ERR, "Error entering Daemon. Port setattr");
return -1;
}
if (!isatty (fd))
{
syslog(LOG_ERR, "Not a tty port");
return -1;
}
return fd;
return -1;
}
...
//------------------------------------looking for a MAP --------------------------------
for (i=1;i<=5;i++)
{
if (i>5) { syslog(LOG_ERR, "No MAP found. Exiting"); return -1;}
fd = open_port (i);
if (fd < 0)
{
syslog(LOG_ERR, "Error open ttyUSB%d. Trying another port....",i-1);
continue;
}
//================ READ ALL MAP SETTINGS ======================
if (read_eeprom(eeprom, fd, mysql) != 0) {
syslog(LOG_ERR, "Error read MAP settings. Trying another port...");
close(fd);
continue;
//return -1;
}
if (eeprom[0]==3)
{
syslog(LOG_ERR, "MAP found on ttyUSB%d. Starting....",i-1);
break;
}
else
{
syslog(LOG_ERR, "MAP not found on ttyUSB%d.",i-1);
close(fd);
}
}
//----------------------- signals handler ------------------------------
#define DAEMON_NAME "MPPT Reader daemon"
#define PORT_MPPT_ALT0 "/dev/ttyUSB0"
#define PORT_MPPT_ALT1 "/dev/ttyUSB1"
#define PORT_MPPT_ALT2 "/dev/ttyUSB2"
#define PORT_MPPT_ALT3 "/dev/ttyUSB3"
#define PORT_MPPT_ALT4 "/dev/ttyUSB4"
#define BAUDRATE B19200
...
int open_port (int dev)
{
int fd;
if (dev == 1)
fd = open (PORT_MPPT_ALT0, O_RDWR, O_NOCTTY);
else if (dev == 2)
fd = open (PORT_MPPT_ALT1, O_RDWR, O_NOCTTY);
else if (dev == 3)
fd = open (PORT_MPPT_ALT2, O_RDWR, O_NOCTTY);
else if (dev == 4)
fd = open (PORT_MPPT_ALT3, O_RDWR, O_NOCTTY);
else
fd = open (PORT_MPPT_ALT4, O_RDWR, O_NOCTTY);
if (fd < 0)
{
syslog(LOG_ERR,"Can't open serial port");
return -1;
}
struct termios port_cfg;
if (tcgetattr (fd, &port_cfg) < 0)
{
syslog(LOG_ERR, "Error getting serial config");
return -1;
}
cfmakeraw (&port_cfg); // All other settings must be after this line
port_cfg.c_cc[VMIN] = 0;
port_cfg.c_cc[VTIME] = 1;
cfsetospeed (&port_cfg, BAUDRATE);
cfsetispeed (&port_cfg, BAUDRATE);
if (tcsetattr (fd, TCSAFLUSH, &port_cfg) < 0)
{
syslog(LOG_ERR, "Error entering Daemon. Port setattr");
return -1;
}
if (!isatty (fd))
{
syslog(LOG_ERR, "Not a tty port");
return -1;
}
return fd;
return -1;
}
...
//-------------------------Looking for MPPT-----------------------------------
for (i=1;i<=5;i++)
{
if (i>5) { syslog(LOG_ERR, "No MPPT found. Exiting"); return -1;}
fd_mppt = open_port (i);
if (fd_mppt < 0)
{
syslog(LOG_NOTICE, "Error open ttyUSB%d. Trying another port....",i-1);
continue;
}
send_command(to_read,fd_mppt,0,0);
if (read_answer(fd_mppt) != 0)
{
syslog(LOG_ERR, "Error read eeprom cell. Trying another port...");
close(fd_mppt);
continue;
//return -1;
}
if (Buffer[1]==5)
{
syslog(LOG_ERR, "MPPT found on ttyUSB%d. Starting....",i-1);
break;
}
else
{
syslog(LOG_ERR, "MPPT not found on ttyUSB%d.",i-1);
close(fd_mppt);
}
}
//----------------------- signals handler ------------------------------
ошибки при записи в регистры с большим offset
д.б.
unsigned char char_fifo[4];
...
size_t cntrd;
unsigned char char_fi0;
unsigned char char_fi1;
unsigned char char_fi2;
unsigned char char_fi3;
unsigned char char0xFF = 0xFF;
if (write_flag==0)
{ // - if the're no pending query
cntrd = read(fd_fifo,char_fifo,4);
if (cntrd == 4)
{
char_fi0 = char_fifo[0];
char_fi1 = char_fifo[1];
char_fi2 = char_fifo[2];
char_fi3 = char_fifo[3];
if (char_fi0==char0xFF)
{
offset=char_fi2*256+char_fi1;
write_flag=1;
}
}
}
...
Далее по тексту по аналогии везде заменить char_fifo[N] на char_fiN
поправил то много наобум, ХЗ что именно помогло но так на x86_64 работает
sprintf (cache_str,
"{\"time\":\"%02d:%02d:%02d\",\"_MODE\":\"%d\",\"_Status_Char\":\"%d\",\"_Uacc\":\"%.1f\",\"_Iacc\":\"%i\",\"_PLoad\":\"%i\",\"_F_Acc_Over\":\"%d\",\"_F_Net_Over\":\"%d\",\"_UNET\":\"%i\",\"_INET\":\"%d\",\"_PNET\":\"%i\",\"_TFNET\":\"%d\",\"_ThFMAP\":\"%d\",\"_UOUTmed\":\"%i\",\"_TFNET_Limit\":\"%d\",\"_UNET_Limit\":\"%i\",\"_RSErrSis\":\"%d\",\"_RSErrJobM\":\"%d\",\"_RSErrJob\":\"%d\",\"_RSWarning\":\"%d\",\"_Temp_Grad0\":\"%d\",\"_Temp_Grad2\":\"%d\",\"_INET_16_4\":\"%.1f\",\"_IAcc_med_A_u16\":\"%.1f\",\"Temp_off\":\"%d\",\"_E_NET\":\"%lu\",\"_E_ACC\":\"%lu\",\"_E_ACC_CHARGE\":\"%lu\",\"_Inet_flag\":\"%.1f\",\"_I_acc_avg\":\"%.1f\",\"_I_mppt_avg\":\"%.1f\",\"_I2C_Err\":\"%d\",\"_Temp_Grad1\":\"%d\",\"_Relay1\":\"%d\",\"_Relay2\":\"%d\",\"_Flag_ECO\":\"%d\",\"_RSErrDop\":\"%d\",\"_flagUnet2\":\"%u\"}",
...
sprintf (query,
"INSERT INTO data VALUES (NULL,'%d-%d-%d','%d:%d:%d','%d','%d','%.1f','%i','%i','%d','%d','%i','%d','%i','%d','%d','%i','%d','%i','%d','%d','%d','%d','%d','%d','%.1f','%.1f','%d','%lu','%lu','%lu','%.1f','%.1f','%.1f','%d','%d','%d','%d','%d')",
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.