No description
Find a file
2024-01-24 11:12:21 +00:00
examples Fix #42 - check timeouts for > 0 rather than != 0 (#43) 2020-03-31 18:38:37 +01:00
src add support for NuttX RTOS. 2024-01-23 13:27:00 +09:00
.editorconfig Major refactor, improving Windows support and adding SerialStream 2017-08-15 21:02:14 +01:00
.gitattributes Code formatting with nimpretty 2019-11-27 09:44:04 +00:00
.gitignore Add support for reading from and writing to ports asynchronously (#9) 2018-06-26 20:34:14 +01:00
LICENSE Update LICENSE, remove docs from cluttering up repository 2020-01-20 22:19:20 +00:00
README.md Update LICENSE, remove docs from cluttering up repository 2020-01-20 22:19:20 +00:00
serial.nimble Updated version to 1.2.0 for added platform support 2024-01-24 11:12:21 +00:00

serial.nim

A library to work with serial ports using pure Nim.

Installation

serial can be installed using Nimble:

nimble install serial

Or add the following to your .nimble file:

# Dependencies

requires "serial >= 1.0.0"

Usage

There are some examples in the examples directory, showing reading from and writing to a serialport.

Listing serial ports

import serial # Or: `import serial/utils`

for port in listSerialPorts():
  echo port

Reading from/writing to a serial port (echoing data)

import serial # Or: `import serial/serialport`

let port = newSerialPort("COM1")
# use 9600bps, no parity, 8 data bits and 1 stop bit
port.open(9600, Parity.None, 8, StopBits.One)

# You can modify the baud rate, parity, databits, etc. after opening the port
port.baudRate = 2400

var receiveBuffer = newString(1024)
while true:
  let numReceived = port.read(receiveBuffer)
  discard port.write(receiveBuffer[0 ..< numReceived])

Using the SerialStream

import serial # Or: `import serial/serialstream`

let port = newSerialStream("COM1", 9600, Parity.None, 8, StopBits.One, buffered=true)

while true:
  # Read a line from the serial port then write it back.
  port.writeLine(port.readLine())

Features

  • Basic port reading/writing for Windows/Posix
  • Port setting control - baud rate, stop bits, databits, parity, handshaking
  • Port listing to list available serial ports
    • Windows, using SetupDiGetClassDevs
    • Mac, using I/O Kit
    • Posix, by iterating possible device files
  • High level SerialPortStream that complies with the streams API
  • Async API using asyncdispatch for reading from and writing to a port