Engee documentation

COM software management

This page contains all the available COM software management functions in Engee.

To work with COM software control functions in Engee. install the hardware support package as specified in article. After installation, run:

using Main.EngeeDeviceManager.Devices.COM

Next, you need to create a COM object.:

COM.Com()
COM.close(device, port::String)

Closes the COM port and releases resources.

Arguments

  • device::Main.EngeeDeviceManager.Devices.COM.ComType: COM object.

  • port::String: the name of the port.

COM.get_devices(device) → Vector{String}

Retrieves the list of available COM ports (for example, ["COM1", "COM3", "/dev/ttyUSB0"]).

Arguments

  • device::Main.EngeeDeviceManager.Devices.COM.ComType: COM object.

COM.init(device, port::String, baudrate::Int64, bytesize::String, parity::String, stopbits::String)

Sets the port configuration.

Arguments

  • device::Main.EngeeDeviceManager.Devices.COM.ComType: COM object.

  • port::String: the name of the port.

  • baudrate::Int64: speed. Standard values: 110, 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 38400, 57600, 115200, 128000, 230400, 256000, 460800, 921600.

  • bytesize::String: byte size. Acceptable string values:

    • "FIVE_BITS": 5 bits;

    • "SIX_BITS": 6 bits;

    • "SEVEN_BITS": 7 bits;

    • "EIGHT_BITS": 8 bits.

    By default — "EIGHT_BITS".

  • parity::String: parity. Acceptable string values:

    • "PARITY_NONE": without the parity bit;

    • "PARITY_EVEN": parity (even);

    • "PARITY_ODD": odd (odd);

    • "PARITY_MARK": the parity bit is always 1 (mark);

    • "PARITY_SPACE": the parity bit is always 0 (space).

  • stopbits::String: stop bits. Acceptable string values:

    • "STOPBITS_ONE": 1 stop bit;

    • "STOPBITS_ONE_POINT_FIVE": 1.5 stop bits;

    • "STOPBITS_TWO": 2 stop bits.

COM.open(device, port::String)

Opens the COM port with the specified configuration.

Arguments

  • device::Main.EngeeDeviceManager.Devices.COM.ComType: COM object.

  • port::String: the name of the port.

COM.receive(device, port::String, bytes_to_read::Int64) → Vector{UInt8}

Reads the specified number of bytes from the port.

Arguments

  • device::Main.EngeeDeviceManager.Devices.COM.ComType: COM object.

  • port::String: the name of the port.

  • bytes_to_read::Int64: the number of bytes to read.

COM.transmit(device, port::String, message::Vector{UInt8}) → Int64

Sends an array of bytes. Returns the number of bytes sent.

Arguments

  • device::Main.EngeeDeviceManager.Devices.COM.ComType: COM object.

  • port::String: the name of the port.

  • message::Vector{UInt8}: an array of bytes.

Step-by-step example of working with COM (virtual ports)

For testing without a physical device, it is convenient to use virtual COM ports via socat. Set up a pair of related pseudo terminals:

socat -d -d PTY,link=/tmp/ttyV0,b9600 PTY,link=/tmp/ttyV1,b9600 &

Now /tmp/ttyV0 and /tmp/ttyV1 — virtual «The jumper»: everything that is written to one port is read from the other.

Creating a COM object

com = COM.Com()

_ Getting a list of ports_

ports = COM.get_devices(com)
println("Доступные порты: $ports")

initialization and opening of ports

port_send = "/tmp/ttyV0"
port_recv = "/tmp/ttyV1"

# Инициализация с идентичными параметрами
COM.init(com, port_send, 9600, "EIGHT_BITS", "PARITY_NONE", "STOPBITS_ONE")
COM.init(com, port_recv, 9600, "EIGHT_BITS", "PARITY_NONE", "STOPBITS_ONE")

# Открытие портов
COM.open(com, port_send)
COM.open(com, port_recv)

_ Data exchange_

data = UInt8[]

for i in 1:20
    push!(data, i)

    # Отправка накопленных байт
    sent = COM.transmit(com, port_send, data)
    println("Отправлено $sent байт: $data")

    # Прием такого же количества байт
    received = COM.receive(com, port_recv, length(data))
    println("Получено байт: $received")

    @assert data == received
end

working with non-standard parameters

Example of speed connection 19200, 6 data bit, parity even, 2 stop bits:

COM.init(com, "/tmp/ttyV2", 19200, "SIX_BITS", "PARITY_EVEN", "STOPBITS_TWO")
COM.open(com, "/tmp/ttyV2")

data = UInt8[0x01, 0x02, 0x03]
COM.transmit(com, "/tmp/ttyV2", data)
received = COM.receive(com, "/tmp/ttyV3", length(data))

The completion of the work

COM.close(com, port_send)
COM.close(com, port_recv)
println("Порты закрыты")

Features

  • The order of work: initopentransmit/receiveclose. init it only saves the configuration, the real opening takes place in open.

  • COM.receive — blocking call: the method waits until the port buffer has accumulated bytes_to_read bytes or the internal timeout will not expire.

  • A single COM object can manage multiple ports: initialize, open and close them by name.

  • COM.transmit returns the number of bytes actually sent. In normal operation, it is equal to the length message.