helins / rxtx.clj

Serial IO based on RXTX from Clojure JVM

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

RXTX, using the serial port from Clojure JVM

Clojars Project

Cljdoc

Simple API for doing serial IO.

Based on jRxTx.

Supports a (very) wide variety of platforms.

Installation

There is a bit of a setup but nothing to worry about, it is fairly easy.

For more information about the process or if something goes wrong, go to jRxTx.

For instance, on a debian based distribution :

sudo apt install librxtx-java

When starting your application or a repl, check if the "java.library.path" property contains a path to the installed native libs. On debian, that should be '/usr/lib/jni' :

(System/getProperty "java.library.path")

If this property is not properly set, you must do it yourself.

In your project file :

:jvm-opts ["-Djava.library.path=/PATH/TO/LIBS"]

When executing your uberjar :

java -Djava.library.path=/PATH/TO/LIBS -jar your_program.jar

Usage

This is a very brief overview.

The full API is available on Cljdoc.

In short, without error handling :

(require '[helins.rxtx :as rxtx])


(with-open [port (rxtx/serial-port "/dev/ttyUSB0"
                                   {:rxtx/baud-rate 9600
                                    :rxtx/parity    :even})]

  ;; Different things can be written to a serial port besides a byte array.
  ;;
  (rxtx/write port
              "Hello ")
  (rxtx/write port
              \w)
  (rxtx/write port
              111)
  (rxtx/write port
              [114 108 100])

  ;; Reads the answer, up to 16 unsigned bytes, but with a timeout of 2000 milliseconds.
  (println :answer
           (String. (byte-array (rxtx/read port
                                           16
                                           2000)))))

License

Copyright © 2017 Adam Helinski

Licensed under the term of the Mozilla Public License 2.0, see LICENSE.

About

Serial IO based on RXTX from Clojure JVM

License:Mozilla Public License 2.0


Languages

Language:Clojure 97.6%Language:Shell 2.4%