我对C ++ / Qt还是很陌生,在编写使用连接到多个其他设备的SerialBus连接的应用程序时遇到了一些问题。
为了进行交流,我编写了一个包装QtSerialBus的类,并进行了一些编码/校验和计算等。该类应可由程序中的其他其他类/线程/模块使用。
这个包装器类看起来像这样简化:
标题:
class PeripherieCommunicator : public QObject
{
Q_OBJECT
public:
explicit PeripherieCommunicator(std::shared_ptr<MultiMasterCommunicator> mmcom, QObject *parent = nullptr);
~PeripherieCommunicator();
private:
std::shared_ptr<MultiMasterCommunicator> mmcom;
template<typename T1> void send(unsigned char command, unsigned char destAddress, T1 sendItem = nullptr) {
auto sendBuffer = QByteArray::fromRawData(reinterpret_cast<const char*>(&sendItem), sizeof(T1));
emit sendCommand(command, destAddress, sendBuffer);
}
signals:
void sendCommand(unsigned char command, unsigned char destAddress, QByteArray sendBuffer = nullptr);
void openPort();
public slots:
void CardTypeAGetFullDeviceInfo(unsigned char ucAddress);
void CardTypeBStartPositioning(unsigned char ucMotorNbr, signed long slPosition, signed long slSpeed, signed long slAcceleration, unsigned char ucAddress);
signals:
void CardTypeAFullDeviceInfoReceived(CARDA_FULL_DEVICE_INFO strFullDeviceInfo);
void CardTypeBStartPositioningReceived(unsigned short usCMDAnswer);
};
广告位定义示例:
void PeripherieCommunicator::PR112StartPositioning(unsigned char ucMotorNbr,
signed long slPosition, signed long slSpeed, signed long slAcceleration,
unsigned char ucAddress)
{
CARDTYPEA_START_POSITIONING data;
data.usMotorNumber = ucMotorNbr;
data.slDestinationPosition = slPosition;
data.slSpeed = slSpeed;
data.slAcceleration = slAcceleration;
send(COMMAND_START_POSITIONING , ucAddress, data);
}
到目前为止,它工作正常,这意味着命令已通过SerialBus发送并执行。但是,总线上的大多数设备都会发送我需要处理的答复。 到目前为止,我所尝试的是在发送将处理答复并发出相应信号的命令之前,先连接了lamba函数。但这将导致以下情况:正在调用与该信号连接的每个插槽。 相反,我想要实现的是某种回调功能。这意味着,例如,插槽“ CardTypeAGetFullDeviceInfo”将获得一个附加参数,即回调插槽。 有建议的方法吗?
谢谢