aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJonathan McCrohan <jmccrohan@gmail.com>2011-10-24 01:30:23 +0100
committerJonathan McCrohan <jmccrohan@gmail.com>2011-10-24 01:30:23 +0100
commit8adfff8b2419a0f79e85ba6884e507e787a313da (patch)
tree3e360dd9b0c2d238520b16718218045df4872bf6
parent2643a5dc67dc6945ce8e0a3962818b16a240f0b5 (diff)
downloadverteco-8adfff8b2419a0f79e85ba6884e507e787a313da.tar.gz
Timer working. MAC address in string variable.
-rw-r--r--alarm.c29
-rw-r--r--cpurelax.c16
-rw-r--r--gethwaddr.c23
-rw-r--r--modbuslog.c61
-rw-r--r--select.c25
5 files changed, 147 insertions, 7 deletions
diff --git a/alarm.c b/alarm.c
new file mode 100644
index 0000000..a0bfa35
--- /dev/null
+++ b/alarm.c
@@ -0,0 +1,29 @@
+#include <signal.h>
+#include <stdio.h>
+#include <sys/select.h>
+#include <unistd.h>
+#include <time.h>
+
+volatile unsigned int variable = 0;
+volatile unsigned int print_variable = 0;
+
+void alarm_handler(int signum)
+{
+ alarm(60);
+}
+
+int main()
+{
+ signal(SIGALRM, alarm_handler);
+ alarm(1);
+
+ for (;;)
+ {
+ select(0, NULL, NULL, NULL, NULL);
+ time_t t = time(NULL);
+ struct tm tm = *localtime(&t);
+ printf("%d-%d-%d %d:%d:%d\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
+
+ }
+}
+
diff --git a/cpurelax.c b/cpurelax.c
new file mode 100644
index 0000000..f2eacf0
--- /dev/null
+++ b/cpurelax.c
@@ -0,0 +1,16 @@
+#include <stdio.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <time.h>
+
+int main(int argc, char *argv[]) {
+
+while(1){
+cpu_relax();
+
+}
+
+ return 0;
+}
diff --git a/gethwaddr.c b/gethwaddr.c
new file mode 100644
index 0000000..2ca2fbb
--- /dev/null
+++ b/gethwaddr.c
@@ -0,0 +1,23 @@
+#include <stdio.h>
+#include <sys/ioctl.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <net/if.h>
+#include <string.h>
+
+int main( int argc, char *argv[] )
+{
+ int s;
+ struct ifreq ifr;
+
+ s = socket(PF_INET, SOCK_DGRAM, 0);
+ memset(&ifr, 0x00, sizeof(ifr));
+ strcpy(ifr.ifr_name, "eth0");
+ ioctl(s, SIOCGIFHWADDR, &ifr);
+ close(s);
+
+ unsigned char mac_address[6];
+ sprintf(mac_address,"%.2X%.2X%.2X%.2X%.2X%.2X", (unsigned char) ifr.ifr_hwaddr.sa_data[0], (unsigned char) ifr.ifr_hwaddr.sa_data[1], (unsigned char) ifr.ifr_hwaddr.sa_data[2], (unsigned char) ifr.ifr_hwaddr.sa_data[3], (unsigned char) ifr.ifr_hwaddr.sa_data[4], (unsigned char) ifr.ifr_hwaddr.sa_data[5]);
+ printf("%s", mac_address);
+
+}
diff --git a/modbuslog.c b/modbuslog.c
index 0649215..ec0acc4 100644
--- a/modbuslog.c
+++ b/modbuslog.c
@@ -24,6 +24,10 @@
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
+#include <time.h>
+#include <unistd.h>
+#include <sys/select.h>
+#include <signal.h>
#include <libconfig.h>
#include <modbus.h>
@@ -33,10 +37,18 @@
#define NUMBER_REGISTERS 14
#define DEBUG 1
+void minute_check(int signum){
+ alarm(60);
+}
+
+
+
+
+
int main(int argc, char *argv[])
{
-const char *CONFIG_FILE = "modbuslog.cfg";
+ const char *CONFIG_FILE = "modbuslog.cfg";
config_t cfg;
//config_setting_t *setting;
@@ -59,18 +71,13 @@ const char *CONFIG_FILE = "modbuslog.cfg";
return -1;
}
- modbus_t *ctx;
- uint16_t tab_reg[64];
- int rc;
- int i;
-
if(!(config_lookup_string(&cfg, "modbus.device", &modbus_device_address)
&& config_lookup_int(&cfg, "modbus.baud", &modbus_baud_rate)
&& config_lookup_int(&cfg, "modbus.data_bits", &modbus_data_bits)
&& config_lookup_string(&cfg, "modbus.parity", &modbus_parity)
&& config_lookup_int(&cfg, "modbus.stop_bits", &modbus_stop_bits))
){
- fprintf(stderr, "Check modbus configuration in configuration file.\n");
+ fprintf(stderr, "Incomplete modbus configuration. Check configuration file.\n");
return -1;
}
@@ -78,6 +85,44 @@ const char *CONFIG_FILE = "modbuslog.cfg";
printf("%s %d %c %d %d\n",modbus_device_address, modbus_baud_rate, modbus_parity[0], modbus_data_bits, modbus_stop_bits);
}
+
+ modbus_t *ctx;
+ uint16_t tab_reg[64];
+ int rc;
+ int i;
+
+
+ signal(SIGALRM, minute_check);
+ alarm(60);
+
+ for (;;){
+ select(0, NULL, NULL, NULL, NULL);
+
+ time_t t = time(NULL);
+ //int unixtime_min = (((int) t)/60*60);
+ int unixtime = (int) t;
+ printf("%d\n", unixtime);
+
+ config_setting_t *registers;
+
+ registers = config_lookup(&cfg, "register");
+
+ unsigned int num_registers = config_setting_length(registers);
+
+ int i;
+ for(i = 0; i < num_registers; ++i){
+ config_setting_t *register_element = config_setting_get_elem(registers, i);
+ int slaveid;
+ config_setting_lookup_int(register_element, "slaveid", &slaveid);
+ printf("%d ", slaveid);
+ }
+ printf("\n");
+ }
+}
+
+
+/*
+
ctx = modbus_new_rtu(modbus_device_address, modbus_baud_rate, modbus_parity[0], modbus_data_bits, modbus_stop_bits);
modbus_set_slave(ctx, SLAVE_ID);
@@ -114,3 +159,5 @@ modbus_free(ctx);
return 0;
}
+
+*/
diff --git a/select.c b/select.c
new file mode 100644
index 0000000..47351f9
--- /dev/null
+++ b/select.c
@@ -0,0 +1,25 @@
+#include <errno.h>
+#include <stdio.h>
+#include <sys/select.h>
+
+volatile unsigned int variable = 0;
+
+int main()
+{
+ struct timeval tv;
+ int val;
+
+ for (;;)
+ {
+ tv.tv_sec = 1;
+ tv.tv_usec = 0;
+
+ do
+ {
+ val = select(0, NULL, NULL, NULL, &tv);
+ } while (val != 0 && errno == EINTR);
+
+ printf("Variable = %u\n", ++variable);
+ }
+}
+