Project

General

Profile

Revision 2dde134c

ID2dde134ca53ca2a44f4bcdcd3f91ef32067ec2c9
Parent 9e26460d
Child e2a3c71f

Added by Thomas Mullins almost 9 years ago

Shorted modbus timeout

View differences:

mainbox/main.c
7 7
#include <signal.h>
8 8
#include <string.h>
9 9
#include <stdio.h>
10

  
11
#define SLEEP_MS 25
10
#include <sys/time.h>
12 11

  
13 12
static struct tool_t tools[] = {
14 13
  TOOL_DECL("Mill", 1),
......
52 51

  
53 52
int tooltron_main(const char *device, const char *server) {
54 53
  struct sigaction sigact;
55
  int i;
54
  struct timeval time_prev, time_cur;
55
  int i, diff;
56 56

  
57 57
  log_print("Serial device: %s", device);
58 58
  log_print("CRM server: http://%s/", server);
......
81 81

  
82 82
  log_print("Modbus initialized; polling tools...");
83 83

  
84
  gettimeofday(&time_prev, NULL);
85

  
84 86
  i = 0;
85 87
  while (run) {
86 88
    tool_poll(&tools[i]);
......
95 97
      cache_clear();
96 98
      clear_cache = 0;
97 99
    }
98
    usleep(SLEEP_MS * (useconds_t)1000);
100

  
101
    // slow loop period down to MB_TIMEOUT_MS
102
    gettimeofday(&time_cur, NULL);
103
    diff = (time_cur.tv_sec - time_prev.tv_sec) * 1000000
104
         + (time_cur.tv_usec - time_prev.tv_usec);
105
    diff = MB_TIMEOUT_MS * 1000 - diff;
106
    if (diff > 0) {
107
      usleep(diff);
108
    }
109
    gettimeofday(&time_prev, NULL);
110

  
99 111
    i = (i+1) % N_TOOLS;
100 112
  }
101 113
  log_print("Recieved SIGINT or SIGTERM, shutting down");
mainbox/tool.c
24 24
    return 1;
25 25
  }
26 26

  
27
  timeout.tv_sec = 0;
28
  timeout.tv_usec = MB_TIMEOUT_MS * 1000;
29
  modbus_set_response_timeout(ctx, &timeout);
30

  
27 31
  modbus_get_response_timeout(ctx, &timeout);
28 32
  log_print("Modbus response timeout is %lus %luus",
29 33
      timeout.tv_sec, timeout.tv_usec);
mainbox/tool.h
24 24
void tool_request_disable(struct tool_t *tool);
25 25
void tool_poll(struct tool_t *tool);
26 26

  
27
/* Should be less than 1000 (see modbus_set_response_timeout()). Should also be
28
 * high enough to ensure that connected tools will respond. Tools poll their own
29
 * modbus code about every 50ms. */
30
#define MB_TIMEOUT_MS 70
31

  
27 32
#endif

Also available in: Unified diff