Hi there!
I’m happy to announce the first release of the serialport library. The library is planned to be a cross-platform library for serial port communication in OCaml, which supports both POSIX and Windows systems. It provides synchronous and asynchronous interfaces using various I/O libraries (like Lwt and other).
The library currently only supports POSIX systems.
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.
The serial port library is most inspired by similar implementations in other languages, such as Rust’s serialport and Golang’s bugst/go-serial.
Usage
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";;
(* #require "serialport.lwt";; *)
# 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
end
Enjoy it!
Windows supports
I will be implementing Windows support in the next version (coming soon).
P.S.
I would be delighted to discuss your ideas and suggestion!