package serial

  1. Overview
  2. Docs
Serial communication module

Install

Dune Dependency

Authors

Maintainers

Sources

serial-0.1.0.tbz
sha256=5034e009b14e0ba3a82b48026de13b2df3d80f37e14bd013b5dbd062f698370c
sha512=3b56e99d7dc4d12cfc3c4624c1b8bf7d5b8aea80b0a60849bd25932af52584cef529075076c0a3e9f61da5db71fd80dc56a9df4b0325f1c3c11c931462ecfbda

README.md.html

OSerial

OCaml Serial Module

Installation

opam install serial

If using dune, add serial to the libraries stanza.

Usage

open Lwt.Infix

let port = "/dev/pts/19" in
let baud_rate = 115200 in

Serial.connect ~port ~baud_rate >>= function
| Ok connection ->
	Lwt_io.printl "Awaiting input. Enter 'quit' when done..." >>= fun () ->
	Serial.io_loop connection (Some "quit")

| Error e -> Lwt_io.printlf "Error connecting: %s" (Printexc.to_string e)

See examples for more.

No serial device to play around with?
socat -d -d pty,raw,echo=0 pty,raw,echo=0

# first two lines may be something like:
# "... N PTY is /dev/pts/14"
# "... N PTY is /dev/pts/19"

# open one using Serial, the other using socat
(or two connections via this library!):

socat - /dev/pts/14,raw,echo=0

Supplied Functions

The function returns are wrapped in Lwt.t, so please read up on Lwt should you be unfamiliar with the library.

read_line : connection -> unit -> string Lwt.t
write_line : connection -> string -> unit Lwt.t
write : connection -> string -> unit Lwt.t

baud_rate : connection -> int
port : connection -> string
wait_for_line : connection -> string -> timeout_s:float option -> unit Lwt.t

open Lwt.Infix

wait_for_line connection "ok" ~timeout_s:(Some  5.) >>= function
| Received -> Lwt_io.printlf "ok received for %S" c
| TimedOut -> Lwt_io.printlf "didn't hear back in time for %S" c

Waits for a keyword with optional timeout. Passing None to timeout_s means this can wait forever.

io_loop : connection -> string option -> unit Lwt.t

Opens a two-way communication channel between stdin and the serial device.
Usage: io_loop connection (Some "quit").
If None is supplied instead, does not exit for any keyword.

val make : Connection.t -> (module T)

See below.

Usage (module-based)

Once you have a connection record, you can create module:

let module DaytimeSerial = (val Serial.make connection) 

Use the created module

DaytimeSerial.write_line "Regresaré como lo que soy: como una reina." >>= fun () ->
DaytimeSerial.io_loop (Some "Siniestra belleza")

Modules or Records?

It's up to you. The functions are 1:1 equivalent. Side by side:

MySerial.write "Enamórate de ti. De la vida. Y luego de quien tú quieras."
Serial.write connection "Enamórate de ti. De la vida. Y luego de quien tú quieras."
OCaml

Innovation. Community. Security.