Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved Global serial port class

    General and Desktop
    3
    4
    705
    Loading More Posts
    • 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.
    • C
      Chobin last edited by

      Hi,

      I developed a class to communicate with an external device through RS485 serial port.
      In this class I built two private structs of parameters, where the external device writes and reads data. I built some member functions to read and modify these structs.

      I'd like to call these member functions from any point of my software. Is it possible to do this?

      Many thanks for your support.

      Best Regards,

      Chobin

      jsulm 1 Reply Last reply Reply Quote 0
      • jsulm
        jsulm Lifetime Qt Champion @Chobin last edited by

        @Chobin Sure, it is.
        You just need the instance of that class where you want to call those methods. There are different ways to get the instance. One is singleton pattern, but you should avoid it. Other way is to provide a pointer to all other classes which needs access to it.

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

        1 Reply Last reply Reply Quote 1
        • C
          Chobin last edited by

          Hi,

          thank you for your replay.

          Is it a good way to define my mathods as static?

          For example:
          @
          class serialPort {
          public:
          serialPort(QObject *parent = nullptr);
          static void WriteSerialPortBuff(quint16 dataNum);
          }

          //I call the method as follow from other classes
          serialPort::WriteSerialPortBuff(5);
          @

          What do you think about it?

          Many Thanks.

          Best Regards,

          Chobin

          M 1 Reply Last reply Reply Quote 0
          • M
            mostefa @Chobin last edited by

            @Chobin said in Global serial port class:

            Hi,

            thank you for your replay.

            Is it a good way to define my mathods as static?

            For example:
            @
            class serialPort {
            public:
            serialPort(QObject *parent = nullptr);
            static void WriteSerialPortBuff(quint16 dataNum);
            }

            //I call the method as follow from other classes
            serialPort::WriteSerialPortBuff(5);
            @

            What do you think about it?

            Many Thanks.

            Best Regards,

            Chobin

            Hi

            I think that it is only a good way if you want to have just one serialPort for whole your application project

            But if one day you will have a device with two serial ports static method will be not a good way , because this will mean that for each instance of serialPort the writeSerialPortBuff will produce the exactly same result ,

            Therefore for me it is not a good way to have such method as a static method,

            But maybe is there any other opinions?

            1 Reply Last reply Reply Quote 1
            • First post
              Last post