Tentez l'expérience  
Menu
Tentez l'expérience  

[Break Tech] How to establish the communication between two devices / two file of code

Écrit par Florian Dordain, Publié le 01 juillet 2021

For that purpose you need to use QRemoteObjects ! 

For example, QRemoteObjects is able to manage the communication between a  tablet and PC. 

What is QRemoteObject ? 

Qt Remote Objects (QtRO) is an Inter-Process Communication (IPC) module developed for Qt. This module extends Qt's existing functionalities to enable information exchange between processes or computers, easily.

In a word, QRemoteObject allows to use function in a code file from another devices. 

Sources : https://doc.qt.io/qt-5/qtremoteobjects-index.html

How does it work ?

Afficher l’image source

Of course, there is a code in both PCs. For example : 

Computer 1 :

  • Source.cpp file (automatically generated from Rep file)
  • Rep file contains slot and properties - variables
  • Client.cpp to connect the replicas

Computer 2:

  • Replicas.cpp (automatically generated from Rep file)
  • Rep file contains slot and properties - variables
  • Gateway.cpp to receive signals/slots

The replicas fil is a copy of source file. A communication (IPC) is automatically generated between both files. So if you use signals or slot in source or replicas file the trigger is sent through the network to other file. 

To get more information please see [ How to improve the UX of an industrial robot with Qt ] :

https://resources.qt.io/on-demand/how-to-improve-the-ux-of-an-industrial-robot-with-qt-devdes-2021

For example, in KMeleonBox :

How to set up the PRO file of my project ? 

It is necessary to add the following line : 

QT += remoteobjects

And in the replica project you need to add this line :
REPC_REPLICA = myfile.rep

In the source project you can use the following lines :

QT
+= remoteobjects
REPC_SOURCE = myfile_source.rep

How to set up the REP file ?

You need to add a new file in your project with extension .rep. 

Use the following line to add a property. For me I see the remoteobject properties like sharde variables.

PROP( type  variablename = value )
For example : PROP ( bool ok = false)

Use the following line to declare a SLOT. For me the SLOT opens an acces to a specific function from another device.

SLOT(slot_name(parameter 1, parameter 2, ....))
For example : SLOT( getNumber(int ID))

How to set up the client.cpp file ?

You need to add a nex cpp class in your project source. 

In this file, you need to write the slot from REP file like virtual function. 

For example : 

virtual void getNumber(int ID);

In your cpp file, you need to add the parent myfileSimpleSource and include the file "

rep_myfile_source.h"

How to set up the gateway.cpp file ?

You need to add a nex cpp class in your project replicas. 

In this file, you need to write the signals when the properties value changed. 

For example : 

void okChanged(bool value);

In your cpp file, you need to include the file "rep_myfile_replicas.h" and you need to declare a shared pointer to store the replicas

QSharedPointer<myFileReplica>

Afterwards you need to connect the shared pointer to slots. For example :

connect(myPointer.data(),SIGNAL(mySignal()),this,SLOT(mySlot()));

And finally how to establish the communication between both devices ?

In your main.cpp in your replicas project, accept the connection :

    QSharedPointer<SimpleSwitchReplica> ptr; // shared pointer to hold source replica

QRemoteObjectNode repNode; // create remote object node
repNode
.connectToNode(QUrl(QStringLiteral("local:switch"))); // connect with remote host node

ptr
.reset(repNode.acquire<SimpleSwitchReplica>()); // acquire replica of source from host node

Client rswitch(ptr); // create client switch object and pass reference of replica to it

In your main.cpp in your source project, cast the connection :

    SimpleSwitch srcSwitch; // create simple switch

QRemoteObjectHost srcNode(QUrl(QStringLiteral("local:switch"))); // create host node without Registry

srcNode
.enableRemoting(&srcSwitch); // enable remoting/sharing

And now you can run both executable, the connection is established !

 

Please see the example here : https://doc.qt.io/qt-5/remoteobjects-example-static-source.html

 

Aucun commentaire pour l'instant

Dites-nous ce que vous en pensez