_YYYY_MM_DD_HH_MM_SS.log
sprintf(log_filename,
"/var/modbuslog/%s_%04i_%02i_%02i_%02i_%02i_%02i.log",
mac_address(), midnight.tm_year + 1900,
midnight.tm_mon + 1, midnight.tm_mday, midnight.tm_hour,
midnight.tm_min, midnight.tm_sec);
//printf("%s\n",filename);
FILE *fp = fopen(log_filename, "r");
if (fp) {
fclose(fp);
} else {
// file doesn't exist. create it.
syslog(LOG_NOTICE, "logfile does not exist");
syslog(LOG_INFO, "creating file: %s", log_filename);
FILE *fp = fopen(log_filename, "w");
fprintf(
fp,
"IntervalID|UTCDate|UTCTime|LOCALDate|LOCALTime|SensorID|RegisterID|Reading\n");
fclose(fp);
}
FILE *filehandle = fopen(log_filename, "a+");
syslog(LOG_DEBUG, "opening file for append: %s", log_filename);
int16_t registervalue = 0;
int p;
// data arrives in words, split into bytes
for (p = 0; p < numregisters; p++) {
MODBUS_SET_INT16_TO_INT8(byte, p * 2, tab_reg[p]);
//registervalue += tab_reg[i] + tab_reg[i + 1];
}
// bitshifting magic
switch (numregisters) {
case 1:
registervalue = byte[0];
break;
case 2:
registervalue = ((byte[0]) << 8) + byte[1];
break;
case 4:
registervalue = ((byte[0]) << 24) + ((byte[1]) << 16)
+ ((byte[2]) << 8) + byte[3];
break;
default:
registervalue = byte[0];
break;
}
struct tm utc = *gmtime(&unixtime_min);
struct tm lc = *localtime(&unixtime_min);
int intervalid;
char interval_filename[50];
sprintf(interval_filename,
"/var/modbuslog/interval/interval.txt");
syslog(LOG_DEBUG, "opening interval file: %s",
interval_filename);
FILE *intervalfile = fopen(interval_filename, "r+");
if (intervalfile) {
fclose(intervalfile);
} else {
// file doesn't exist. create it.
syslog(LOG_NOTICE, "interval file does not exist");
syslog(LOG_INFO, "attempting to create file: %s",
interval_filename);
intervalfile = fopen(interval_filename, "w");
fprintf(intervalfile, "0\n");
fclose(intervalfile);
}
// file now exists, try opening again
intervalfile = fopen(interval_filename, "r+");
fscanf(intervalfile, "%d", &intervalid);
//handle 32bit signed overflow
if (intervalid == 2147483647) {
intervalid = 0;
} else {
intervalid++;
}
rewind(intervalfile);
fprintf(intervalfile, "%d", intervalid);
fclose(intervalfile);
fprintf(
filehandle,
"%i|%04i%02i%02i|%02i%02i%02i|%04i%02i%02i|%02i%02i%02i|%i|%i|%i\n",
intervalid, utc.tm_year + 1900, utc.tm_mon + 1,
utc.tm_mday, utc.tm_hour, utc.tm_min, utc.tm_sec,
lc.tm_year + 1900, lc.tm_mon + 1, lc.tm_mday,
lc.tm_hour, lc.tm_min, lc.tm_sec, slaveid, startaddress,
registervalue);
fclose(filehandle);
syslog(
LOG_DEBUG,
"%i|%04i%02i%02i|%02i%02i%02i|%04i%02i%02i|%02i%02i%02i|%i|%i|%i\n",
intervalid, utc.tm_year + 1900, utc.tm_mon + 1,
utc.tm_mday, utc.tm_hour, utc.tm_min, utc.tm_sec,
lc.tm_year + 1900, lc.tm_mon + 1, lc.tm_mday,
lc.tm_hour, lc.tm_min, lc.tm_sec, slaveid, startaddress,
registervalue);
modbus_close(ctx);
modbus_free(ctx);
sleep(1);
//return 0;
}
//printf("%d ", slaveid);
}
//printf("%d\n", unixtime_min);
}
}
/td>-3/+4 |
2012-03-07 | Surround log startup notice with empty lines | Jonathan McCrohan | 1 | -0/+2 |
2012-03-07 | Add compile time version numbers | Jonathan McCrohan | 2 | -1/+10 |
2012-03-05 | Fix copypasta errorv1.1 | Jonathan McCrohan | 1 | -10/+18 |
2012-03-05 | Rearrange syslog output to read 8N1 rather than N81 | Jonathan McCrohan | 1 | -2/+2 |
2012-03-05 | Refactor code | Jonathan McCrohan | 2 | -6/+4 |
2012-03-05 | Add syslog logging | Jonathan McCrohan | 1 | -24/+57 |
2012-03-04 | Remove unused #define statements | Jonathan McCrohan | 1 | -3/+0 |
2012-02-20 | Prune archive after 3 months | Jonathan McCrohan | 3 | -0/+7 |
2012-02-20 | Update documentation | Jonathan McCrohan | 1 | -3/+5 |
2012-02-20 | Add PHP to make install | Jonathan McCrohan | 1 | -0/+3 |
2012-02-20 | Update Makefile | Jonathan McCrohan | 1 | -0/+17 |
2012-02-20 | Move interval.txt to src/ | Jonathan McCrohan | 2 | -1/+1 |
2012-02-19 | Remove debug/testing files. | Jonathan McCrohan | 50 | -4742/+0 |
2012-02-15 | Archive files after upload. | Jonathan McCrohan | 1 | -0/+2 |
2012-02-12 | Fix typo in README. | Jonathan McCrohan | 1 | -1/+1 |
2012-02-12 | Add ftp upload function + add documentation. | Jonathan McCrohan | 5 | -3/+151 |
2012-01-10 | Handle 32bit signed overflow. | Jonathan McCrohan | 1 | -1/+6 |
2012-01-10 | Update README. | Jonathan McCrohan | 1 | -1/+3 |