Skip to content

Instantly share code, notes, and snippets.

@dbuenzli

dbuenzli/ioserial.ml

Last active Apr 12, 2019
Embed
What would you like to do?
Serial connection
(*----------------------------------------------------------------------------
Copyright (c) 2009, Daniel C. Bünzli. All rights reserved.
Distributed under a BSD license, see license at the end of the file.
----------------------------------------------------------------------------*)
let str = Printf.sprintf
let exec = Filename.basename Sys.executable_name
let pr = Format.printf
let pr_err s = Printf.eprintf "%s:%s\n" exec s
let open_device tty baud mode =
let flags = [ mode; (* Unix.O_NOCTTY ; *) Unix.O_NONBLOCK ] in
let fd = Unix.openfile tty flags 0o640 in
let attrs = Unix.tcgetattr fd in
let attrs' = { attrs with
(* Baud rate *)
Unix.c_obaud = baud; c_ibaud = baud;
(* 8N1 *)
c_csize = 8;
c_parenb = false;
c_cstopb = 1;
(* No flow control. *)
c_cread = true;
c_clocal = true;
c_ixon = false;
c_ixoff = false;
(* Raw input *)
c_icanon = false;
c_echo = false;
c_echoe = false;
c_isig = false;
(* Raw output *)
c_opost = false; }
in
Unix.tcsetattr fd Unix.TCSANOW attrs';
Unix.clear_nonblock fd;
fd
let raw_stdin () =
let attrs = Unix.tcgetattr Unix.stdin in
let attrs' = { attrs with Unix.c_icanon = false; } in
Unix.tcsetattr Unix.stdin Unix.TCSAFLUSH attrs';
Unix.stdin
let raw_io i o =
let buf = " " in
while true do if Unix.read i buf 0 1 = 1 then ignore (Unix.write o buf 0 1)
done
let io tty baud mode = match mode with
| (Unix.O_RDONLY | Unix.O_RDWR (* TODO *)) ->
let fd = open_device tty baud mode in
raw_io fd Unix.stdout
| Unix.O_WRONLY ->
let fd = open_device tty baud mode in
raw_io Unix.stdin fd
| _ -> assert false
let main () =
let usage =
str "Usage: %s <option> <tty>\n\
Sends and/or receives data to/from the device on tty.\n\
Options:" exec
in
let tty = ref None in
let baud = ref 115200 in
let mode = ref Unix.O_RDWR in
let options = [
"-b", Arg.Set_int baud,
" <int> specify the baud rate (defaults to 115200).";
"-r", Arg.Unit (fun () -> mode := Unix.O_RDONLY),
" only read data from the device.";
"-w", Arg.Unit (fun () -> mode := Unix.O_WRONLY),
" only write data to the device.";
"-rw", Arg.Unit (fun () -> mode := Unix.O_RDWR),
" read and write data from/to the device (default).";]
in
Arg.parse options (fun s -> tty := Some s) usage;
match !tty with
| None -> pr_err " no tty specified."; exit 1
| Some tty ->
Unix.handle_unix_error (io tty !baud) !mode
let () = main ()
(*----------------------------------------------------------------------------
Copyright (c) 2009, Daniel C. Bünzli
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the
distribution.
3. Neither the name of the Daniel C. Bünzli nor the names of
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
----------------------------------------------------------------------------*)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.