Home

Awesome

vserialport

This is a wrapper of libserialport written in V. vserialport consists of the main datatype Port, and Configuration, EventSet, UsbBus and UsbID as secondary datatypes.

Construct and deconstruct the serial Port

Create a Port object by calling new_port. new_port requires port_name argument as string (such as devttyUSB0 or /dev/ttyS0) and returns optionally Port object. Use or to handle the error, in case it failes to create the Port object.

mut p1 := vserialport.new_port('/dev/ttyUSB1') or {
	code := vserialport.error_code()
	msg  := vserialport.error_message()
	println('>> [${code}]: ${msg}')
	println('Failed to open:\n\t${err.msg}')
	return
}

In case the object is created, make sure to call the destructor function free() to deallocate the Port object. A safer approach is to use defer.

p1.free()

Open and close the connection with serial port

Use p1.open() and p1.close() to open the connection with serial port and to disconnect the serial port. open(mode) requires vserialport.Mode argument and returns a boolean if it is connected or not. The other way to check if the Port is connected or not, use p1.is_connected() function.

enum Mode {
	read
	write
	read_write
}

Such as:

mut rc := int(0)
if p1.open(vserialport.Mode.read_write) == false {
	code := vserialport.error_code()
	msg  := vserialport.error_message()
	println('>> [${code}]: ${msg}')
	p1.free()
}
println('p1 port is connected = ${p1.is_connected()}')

Read / write functions

libserialport supports non-blocking and blocking read/write functions. The same approach is reflected on vserialport as well. write function requires the first argument of boolean type to be set, if the write function is blocking or non-blocking write to serial port, respectively - that is, block bool: true for blocking write function, and false for non blocking write function. If it is non-blocking write function, the third argument - the timeout_ms argument is not going to be taken into consideration. It is useful only for blocking write function. The second argument is the byte array to be sent. In case you have to send string, string provides the bytes() function, which returns the byte array of that string. The write functions returns optional integer - that is, either number of bytes that are written to the serial port or the error that needs to be handled with or operator.

Example 1:

// Use non-blocking write to sent string AT+R
rc := p1.write(false, 'AT+R\r'.bytes(), 0) or {
	println('faild to write')
	return
}
println('${rc} bytes are sent.)

Example 2

// Use blocking write (and wait 1000 ms) to send this byte array [0x01, 0x31, 0x31, 0x04]
rc := p1.write(true, [byte(0x01), 0x31, 0x31, 0x04], 1000) or {
	println('faild to write')
	return
}
println('${rc} bytes are sent.)

Function prototype for read/write.

write(block bool, buffer []byte, timeout_ms u32) ?int
read(mode ReaderMode, max_length u32, timeout_ms u32) []byte

In contrast with write function, the first argument of read function, instead of bool, requires mode as enum ReaderMode which has following three options:

enum ReaderMode{
	blocking    = 0
	next        = 1
	nonblocking = 2
}

Configuration functions

Use set_baudrate(), set_bits(), set_parity(), set_stopbits(), set_rts(vserialport.Rts), set_cts(vserialport.Cts), set_dtr(vserialport.Dtr), set_dsr(vserialport.Dsr), set_xon_xoff(vserialport.XonXoff) and set_flowcontrol(vserialport.FlowControl) to configure baudrate, bits, parity, stop bits, rts, cts, dtr, dsr, xon_xoff and flowcontrol, respectively.

Some other helping functions are:

functionreturn typedescription
Port.name()stringreturns the name of serial port.
Port.description()stringreturns the description of serial port.
Port.transport()enum Transportreturns the transport type of serial port. enum Transport {native, usb, bluetooth}
Port.usb_bus()struct UsbBusreturns the usb bus and usb address.
Port.usb_id()struct UsbIdreturns the USB vendor ID and USB product ID.
Port.usb_manufacturer()stringreturns the USB manufacturer.
Port.usb_product()stringreturns the USB product.
Port.bluetooth_adress()stringreturns the bluetooth address as string.

Other functions

Function nameReturn typeDescription
Port.bytes_to_read()intReturn the number of bytes waiting in the input buffer.
Port.bytes_to_write()intReturn the number of bytes waiting in the output buffer.
Port.flush(Buffer)boolFlushes the Buffer.input, Buffer.output or Buffer.both and returns true if the selected buffer is flushed, otherwise false.
Port.drain()falseWaits for the buffered data to be transmitted.
Port.signal()enum SignalReturns the status of control signal. This depends from the control signals that the serial port is configured. This may trigger as output Signal.cts, Signal.dsr, Signal.dcd or Signal.ri. If it failes to get the control signal, error needs to be handled with or operator.
Port.start_breakboolPuts the serial port transmission line into the break state.
Port.end_breakboolTakes the serial port tranmission line out of the break state.

Other helping function from vserialport:

Function nameReturn typeDescription
error_code()intReturns the last error code.
error_message()stringReturns the last error message.
major_version()intReturns the major package version of libserialport.
minor_version()intReturns the minor package version of libserialport.
micro_version()intReturns the micro package version of libserialport.
package_version()stringReturns the full package version of libserialport

Depedencies

Bugfix

If you install the libserialport using apt-get install libserialport*, you may have to deal with following error: "Inappropriate ioctl for device". To avoid this error, make sure to install the libserialport from the github repository. More specifically, make sure that the following patch is applied (patch):

configure.ac | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/configure.ac b/configure.ac
index b1af16f..a26b851 100644
--- a/configure.ac
+++ b/configure.ac
@@ -112,7 +112,7 @@ AC_SYS_LARGEFILE
 AC_TYPE_SIZE_T
 
 # Check for specific termios structures.
-AC_CHECK_TYPES([struct termios2, struct termiox],,,
+AC_CHECK_TYPES([struct termios2],,,
 	[[#include <linux/termios.h>]])
 AC_CHECK_MEMBERS([struct termios.c_ispeed, struct termios.c_ospeed,
 		struct termios2.c_ispeed, struct termios2.c_ospeed],,,

Roadmap

WARNING

This wrapper is not yet fully validated enough to be implemented in the application industry.