1

I am working on a GUI with wxWidgets to communicate with a PLC. I dedicate a thread especially for the communication via the ModBus protocol. The read variables are send back to the GUI as a custom event containing the adress of the variable and the value. The GUI is designed as a notebook which displays some content that depends on the read variables.

The problem

I am looking for the best and cleanest way to assign the received variable to a widget that should display it (either a led that must be switched on/off, or a textcontrol,...). My first idea was to choose the Ids of the widgets such that it is linked to the modbus adress. Thus, when I receive the event in the top handler (one page of a notebook), I would distribute it further to the respective widget as follows:

int id = convert_adress2id(evt.adress); // For example, 2*adress + wxID_LAST
wxWindow w = FindWindow(id);
w.AppendEvent(evt); // Distribute it further to the widgets that handles the adress

This solution seems nice to me but is not very robust. For example, one widget might need several variables from different widgets. The task is complex because there are a lot of variables.

So what other solution do I have? I need sort of a table to connect the adresses to the widgets. But how to design it the best way?

I thank you in advance for your help.

Chenoille
  • 45
  • 6
  • 1
    And how would you convert? I personally would rather look up the widgets *directly*, e.g. from within a `std::unordered_map` – note especially the usage of pointers; with `wxWindow w = ...` you create a *copy* of the element returned, where I'd put a hand into fire for this not being what you need... – Aconcagua Oct 24 '22 at 08:51
  • 1
    It's not a problem if a widget is interested in several variables, it would then simply appear multiple times in the map. The other way round – several widgets for the same variable – needs an adjustment, though. The value type then might be turned from single pointer to a vector of pointers, and you could on updating iterate over all these. – Aconcagua Oct 24 '22 at 08:54
  • 1
    By the way, there's a [socket implementation integrated](https://wiki.wxwidgets.org/WxSocket) in wxWidgets, you could, instead of having a separate thread, use this one, which would produce an event on data being available on the socket. Within the event you'd then read this data and process it directly. This way your implementation can remain single-threaded and you spare all those efforts around thread synchronisation. If you are on serial, there's no direct support, though there seems to be a 3rd party library being [available](https://forums.wxwidgets.org/viewtopic.php?t=22670) at least. – Aconcagua Oct 24 '22 at 09:02
  • @Aconcagua That is what I was thinking about but was afraid to. But finally, I think that is the best way, also if I want to change the adress code (if I change the protocol for example). Thank you for the advices. – Chenoille Oct 24 '22 at 09:03

0 Answers0