package serialport
Install
dune-project
Dependency
Authors
Maintainers
Sources
md5=75d4e9843bd11717f060a8feca148424
sha512=3fa68b03598b1fb63f2891a0cc6e5a35cabbafd4c454d7ba1d95cb880a1eb5d8684404cc558a419f4f7aff0119b41a10fb6ffde3242edde432ea91a5e429af19
Description
A cross-platform serial port library for OCaml
README
serialport
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
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.
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";;
# 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
endReferences
For 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.
License
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!