Project

General

Profile

Statistics
| Branch: | Revision:

root / mainbox / main.c @ 8f961e44

History | View | Annotate | Download (1.9 KB)

1 7bdb98c5 Tom Mullins
#include "tool.h"
2 8f961e44 Tom Mullins
#include "query.h"
3 7bdb98c5 Tom Mullins
#include <unistd.h>
4
#include <signal.h>
5
#include <strings.h>
6
#include <stdio.h>
7
8
#define SLEEP_MS 250
9
10
struct tool_t tools[] = {
11
  {4, "test", TS_INIT}
12
};
13
14
#define N_TOOLS (sizeof(tools)/sizeof(struct tool_t))
15
16
volatile int run = 1;
17
18
void sigint(int sig) {
19
  run = 0;
20
}
21
22 8f961e44 Tom Mullins
void print_usage(const char *name) {
23
  printf("Usage: %s [-h] [-d serial_device] [-s db_server[:port]]\n", name);
24
  printf("       -h prints this message\n");
25
  printf("       -d specifies the serial port for Modbus\n");
26
  printf("          defaults to /dev/ttyUSB0\n");
27
  printf("       -s specifies the server where the CRM is running\n");
28
  printf("          defaults to minecraft.roboclub.org:8000\n");
29
}
30
31 7bdb98c5 Tom Mullins
int main(int argc, char **argv) {
32 8f961e44 Tom Mullins
  int i, opt;
33 7bdb98c5 Tom Mullins
  struct sigaction sigact;
34
  const char *device = "/dev/ttyUSB0";
35 8f961e44 Tom Mullins
  const char *server = "minecraft.roboclub.org:8000";
36
37
  while ((opt = getopt(argc, argv, "hd:s:")) != -1) {
38
    switch (opt) {
39
      case 'h':
40
        print_usage(argv[0]);
41
        return 0;
42
      case 'd':
43
        device = optarg;
44
        break;
45
      case 's':
46
        server = optarg;
47
        break;
48
      default:
49
        print_usage(argv[0]);
50
        return 1;
51
    }
52
  }
53 7bdb98c5 Tom Mullins
54 8f961e44 Tom Mullins
  printf("Serial device: %s\n", device);
55
  printf("CRM server: http://%s/\n", server);
56 7bdb98c5 Tom Mullins
57
  bzero(&sigact, sizeof(sigact));
58
  sigact.sa_handler = sigint;
59
  sigact.sa_flags = SA_RESTART;
60
  sigemptyset(&sigact.sa_mask);
61
  sigaction(SIGINT, &sigact, NULL);
62
63 8f961e44 Tom Mullins
  if (query_init(server)) {
64
    return 1;
65
  }
66
67 7bdb98c5 Tom Mullins
  if (tool_init_mb(device)) {
68
    return 1;
69
  }
70
71
  printf("Modbus initialized; polling tools...\n");
72
73
  i = 0;
74
  while (run) {
75
    tool_poll(&tools[i]);
76
    usleep(SLEEP_MS * (useconds_t)1000);
77
    i = (i+1) % N_TOOLS;
78
  }
79
80
  printf("\nDisabling tools\n");
81
  for (i = 0; i < N_TOOLS; i++) {
82
    tool_request_disable(&tools[i]);
83
  }
84
85
  printf("Closing modbus connection\n");
86
  tool_close_mb();
87
88
  printf("Exiting\n");
89
  return 0;
90
}