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. Global serial port class

Global serial port class

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 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.
  • C Offline
    C Offline
    Chobin
    wrote on last edited by
    #1

    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

    jsulmJ 1 Reply Last reply
    0
    • C Chobin

      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

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

      @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
      1
      • C Offline
        C Offline
        Chobin
        wrote on last edited by
        #3

        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
        0
        • C Chobin

          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 Offline
          M Offline
          mostefa
          wrote on last edited by
          #4

          @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
          1

          • Login

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