serial-port

Module providing instruments to work with serial port on Windows and Posix OS. SerialPort class can enumerate available serial ports (works robust only on Windows), check available baud rates.

Example:
auto com = new SerialPort("COM1"); // ttyS0 on GNU/Linux, for instance
string test = "Hello, World!";
com.write(test.ptr);

ubyte[13] buff;
com.read(buff);
writeln(cast(string)buff);
Note:
Some points were extracted from Tango D2 library.
TODO:
Serial port tweaking, asynchronous i/o.

enum  BaudRate: uint;

Represents allowed baud rate speeds for serial port.


class  SpeedUnsupportedException: object.Exception;

Thrown when trying to setup serial port with unsupported baud rate by current OS.


class  InvalidParametersException: object.Exception;

Thrown when setting up new serial port parameters has failed.


class  InvalidDeviceException: object.Exception;

Thrown when tried to open invalid serial port file.


class  DeviceClosedException: object.Exception;

Thrown when trying to accept closed device.


class  TimeoutException: object.Exception;

Thrown when read/write operations failed due timeout.

Exception carries useful info about number of read/wrote bytes until timeout occurs. It takes sense only for Windows, as posix version won't start reading until the port is empty.


this(string port, size_t size, string file = __FILE__, size_t line = __LINE__);

Transfers problematic port name and size of read/wrote bytes until timeout.


class  DeviceReadException: object.Exception;

Thrown when reading from serial port is failed.


class  DeviceWriteException: object.Exception;

Thrown when writing to serial port is failed.


class  SerialPort;

Main class encapsulating platform dependent files handles and algorithms to work with serial port.

You can open serial port only once, after calling close any nonstatic method will throw DeviceClosedException.

Note:
Serial port enumerating is robust only on Windows, due other platform doesn't strictly bound serial port names.

this(string port);

Creates new serial port instance.

Parameters
string port Port name. On Posix, it should be reffer to device file like /dev/ttyS. On Windows, port name should be like COM or any other.
Throws
InvalidParametersException, InvalidDeviceException

this(string port, Duration readTimeout, Duration writeTimeout);

Creates new serial port instance.

Parameters
string port Port name. On Posix, it should be reffer to device file like /dev/ttyS. On Windows, port name should be like COM or any other.
Duration readTimeout Setups constant timeout on read operations.
Duration writeTimeout Setups constant timeout on write operations. In posix is ignored.
Throws
InvalidParametersException, InvalidDeviceException

this(string port, Duration readTimeoutMult, Duration readTimeoutConst, Duration writeTimeoutMult, Duration writeTimeoutConst);

Creates new serial port instance.

Parameters
string port Port name. On Posix, it should be reffer to device file like /dev/ttyS. On Windows, port name should be like COM or any other.
Duration readTimeoutConst Setups constant timeout on read operations.
Duration writeTimeoutConst Setups constant timeout on write operations. In posix is ignored.
Duration readTimeoutMult Setups timeout on read operations depending on buffer size.
Duration writeTimeoutMult Setups timeout on write operations depending on buffer size. In posix is ignored.
Note:
Total timeout is calculated as timeoutMult*buff.length + timeoutConst.
Throws
InvalidParametersException, InvalidDeviceException

string  toString();

Converts serial port to it port name.

Example:
"ttyS0", "ttyS1", "COM1", "CNDA1".

@property SerialPort  speed(BaudRate  speed);

Set the baud rate for this serial port. Speed values are usually restricted to be 1200 * i ^ 2.

Note:
that for Posix, the specification only mandates speeds up to 38400, excluding speeds such as 7200, 14400 and 28800. Most Posix systems have chosen to support at least higher speeds though.
Throws
SpeedUnsupportedException if  speed is unsupported by current system.

@property BaudRate  speed();

Returns current port  speed. Can return BR_UNKNONW baud rate if  speed was changed not by  speed property or wreid errors are occured.


BaudRate[]  getBaudRates();

Iterates over all bauds rate and tries to setup port with it.

Returns
array of successfully setuped baud rates for current serial port.

static string[]  ports();

Tries to enumerate all serial  ports. While this usually works on Windows, it's more problematic on other OS. Posix provides no way to list serial  ports, and the only option is searching through "/dev".

Because there's no naming standard for the device files, this method must be ported for each OS. This method is also unreliable because the user could have created invalid device files, or deleted them.

Returns
A string array of all the serial  ports that could be found, in alphabetical order. Every string is formatted as a valid argument to the constructor, but the port may not be accessible.

void  close();

Closing underlying serial port. You shouldn't use port after it closing.


@property bool  closed();

Returns true if serial port was  closed.


void  write(const(void[]) arr);

Writes down array of bytes to serial port.

Throws
TimeoutException (Windows only)

size_t  read(void[] arr);

Fills up provided array with bytes from com port.

Returns
actual number of readed bytes.
Throws
DeviceReadException, TimeoutException