A cross-platform library for serial port communication in OCaml, which supports both POSIX and Windows systems. It provides a synchronous and asynchronous interface using various I/O libraries.
The main motivation behind creating this project is to address the lack of a comprehensive library for managing serial port communication in different environments, as well as the lack of an intuitive API for this task. The existing OSerial library has significant limitations in terms of functionality and future development, making it unsuitable for use in modern environments.
Installation from the OPAM repository using the OPAM package manager.
$ opam install serialportYou can also get the latest version of the upstream (developer) branch using OPAM.
$ opam pin serialport.dev https://github.com/dx3mod/serialport.gitIf you are using Dune, please add the serialport library to your dependencies.
Typically, an example of usage is communication between a PC and an Arduino board or other devices via an old-school serial port.
# #require "serialport.unix";;
# let port_opts = Serialport.Port_options.make ~baud_rate:9600 ()
and port_name = "/dev/ttyUSB0" in
Serialport_unix.with_open_communication ~opts:port_opts port_name
begin fun ser_port ->
(* Get channels abstractions for high-level working with I/O without buffering. *)
let ic, oc = Serialport_unix.to_channels ser_port in
(* Wait until Arduino has been initialized. *)
Unix.sleep 2;
(* Send the message to the Arduino via the serial port. *)
Out_channel.output_string oc "Hello from PC!\n";
(* Read the response from the serial port. *)
In_channel.input_line ic
endFor research this topic you should read Serial Programming Guide for POSIX Operating Systems for Unix systems and Windows Serial Port Programming for Windows platform.
Other implementations
- outdated OCaml Serial Module m-laniakea/oserial,
- Rust serialport,
- Golang bugst/go-serial.
The project is licensed under the MIT License, which allows for all permissions. Just use it and enjoy yourself without fear. We are always open to pull requests!