Home
  • AbstractComm
  • AbstractComm Class

    串口通信的接口类,所有具体实现串口通信/网络通讯的相关类都应该继承此类并实现其中的纯虚方法。 More...

    Properties

    Public Functions

    virtual bool isStarted()
    virtual void setStarted(const bool started)
    void setState(const CommState &state)
    CommState &state()

    Public Slots

    virtual void close() = 0
    virtual void init() = 0
    virtual void openComm(const QString &name) = 0
    virtual void setCommProperty(const QString &key, const QVariant &value) = 0
    virtual void write(const QByteArray &rawdata) = 0

    Signals

    void codeMayMessed()
    void recvLine(const QByteArray &data)
    void recvRawData(const QByteArray &data)
    void sendData(const QByteArray &data)
    void stateChanged(const CommState &serial)

    Protected Functions

    AbstractComm()
    void close(QIODevice *device)
    void writeData(QIODevice *device, const QByteArray &rawdata)

    Protected Slots

    virtual void onRead() = 0

    Detailed Description

    \authorBriFuture \date 2018.07.20

    串口通信的接口类,所有具体实现串口通信/网络通讯的相关类都应该继承此类并实现其中的纯虚方法。

    @date 2018.04.26

    The Interface of serial communication, all implementation of concrete serial communication should be the subclass of this pure virtual class and implement all the virtual methods in this class.

    当串口的实现较多时,实现该类的接口时,将实现类的构造函数声明为 Q_INVOKABLE, 并在相应的工厂类中进行注册,便于使用工厂方法构造子类。

    When implementing this interface, it is recommonded that the constructor of subclass is decorated with the MACRO Q_INVOKABLE and register it into Qt's MOC system, then it will be easy to use factory to construct subclass. But the port is exclusive from other threads, so be careful when rewrite copy constructor if using Q_DELCARE_METATYPE to declare subclass as metatype.

    @date 2018.05.09

    因为该接口的子类受其它串口管理类的管理,并且当前程序设计是将接口的子类放入子线程中单独执行, 所以实现子类时需要特别注意,子类的所有成员(QSerialPort,QTimer之类)都需要放在 init 函数中 进行初始化,否则 Qt 的多线程执行时就会报错。移除具有工厂类作用的api

    Because the subclasses of this interface is managed by other COM Manager, and the subclasses is designed to execute into a sub thread, when realizing subclass of this interface should remember that all member of certain subclass should be initialized in Function init, otherwise the Qt's multi-thread mechansim would raise error. Apis that effect like factory are removed now。

    See also CommManager.

    Property Documentation

    started : bool

    Access functions:

    virtual bool isStarted()
    virtual void setStarted(const bool started)

    Member Function Documentation

    [protected] AbstractComm::AbstractComm()

    Default constructs an instance of AbstractComm.

    [pure virtual slot] void AbstractComm::close()

    关闭外部设备连接操作

    [protected] void AbstractComm::close(QIODevice *device)

    默认的关闭串口/网络操作 提供统一的关闭操作,供子类调用,若子类对关闭操作没有特殊的要求,那么不需要重写关闭操作

    [signal] void AbstractComm::codeMayMessed()

    [pure virtual slot] void AbstractComm::init()

    初始化串口/网络,设计串口都是工作在子线程中,因此需要通过信号槽方式, 在子线程中运行初始化函数

    [virtual] bool AbstractComm::isStarted()

    返回当前串口/网络设备是否正在运行的标志位

    example:

    AbstractComm *ac = new ImplComm;
    ac->openDevice("device");  // 打开设备
    ac->isStarted();  // true
    ac->close();
    ac->isStarted();  // false

    Note: Getter function for property started.

    [pure virtual protected slot] void AbstractComm::onRead()

    读取操作,若运行在子线程的循环中,需要显式调用 Qt 的事件分发, 避免子线程无法关闭。

    See also AbstractComm::readDevice(QIODevice *device).

    [pure virtual slot] void AbstractComm::openComm(const QString &name)

    根据串口/网络名称,打开新的串口/网络

    example:

    AbstractComm *cfd = new ImplComm1;
    cfd->openComm("COM1");  // 打开串口

    [signal] void AbstractComm::recvLine(const QByteArray &data)

    从串口/网络等设备中接收到的一行数据。 行结束符为 LineSeparator

    [signal] void AbstractComm::recvRawData(const QByteArray &data)

    从串口/网络等设备中接收到的数据,该数据是一行数据中的一部分。

    [signal] void AbstractComm::sendData(const QByteArray &data)

    表示数据已经发送到外部设备中

    [pure virtual slot] void AbstractComm::setCommProperty(const QString &key, const QVariant &value)

    键和值需要参考 QSerialPort 的 Property 设置串口属性(波特率,停止位等)、网络属性(地址)等

    example: AbstractComm *cfd = new ImplComm1; cfd->setCommProperty("baudRate", 2400); // 设置波特率为 2400 cfd->setCommProperty("dataBit", 0); // 设置数据位为 8

    [virtual] void AbstractComm::setStarted(const bool started)

    设置标志位,表示串口/网络设备是否可以运行(由控制器设置,但实现类可忽略该标志位)

    example:

    AbstractComm *ac = new ImplComm;
    ac->setStarted(true);  // 设置标志位
    ac->isStarted();  // true

    Note: you need to have the flag set when opening a device by default. or the manager may not set the flag to false.

    AbstractComm *ac = new ImplComm;
    ac->openDevice("COM1");
    ac->isStarted();  // true

    Note: Setter function for property started.

    See also isStarted() and AbstractComm::openDevice.

    void AbstractComm::setState(const CommState &state)

    设置状态,注意,所有的子类都共享抽象类里的 static 内存区域, 对某个子类的 state 进行修改,会影响到其它所有的子类。

    example:

    AbstractComm *cfd = new ImplComm1;
    
    CommState s1 = cfd->state();
    qDebug() << s1; // { "COM1", 9600, false, "" }
    
    s1.baudRate = 38400;
    cfd->setState(s1);
    
    AbstractComm *chd = new ImplComm1;
    CommState s2 = cfd->state();
    qDebug() << s2; // { "COM1", 38400, false, "" }

    See also AbstractComm::state().

    CommState &AbstractComm::state()

    See also setState().

    [signal] void AbstractComm::stateChanged(const CommState &serial)

    [pure virtual slot] void AbstractComm::write(const QByteArray &rawdata)

    This is a funciton that actually performs the communication it receives raw string and send it into serial port 将文本数据写入到串口 原始的文本数据

    See also AbstractComm::writeData(QIODevice *device, const QByteArray &rawdata) and ;.

    [protected] void AbstractComm::writeData(QIODevice *device, const QByteArray &rawdata)

    默认的写串口操作

    在 rawdata 末尾添加回车换行符, 将 rawdata 发送至串口

    example:

    // Assume that content of a command is "#ALARM=1*ff"
    // in class ImplComm
    writeData(m_device, content);  // then the content will be send into the device (MCU) by SerialPort/Network