chiark / gitweb /
1075de9f9d5fd7e0bd69713c0b2cbb502f271228
[trains.git] / hostside / serialio.c
1 /*
2  * general serial i/o and system interface etc.
3  */
4
5 #include <unistd.h>
6 #include <assert.h>
7 #include <errno.h>
8
9 #include <fcntl.h>
10
11 #include "common.h"
12
13 int serial_fudge_delay= 0;
14 int serial_fd= -1;
15
16 void serial_open(const char *device) {
17   assert(serial_fd==-1);
18
19   serial_fd= open(device,O_RDWR);
20   if (serial_fd<0) diee(device);
21 }
22
23 void serial_transmit_now(const Byte *command, int length) {
24   int r;
25   assert(length <= COMMAND_ENCODED_MAX);
26
27   while (length > 0) {
28     r= write(serial_fd, command,
29              serial_fudge_delay ? 1 : length);
30     if (r==-1) {
31       if (errno == EINTR) continue;
32       diee("command_transmit");
33     }
34     assert(r<=length);
35     command += r;
36     length -= r;
37     if (r>0 && serial_fudge_delay)
38       usleep(serial_fudge_delay);
39   }
40 }