libserial:   Portable Serial Port Access
Parity
Flow-Control
serial-ports
in-serial-ports
open-serial-port
7.7

libserial: Portable Serial Port Access

Jan Dvořák <mordae@anilinux.org>

Serial port handling for Racket utilizing "libserialport".

The libserialport itself is a minimal, cross-platform shared library written in C that is intended to take care of the OS-specific details when writing software that uses serial ports.

It must be installed externally to this Racket module, preferrably via the system package manager.

 (require libserialport) package: libserialport

A high-level interface to port enumeration, access and configuration.

syntax

Parity

Type for parity options. Equivalent to (U 'invalid 'none 'odd 'even 'mark 'space).

Type for flow control options. Equivalent to (U 'none 'xonxoff 'rtscts 'dtrdsr).

procedure

(serial-ports)  (Listof Path-String)

Produce list of system serial port paths or names.

Example:
> (serial-ports)

'("/dev/ttyUSB0")

Iterate over known serial port paths or names. Produces Path-String values.

Example:
> (for ((serial-port (in-serial-ports)))
    (printf "found ~a\n" serial-port))

found /dev/ttyUSB0

procedure

(open-serial-port path 
  [#:baudrate baudrate 
  #:bits bits 
  #:parity parity 
  #:stopbits stopbits 
  #:flowcontrol flowcontrol]) 
  
Input-Port Output-Port
  path : Path-String
  baudrate : Positive-Integer = 9600
  bits : Positive-Integer = 8
  parity : Parity = 'none
  stopbits : Natural = 1
  flowcontrol : Flow-Control = 'none
Opens (and configures) selected serial port for both reading and writing. The default options correspond to the most frequently used null-modem case typical for switches and other similar embedded devices.

Examples:
> (define-values (in out)
    (open-serial-port "/dev/ttyUSB0" #:baudrate 115200))
> (write-bytes #"x1AVx3\n" out)

7

> (let loop ()
    (define read-result (read-bytes-avail!* read-buffer port))
    (cond [(or (eof-object? read-result)
               (and (number? read-result) (not (= read-result 0))))
           (display (bytes->string/utf-8 (read-bytes read-result port)))
           (display "")]
          [else (sleep 0.1)])
    (loop))

eval: unable to replay evaluation of (let loop () (define

read-result (read-bytes-avail!* read-buffer port)) (cond

((or (eof-object? read-result) (and (number? read-result)

(not (= read-result 0)))) (display (bytes->string/utf-8

(read-bytes read-result port))) (display "")) (else (sleep

0....