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