Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pymodaq as a client with TCP/IP #284

Open
jupiterMJM opened this issue Mar 11, 2024 · 16 comments
Open

Pymodaq as a client with TCP/IP #284

jupiterMJM opened this issue Mar 11, 2024 · 16 comments

Comments

@jupiterMJM
Copy link

Hi!
I have a program that always runs and processes data (let's name it prg1.py) . What I want is to configure Pymodaq such that it connects as a client to the server prg1.py .
The programme prg1.py works as a server: I ve tried to exchange some data between prg1.py and a custom client program.
However, when I try to tell to Pymodaq to do the same. It doesn't even connect to the server:
image
(eventhough Pymodaq tells me that it is connected to something).
So, if someone has some ideas, I take!
Thanks

@seb5g
Copy link
Contributor

seb5g commented Mar 11, 2024

Could you explain a bit more thoroufully exactly what you want to do with pymodaq with respect to your program/server? I guess you want to use the daq_viewer to control/plot the data taken by your program. Could you confirm this ?

@jupiterMJM
Copy link
Author

Yes that's it. Here is a better explain:

  • prg1.py: extracts some useful data from detectors. It is home coded.
  • pymodaq daq_viewer: plots (ideally) the datas extracted by prg1.py
    What I would like to do is to setup prg1.py as a server (using socket module); and configure daq_viewer as a client. Daq_viewer has thus to connect to prg1.py and plot the data it receives. I know prg1.py does work as I have tried it with a custom program.

@seb5g
Copy link
Contributor

seb5g commented Mar 11, 2024

Sure then you have to do the same as I did with the Horiba plugin (pymodaq_plugins_horiba). In there there is a DAQ_Viewer1D called labspec6 that connects to a server to communicate with the labspec6 software (so exactly what you want if you replace labspec6 by prg1).

It means you have to code a new instrument class plugin to do so (not using the built in TCP/server stuff that's made for pymodaq control modules to communicate together)

However, while you are it to code something on the plugin side, you may was well directly import your prg1 classes into the plugin as a wrapper to your instrument (or whatever is behind prg1)

@jupiterMJM
Copy link
Author

So, if I understand well: there is no way to easily connect daq_viewer to a custom program by tcp/ip. By easily, I mean "only create a server (as in the socket module documentation) and connect daq_viewer by entering the IP and port information". If so, is there an other way to send data from a custom program to daq_viewer (with an easier way)?

@seb5g
Copy link
Contributor

seb5g commented Mar 11, 2024

Well there is but your program (prg1) has to be a client (while you use the TCP server on pymodaq side). Then your client has to implement the functionalities required by the TCP/IP communication

@seb5g
Copy link
Contributor

seb5g commented Mar 11, 2024

There is an example (a bit old) of such a thing in the example folder when a client in Labview is connecting to the pymodaq server

@jupiterMJM
Copy link
Author

Sounds interesting. Where do I find the "example folder"? I didn't find any such "example folder"? Thanks a lot

@seb5g
Copy link
Contributor

seb5g commented Mar 11, 2024

image

If you start from a python code, you should use the serializer/deserializers to send data objects to the tcp/ip connection:
image

@jupiterMJM
Copy link
Author

It seems that I can connect to a TCP/IP python server thanks to:
image
When the connection is set up, PyModaq sends to the server the following message: b'\x00\x00\x00\x07GRABBER' .
However, there is anything else after this: I do not receive any other information, and when I send some data with "str(data).encode()" It does not display anything.
Therefor, here is my question: can I use this box to connect Pymodaq as a client to the server? Or does it aim to something else?
Thanks

@seb5g
Copy link
Contributor

seb5g commented Mar 11, 2024

This is made for control modules to talk to each other (here a real instrument control module) will connect to a TCPServer control module (on localhost for simplicity). So you have to open both control modules from your console or IDE, first start the server and the client and finally connect the client to the server. But with this you cannot do custom stuff (like just sending "str(data).encode()" that by the way will just be your data string representation not your actual data) except if you know what your supposed to do... But then again, if you use it in that direction, your Mock control plugins (client) is made to acquire data from an instrument, even if just a mock, The Server can control the behaviour of this client and get back data from it (so you could control your real instrument from afar on the network).

I created a somewhat better example (but not that much) on the 4.2.x_dev branch on github:

image

where a custom client (that could be your program) connect to the server control module and use it to plot data on it.

But if really your program is actually kind of an instrument and you want to display data (and control it a bit) then you should follow the Labspec6 example and create an instrument plugin

@jupiterMJM
Copy link
Author

Hi!
Thanks to the example you've shown, I've been able to make a very-basic program that seems to work. I based the class on this example, but I've modified it a little bit to make it fit what I wanted. I join the program to this message, for any future reader.
tcp_ip_client_from_github.txt
I know that the program may not be well optimized, but at least the connection works!
Thanks for your help seb5g.

@seb5g
Copy link
Contributor

seb5g commented Mar 14, 2024

good to know

@seb5g
Copy link
Contributor

seb5g commented Mar 14, 2024

@rgeneaux maybe you could have also a look at it for your use case, but still I think the instrument plugin is still the best approach (with pyleco even better) !

@jupiterMJM
Copy link
Author

Hi again!
So the program I sent you works. But, I have a problem of refresh. Here is the explain: the program I've sent works very well when the sleep in the loop is at 1s. However, when I lower this value to 0.03s (or a resfresh of 30Hz, which is the frequency of refresh I want to reach), the plot of the data on Pymodaq desynchronizes with the client. Of course, I've tried to lower the refresh time on Pymodaq but it didn't change anything.
From the various tests I've made, it is just as if Pymodaq was saving the data and was plotting it with a constant delay. When the refresh frequency of the client increases, the delays add up and desynchronise the whole thing.
Do you know if there is a way to resolve this problem?
Thanks

@seb5g
Copy link
Contributor

seb5g commented Mar 14, 2024

Well it is known that there is a tIme lag when using the legacy TCP/IP stuff. The problem is to do a proper asynchronous execution as here it is looping like creasy...

We worked with @BenediktBurger to use a package he developed based on remote call procedure, called pyleco. It's use will be almost the same as the TCP/IP but works much better and is more general. There is a branch in the GitHub account of pymodaq to test it.

@BenediktBurger
Copy link
Contributor

From the various tests I've made, it is just as if Pymodaq was saving the data and was plotting it with a constant delay.

You have to configure the refresh rate. As far as I know, the pymodaq module will poll the values only every some seconds (which you can configure).

LECO - Laboratory Experiment COntrol, with its python package PyLECO, is a communication protocol to connect different programs on your computer. It encourages to use remote procedure calls as a communication standard.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants