Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Adding custom ID String to Signal/Slot Event?

Adding custom ID String to Signal/Slot Event?

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 389 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SeppyQT
    wrote on last edited by
    #1

    Hallo,

    I have a Problem with QModbusServer. I want to pass an custom Identification string for every ModbusServer Instance.

    void QModbusManager::onServerAddClick()
    {
    	QModbusServer* device = new QModbusTcpServer(this);
    	QModbusDataUnitMap reg;
    	reg.insert(QModbusDataUnit::Coils, { QModbusDataUnit::Coils,0,20 });
    	reg.insert(QModbusDataUnit::DiscreteInputs, { QModbusDataUnit::DiscreteInputs,0,20 });
    	reg.insert(QModbusDataUnit::InputRegisters, { QModbusDataUnit::InputRegisters,0,20 });
    	reg.insert(QModbusDataUnit::HoldingRegisters, { QModbusDataUnit::HoldingRegisters,0,20 });
    	device->setMap(reg);
    	auto name = this->ui.tb_servername->text();
    	connect(device, &QModbusServer::dataWritten, this, [&](QModbusDataUnit::RegisterType type, int address, int size) { 
    		this->DataWrittenCallback(name, type, address, size);
    		});
    	
    	//connect(device, &QModbusServer::dataWritten, this, &QModbusManager::DataWrittenCallbacktest);
    	connect(device, &QModbusServer::errorOccurred, this, [&](QModbusDevice::Error error) { this->ErrorCallback(name, error); });
    
    	//Setting UP IP
    	auto m = ui.lv_interfaces->currentIndex();
    	auto ip = m.data(Qt::DisplayRole).toString();
    	device->setServerAddress(1);
    	device->setConnectionParameter(QModbusDevice::NetworkAddressParameter, ip);
    	device->setConnectionParameter(QModbusDevice::NetworkPortParameter, this->ui.tb_port->text());
    	QPair<QModbusServer*, QString> p(device, this->ui.tb_target->text());
    	this->deviceMap.insert(name, p);
    	device->connectDevice();
    	this->setupTabPage(name);
    }
    

    When I click Add Server. It registers the Server Name and in the deviceMap there is the correct name. But when the Event is invoked name = ??? (Debugger Visual Studio 2019). Why does it forget the value?

    My Intention is to add a Sender Information like in C# Winforms eventcallback(object sender, EventArgs e) or POCO C++ eventCallback(void* sender, EventArgs& arg)

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi
      it seems
      auto name = this->ui.tb_servername->text();
      is a local variable and you seems to capture it by referene so when
      onServerAddClick ends, its no longer valid. (and dataWritten fires much later)
      Try
      connect(device, &QModbusServer::dataWritten, this, [=]

      1 Reply Last reply
      1
      • S Offline
        S Offline
        SeppyQT
        wrote on last edited by
        #3

        Thanks,

        my bad, I misinterpreted the & inside the brackets. I thought [&] were for the lazies meaning include everything. Now I know it is = for values.

        If you wonder why I need multiple Modbus Servers It shall be a Modbus to UDP JSON connector.

        mrjjM 1 Reply Last reply
        0
        • S SeppyQT

          Thanks,

          my bad, I misinterpreted the & inside the brackets. I thought [&] were for the lazies meaning include everything. Now I know it is = for values.

          If you wonder why I need multiple Modbus Servers It shall be a Modbus to UDP JSON connector.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @SeppyQT
          np :)
          I shot my foot of a few times capturing locals by reference.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved