-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathser_port.ml
54 lines (46 loc) · 1.44 KB
/
ser_port.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
type t = Unix.file_descr
exception Timeout
let dump prefix buf =
prerr_string prefix;
String.iter (fun c -> Printf.fprintf stderr " %02X" (Char.code c)) buf;
prerr_newline ()
let cfmakeraw attr =
{ attr with Unix.c_brkint = false; c_csize = 8; c_echo = false;
c_echonl = false; c_icanon = false; c_icrnl = false;
c_ignbrk = false; c_igncr = false; c_inlcr = false;
c_isig = false; c_istrip = false; c_ixon = false;
c_opost = false; c_parenb = false; c_parmrk = false }
let close = Unix.close
let open_port path =
let fd = Unix.(openfile path [O_NONBLOCK; O_RDWR] 0o640) in
Unix.(tcflush fd TCIOFLUSH);
(* TODO: Set speed *)
Unix.(tcsetattr fd TCSANOW (cfmakeraw (tcgetattr fd)));
fd
let timeout = 5.0
let read fd n =
(* TODO: use bytes *)
let buf = String.make n '\x00' in
let rec aux k =
if k < n then
match Unix.select [fd] [] [] timeout with
| [fd], [], [] ->
let m = Unix.read fd buf k (n - k) in
dump "PORT -->" (String.sub buf k m);
aux (k + m)
| _ -> raise Timeout
else
buf in
aux 0
let write fd buf =
(* TODO: use bytes *)
let n = String.length buf in
let rec aux k =
if k < n then
match Unix.select [] [fd] [] timeout with
| [], [fd], [] ->
let m = Unix.write fd buf k (n - k) in
dump "PORT <--" (String.sub buf k m);
aux (k + m)
| _ -> raise Timeout in
aux 0