Custom protocol

A protocol is a plugin just like a driver (a DLL in the Protocols folder). It inherits from the generic ProtocolBase<T> class, where T is the type of data the driver hands to the protocol — e.g. string for text lines from the TCP driver or CanFrame for CAN frames. It is this T that makes a driver and a protocol fit together — Project Configuration matches it against the driver's ITransportSource<T> declaration and greys out protocols the selected driver cannot feed. The protocol declares nothing extra: the ProtocolBase<T> base is enough.

Protocol skeleton

public class MyProtocol : ProtocolBase<string>
{
    public MyProtocol()   // a parameterless constructor is mandatory
    {
        Specification = new SpecificationBase
        {
            Name = "MyProtocol",
            Label = "My Protocol",
            Version = new Version(1, 0, 0),   // mandatory
        };
    }

    public override void SetConfiguration() { /* RawSettings */ }

    // Create a protocol variable from commParams ("key=value;...")
    public override IProtocolVariable? CreateProtocolVariable(
        IVariableBase variable, string commParams, bool isCommunicated)
        => new ProtocolVariable
        {
            Variable = variable,
            IsCommunicated = isCommunicated,
            ProtocolVariableSpecification = MyVariableSpecification.Create(commParams)
        };

    // Two more overloads that also receive the project's events
    // (a protocol without events simply delegates to the one above)
    public override IProtocolVariable? CreateProtocolVariable(
        IVariableBase variable, IVarEvent variableEvent, string id) { ... }
    public override IProtocolVariable? CreateProtocolVariable(
        IVariableBase variable, IEnumerable<IVarEvent> variableEvents,
        string commParams, bool isCommunicated) { ... }

    // Data from the driver — called on the driver's task, must not block
    public override Task AddReceivedDataToQueueAsync(
        IEnumerable<string> data, CancellationToken ct = default) { ... }

    // Processing: Decode + NotifyValueChanged(Async) of the updated variables
    protected override void ProcessReceivedData(IEnumerable<string> data) { ... }
    protected override Task ProcessReceivedDataAsync(
        IEnumerable<string> data, CancellationToken ct = default) { ... }

    // Received data -> values of the protocol variables
    protected override IEnumerable<IProtocolVariable> Decode(
        IEnumerable<string> data) { ... }

    // Variable values -> data for the device
    protected override IEnumerable<string> Encode(
        IEnumerable<IProtocolVariable> protocolVariables) { ... }

    public override Task StartAsync(CancellationToken ct = default) { ... }
    public override Task StopAsync(CancellationToken ct = default) { ... }
    public override void Dispose() { ... }
}

As with drivers, Specification.Version is mandatory (a plugin without it is skipped when the folder is scanned) and the protocol gets its Logger from the driver it is added to. ProtocolVariable is the ready-made implementation of IProtocolVariable; after setting a value, notify the subscribers (controls, loggers, scripts, command drivers) with NotifyValueChangedAsync().

Variable specification

The addressing ("which value belongs to which variable") is defined by a class derived from ProtVariableSpecification — e.g. the Raw CAN protocol keeps CanId, Offset, and ByteOrder in it. The values come from the Comm params text field the user fills in when assigning a variable.

Comm params template and serialization

QInsight knows nothing about the protocol's parameters — it asks the plugin for everything:

  • Template: override CreateDefaultCommParam(variable, variableEvents) and return a string listing every supported key with a representative value — it is pre-filled for the user when a variable is assigned. Without an override the generic direction/eventRef/multiplier/id template is used.
  • Serialization: the specification is turned back into text by ToCommParam() on the specification. The default implementation builds the string from public properties (camelCase names; Direction, VariableEvent as eventRef and Multiplier come first). If the properties do not match the keys 1:1, override ToCommParam() so that its output can be re-parsed unchanged by CreateProtocolVariable (round-trip).
  • Protocol settings: if the protocol has settings of its own, override DefaultRawSettings — the template is pre-filled when the protocol is added. Read them with the shared SettingsParser.Parse(RawSettings, knownKeys, Logger, "My protocol") (Qenex.QSuite.Protocols.Protocol), which reports unknown keys with a warning in the log just like the shipped protocols.
  • Transport loss: the driver calls OnTransportConnectionChanged(bool) when it loses and re-establishes its connection. A protocol with its own session or polling loop should wait on false and continue or reconnect on true (pattern: XCP and the Modbus master); a protocol that only answers or receives needs to do nothing.

Writes and On Request reads

Both are optional interfaces of the protocol; the driver delegates the requests to them through IProtocolVariableCommandDriver (see Custom driver).

  • Operator writes — implement IProtocolVariableWriteProtocol: CanWriteVariable(pv) says whether the variable belongs to this protocol and may be written (Write Mode of a control is offered only then), WriteVariableAsync(pv, ct) transfers its current value to the device. To send over the driver's transport, also implement ITransportProtocol<T>: the driver injects a transmitter delegate with SetTransmitter(...) before starting its protocols and clears it (null) on stop. Controls do not show the written value — only what the device returns with the next read (Write to the device).
  • On Request reads — implement IProtocolVariableReadProtocol, but only in request/response protocols where QInsight is the master (XCP, Modbus master, simulation); protocols that merely receive a stream do not. A variable bound to an On Request event (Events) is excluded from periodic polling; CanReadVariable(pv) enables the Read button of the controls for it and ReadVariableAsync(pv, ct) reads it once, applies the value like a poll (value, timestamp, NotifyValueChangedAsync) and throws on failure so the control can report it.

Narrowing to specific drivers

The data type is sometimes not selective enough: several drivers may hand over string, yet your protocol only understands the format of one of them. Such a protocol can name its drivers explicitly:

public override IReadOnlyList<string> CompatibleDrivers => ["MyDeviceDriver"];

The list contains driver Names (the stable technical ids). Project Configuration then offers the protocol only under those drivers. Leave the property alone (it defaults to null) for any protocol that works with whole classes of drivers — e.g. Modbus runs over serial and TCP alike.

Deployment

Copy the DLL into the Protocols folder next to QInsight.exe and restart QInsight. The protocol appears in the Home → Project Configuration → Protocols list and can be added to any compatible driver — under other drivers it is greyed out.

Next

Custom Control — working driver + protocol pairs are in two chapters: the smallest possible one in Example ISS and the full pattern including writes in Example temp sensor.