Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. QSharedMemory with to sessions

QSharedMemory with to sessions

Scheduled Pinned Locked Moved Mobile and Embedded
2 Posts 2 Posters 1.4k Views 1 Watching
  • 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.
  • B Offline
    B Offline
    Bobbyf_be
    wrote on last edited by Chris Kawa
    #1

    Hello,

    I'm just working on an mobile application. This application consists of two layer (can and gui layer). Every layer is a own application. Between this two applications I'm sharing data with QSharedMemory. So when data is coming in from the CAN-Bus to the CAN layer, I send these with the shared memory to the Gui layer. That works fine!

    Now I would like to send other data from the GUI layer to the CAN layer. So in the other direction. I do this with another class but with the same functions with another key. Write data in the gui layer seems to work. But read the data in the can layer is not possible. The message is: can't attach "QSharedMemory::handle: UNIX key file doesn't exist"

    Here the code:

    Writing in the shared memory on the gui layer:

    void DeviceAdrHandlingOUT::writeToSharedMemory(QString str)
    {
        QSharedMemory sharedMem("DeviceOUT");
    //    sharedMemory.setKey("Deviceout");
        if (sharedMem.isAttached())
        {
            sharedMem.detach();
        }
    
        QBuffer buffer;
        buffer.open(QBuffer::ReadWrite);
        QDataStream out(&buffer);
    
        out << str;
    
        int size = buffer.size();
    
        if (!sharedMem.create(DEVICES_SHARED_MEMORY_BUFFER_SIZE))
        {
            qDebug() << "Unable to create shared memory segment.";
    //        return;
        }
    
        qDebug() << "err" << sharedMem.errorString();
        sharedMem.lock();
        char *to = (char*)sharedMem.data();
        const char *from = buffer.data().data();
        memcpy(to, from, qMin(sharedMem.size(),size));
        sharedMem.unlock();
    
    }
    

    and here I read in the can layer:

    QString DeviceAdrHandlingOUT::read_shared_Memory()
    {
        sharedMemory.setKey("DeviceOUT");
        if(!sharedMemory.attach())
        {
            qDebug() << "can't attach" << sharedMemory.errorString();
        }
        
        QBuffer buffer;
        QDataStream in(&buffer);
        QString str;
    
        sharedMemory.lock();
        buffer.setData((char*)sharedMemory.constData(), sharedMemory.size());
        buffer.open(QBuffer::ReadOnly);
    
        in >> str;
    
        sharedMemory.unlock();
        sharedMemory.detach();
    
        return str;
    }
    

    So when I'm using the shared memory from the can to the gui layer --> it works!
    When I'm using the shared memory from the gui to the can layer --> it doesn't works!
    I'm using another key!

    Do anyone has an idea?

    Thanks,

    Bobby

    1 Reply Last reply
    0
    • N Offline
      N Offline
      nologinma
      wrote on last edited by Chris Kawa
      #2

      I think that the function will be the following:

      QString DeviceAdrHandlingOUT::read_shared_Memory()
      {
          QSharedMemory sharedMemory("DeviceOUT");    // <== MY CHANGE
      
          if(!sharedMemory.attach())
          {
              qDebug() << "can't attach" << sharedMemory.errorString();
          }
         
          QBuffer buffer;
          QDataStream in(&buffer);
          QString str;
       
          sharedMemory.lock();
          buffer.setData((char*)sharedMemory.constData(), sharedMemory.size());
          buffer.open(QBuffer::ReadOnly);
       
          in >> str;
       
          sharedMemory.unlock();
          sharedMemory.detach();
       
          return str;
      }
      
      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