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. Qt Serial not usable from another class
Forum Updated to NodeBB v4.3 + New Features

Qt Serial not usable from another class

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 4 Posters 1.2k 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
    bladekel
    wrote on last edited by
    #1

    Hello to all.
    I'm trying to read and write with serial port. I have created a class , named serial , and connect it with readyRead on mainwindow.cpp.

    I'm reading the data when I received it by connecting signal in serial.cpp without any problem.

    I'm writing the data in serial.cpp without any problem too.

    But when I tried to write data from another class(manuel.cpp) "The program has unexpectedly finished".

    With my researches I found that the when I tried to access the serial device in manuel.cpp , I cant.
    I looked from manuel.cpp , isOpen,isReadable,isWriteable and they are both false.
    But when I looked from serial.cpp they are true.

    I think the problem is the serial device can reachable from another class. Is there any solvers?

    Here is my github link

    jsulmJ J.HilkJ 2 Replies Last reply
    0
    • B bladekel

      Hello to all.
      I'm trying to read and write with serial port. I have created a class , named serial , and connect it with readyRead on mainwindow.cpp.

      I'm reading the data when I received it by connecting signal in serial.cpp without any problem.

      I'm writing the data in serial.cpp without any problem too.

      But when I tried to write data from another class(manuel.cpp) "The program has unexpectedly finished".

      With my researches I found that the when I tried to access the serial device in manuel.cpp , I cant.
      I looked from manuel.cpp , isOpen,isReadable,isWriteable and they are both false.
      But when I looked from serial.cpp they are true.

      I think the problem is the serial device can reachable from another class. Is there any solvers?

      Here is my github link

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @bladekel From design point of view what you are doing is bad:

      • QSerialPort *arduino; - should be private, provide public read() and write() methods

      • const QString arduino_seri = "75533353637351110171"; -should be private + a public getter

      • QString arduino_port_name; - should be private + a public getter

      • bool arduino_is_available; - should be private + a public getter

      Your app is probably crashing because you did not call serial::ard_kontrol() before writing. In serial::ard_kontrol() you initialize arduino pointer, so if you do not call it you're dereferencing a dangling pointer. In general in such cases you should first debug to see where exactly it is crashing and why.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        You do all your initialisation in ard_kontrol. You never call that function thus nothing is initialised.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        2
        • B bladekel

          Hello to all.
          I'm trying to read and write with serial port. I have created a class , named serial , and connect it with readyRead on mainwindow.cpp.

          I'm reading the data when I received it by connecting signal in serial.cpp without any problem.

          I'm writing the data in serial.cpp without any problem too.

          But when I tried to write data from another class(manuel.cpp) "The program has unexpectedly finished".

          With my researches I found that the when I tried to access the serial device in manuel.cpp , I cant.
          I looked from manuel.cpp , isOpen,isReadable,isWriteable and they are both false.
          But when I looked from serial.cpp they are true.

          I think the problem is the serial device can reachable from another class. Is there any solvers?

          Here is my github link

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          @bladekel

          Your code is a tad difficult to understand. But from a quick browse of your code I think I found the culprit:

          Your have 2 objects of your class serial active. One created and maintained from mainwindow and one from manual.

          But you can open your serial port only once -> Error

          Remove the serial instance from your manual class and use signal slots in your mainwindow to connect manual and serial together and let them communicate that way


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          2

          • Login

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