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. Can QT access any company's serial port card?

Can QT access any company's serial port card?

Scheduled Pinned Locked Moved Unsolved General and Desktop
21 Posts 6 Posters 5.0k Views 2 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.
  • N Offline
    N Offline
    Nimika
    wrote on last edited by kshegunov
    #1

    Can QT access any company's serial port card? If so, how to read hex string data from TEWS technology TCP-469 card from one PC to another by RS-422?

    For TEWS card we first need to configure the card for RS-422 after opening the slot in read write command as following:

    QSerialPort serialPort1,serialPort2;
    unsigned long config;
    char Devname1[30];
    unsigned char data;
    int result1;
    int tty1;//baudrate=19200, spdres;
    int main(int argc, char *argv[])
    {
       QCoreApplication a(argc, argv);
    
       strcpy(Devname1,"/dev/ttySTDRV002_2");
       tty1=open(Devname1,O_RDWR|O_NOCTTY);
       qDebug()<<"COM port ttySTDRV002_2 has been connected" <<endl;
    
                            if(tty1<0)
                            {
                                    printf("Device not found\n");
                            }
                            config = TDRV002_INTF_RS422;
                            qDebug()<<"config"<< config << endl;
    
                            result1 = ioctl(tty1, TDRV002_IOCT_CONF_TRANS, config);
    
                            qDebug()<<"result1"<< result1 << endl;
                            if(result1<0)
                            {
                                    printf("Not able to configure receiver\n");
                            }
                            if(tty2<0)
                            {
                                    printf("Device not found\n");
                            }
                      
        QByteArray readData,read_Data;
        serialPort2.setPortName(Devname1);
        serialPort2.setBaudRate(QSerialPort:: Baud19200);                                                      // Setting BaudRate
        serialPort2.setDataBits(QSerialPort:: Data8);                                                          // Setting Data Bits
        serialPort2.setParity(QSerialPort:: NoParity);                                                       // Setting Parity bit
        serialPort2.setStopBits(QSerialPort:: OneStop);                                                        // Setting Stop bit
    
    
        if (serialPort2.isOpen()) {
            qDebug()<<"COM port channel 10 has been connected" <<endl;
    
        } else
            qDebug()<< "Open error"<< endl;
    
       for(int i =0;i<=10;i++){
    
       if(serialPort2.waitForReadyRead(1000))
            readData = serialPort2.readAll();
        qDebug()<< "Received data from channel 2"<<readData << endl;
       read_Data.append(readData);
            if (serialPort2.error() == QSerialPort::TimeoutError || readData.isEmpty())
              qDebug()<<"No data was currently available for reading from port ttySTDRV002_2"<<endl;
    }
    
        return a.exec();
    }
    

    then if I already open the slot of RS-422 so no need to reopen it through QSerialPort but still I need to open the port using QSerialPort otherwise "Open error" message pop-up and still if I am opening it through QSerialPort then I am not getting the exact data which I need to read.
    Kindly give any solution!

    [Added code tags ~kshegunov]

    jsulmJ 2 Replies Last reply
    0
    • N Nimika

      Can QT access any company's serial port card? If so, how to read hex string data from TEWS technology TCP-469 card from one PC to another by RS-422?

      For TEWS card we first need to configure the card for RS-422 after opening the slot in read write command as following:

      QSerialPort serialPort1,serialPort2;
      unsigned long config;
      char Devname1[30];
      unsigned char data;
      int result1;
      int tty1;//baudrate=19200, spdres;
      int main(int argc, char *argv[])
      {
         QCoreApplication a(argc, argv);
      
         strcpy(Devname1,"/dev/ttySTDRV002_2");
         tty1=open(Devname1,O_RDWR|O_NOCTTY);
         qDebug()<<"COM port ttySTDRV002_2 has been connected" <<endl;
      
                              if(tty1<0)
                              {
                                      printf("Device not found\n");
                              }
                              config = TDRV002_INTF_RS422;
                              qDebug()<<"config"<< config << endl;
      
                              result1 = ioctl(tty1, TDRV002_IOCT_CONF_TRANS, config);
      
                              qDebug()<<"result1"<< result1 << endl;
                              if(result1<0)
                              {
                                      printf("Not able to configure receiver\n");
                              }
                              if(tty2<0)
                              {
                                      printf("Device not found\n");
                              }
                        
          QByteArray readData,read_Data;
          serialPort2.setPortName(Devname1);
          serialPort2.setBaudRate(QSerialPort:: Baud19200);                                                      // Setting BaudRate
          serialPort2.setDataBits(QSerialPort:: Data8);                                                          // Setting Data Bits
          serialPort2.setParity(QSerialPort:: NoParity);                                                       // Setting Parity bit
          serialPort2.setStopBits(QSerialPort:: OneStop);                                                        // Setting Stop bit
      
      
          if (serialPort2.isOpen()) {
              qDebug()<<"COM port channel 10 has been connected" <<endl;
      
          } else
              qDebug()<< "Open error"<< endl;
      
         for(int i =0;i<=10;i++){
      
         if(serialPort2.waitForReadyRead(1000))
              readData = serialPort2.readAll();
          qDebug()<< "Received data from channel 2"<<readData << endl;
         read_Data.append(readData);
              if (serialPort2.error() == QSerialPort::TimeoutError || readData.isEmpty())
                qDebug()<<"No data was currently available for reading from port ttySTDRV002_2"<<endl;
      }
      
          return a.exec();
      }
      

      then if I already open the slot of RS-422 so no need to reopen it through QSerialPort but still I need to open the port using QSerialPort otherwise "Open error" message pop-up and still if I am opening it through QSerialPort then I am not getting the exact data which I need to read.
      Kindly give any solution!

      [Added code tags ~kshegunov]

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

      @Nimika https://doc.qt.io/qt-5.10/qtserialport-index.html
      http://doc.qt.io/qt-5/qtserialport-examples.html
      https://wiki.qt.io/Qt_Serial_Port

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

      1 Reply Last reply
      2
      • N Nimika

        Can QT access any company's serial port card? If so, how to read hex string data from TEWS technology TCP-469 card from one PC to another by RS-422?

        For TEWS card we first need to configure the card for RS-422 after opening the slot in read write command as following:

        QSerialPort serialPort1,serialPort2;
        unsigned long config;
        char Devname1[30];
        unsigned char data;
        int result1;
        int tty1;//baudrate=19200, spdres;
        int main(int argc, char *argv[])
        {
           QCoreApplication a(argc, argv);
        
           strcpy(Devname1,"/dev/ttySTDRV002_2");
           tty1=open(Devname1,O_RDWR|O_NOCTTY);
           qDebug()<<"COM port ttySTDRV002_2 has been connected" <<endl;
        
                                if(tty1<0)
                                {
                                        printf("Device not found\n");
                                }
                                config = TDRV002_INTF_RS422;
                                qDebug()<<"config"<< config << endl;
        
                                result1 = ioctl(tty1, TDRV002_IOCT_CONF_TRANS, config);
        
                                qDebug()<<"result1"<< result1 << endl;
                                if(result1<0)
                                {
                                        printf("Not able to configure receiver\n");
                                }
                                if(tty2<0)
                                {
                                        printf("Device not found\n");
                                }
                          
            QByteArray readData,read_Data;
            serialPort2.setPortName(Devname1);
            serialPort2.setBaudRate(QSerialPort:: Baud19200);                                                      // Setting BaudRate
            serialPort2.setDataBits(QSerialPort:: Data8);                                                          // Setting Data Bits
            serialPort2.setParity(QSerialPort:: NoParity);                                                       // Setting Parity bit
            serialPort2.setStopBits(QSerialPort:: OneStop);                                                        // Setting Stop bit
        
        
            if (serialPort2.isOpen()) {
                qDebug()<<"COM port channel 10 has been connected" <<endl;
        
            } else
                qDebug()<< "Open error"<< endl;
        
           for(int i =0;i<=10;i++){
        
           if(serialPort2.waitForReadyRead(1000))
                readData = serialPort2.readAll();
            qDebug()<< "Received data from channel 2"<<readData << endl;
           read_Data.append(readData);
                if (serialPort2.error() == QSerialPort::TimeoutError || readData.isEmpty())
                  qDebug()<<"No data was currently available for reading from port ttySTDRV002_2"<<endl;
        }
        
            return a.exec();
        }
        

        then if I already open the slot of RS-422 so no need to reopen it through QSerialPort but still I need to open the port using QSerialPort otherwise "Open error" message pop-up and still if I am opening it through QSerialPort then I am not getting the exact data which I need to read.
        Kindly give any solution!

        [Added code tags ~kshegunov]

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

        @Nimika said in Can QT access any company's serial port card?:

        I am not getting the exact data which I need to read

        Can you explain better? What do you expect and what do you get.
        Also, keep in mind that you will not necessarily get the data at once, so you need to buffer it until you have your whole package.

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

        N 1 Reply Last reply
        3
        • jsulmJ jsulm

          @Nimika said in Can QT access any company's serial port card?:

          I am not getting the exact data which I need to read

          Can you explain better? What do you expect and what do you get.
          Also, keep in mind that you will not necessarily get the data at once, so you need to buffer it until you have your whole package.

          N Offline
          N Offline
          Nimika
          wrote on last edited by
          #4

          @jsulm Its like I am sending a Hex code data like 1a 52 24 f1 12 upto 91 bytes through RS 422 cable in another PC where I have written the code I just shown. I am transmitting the data in Qbytearray and so what need to receive is the same but I am receiving like 1a 92 @ & so on and now I don't understand why am I getting this problem.

          May be it's because I opened the port twice or may be need to set buffersize double of what I am receiving or may be something with the time mismatch or may be something like datatype is wrong.

          jsulmJ aha_1980A 2 Replies Last reply
          0
          • N Nimika

            @jsulm Its like I am sending a Hex code data like 1a 52 24 f1 12 upto 91 bytes through RS 422 cable in another PC where I have written the code I just shown. I am transmitting the data in Qbytearray and so what need to receive is the same but I am receiving like 1a 92 @ & so on and now I don't understand why am I getting this problem.

            May be it's because I opened the port twice or may be need to set buffersize double of what I am receiving or may be something with the time mismatch or may be something like datatype is wrong.

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

            @Nimika Is there a reason why you're opening the port before using QSerialPort?

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

            N 1 Reply Last reply
            0
            • N Nimika

              @jsulm Its like I am sending a Hex code data like 1a 52 24 f1 12 upto 91 bytes through RS 422 cable in another PC where I have written the code I just shown. I am transmitting the data in Qbytearray and so what need to receive is the same but I am receiving like 1a 92 @ & so on and now I don't understand why am I getting this problem.

              May be it's because I opened the port twice or may be need to set buffersize double of what I am receiving or may be something with the time mismatch or may be something like datatype is wrong.

              aha_1980A Offline
              aha_1980A Offline
              aha_1980
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Nimika

              Am I right that you open the port with O/S methods instead QSerialPort::open()?

              Can you change to code to use QSerialPort::open() and afterwards use handle to perform O/S specific functions? Why do you need them at all?

              Qt has to stay free or it will die.

              N 1 Reply Last reply
              3
              • jsulmJ jsulm

                @Nimika Is there a reason why you're opening the port before using QSerialPort?

                N Offline
                N Offline
                Nimika
                wrote on last edited by
                #7

                @jsulm Its the TEWS serial port card so I need to open it before configuring it to RS 422.

                1 Reply Last reply
                0
                • aha_1980A aha_1980

                  @Nimika

                  Am I right that you open the port with O/S methods instead QSerialPort::open()?

                  Can you change to code to use QSerialPort::open() and afterwards use handle to perform O/S specific functions? Why do you need them at all?

                  N Offline
                  N Offline
                  Nimika
                  wrote on last edited by
                  #8

                  @aha_1980 Yeah I have already used it but still the same problem.

                  aha_1980A 1 Reply Last reply
                  0
                  • N Nimika

                    @aha_1980 Yeah I have already used it but still the same problem.

                    aha_1980A Offline
                    aha_1980A Offline
                    aha_1980
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @Nimika

                    then if I already open the slot of RS-422 so no need to reopen it through QSerialPort

                    Wrong. QSerialPort is a class wrapping O/S specific functions, so you cannot simply omit it's open() function

                    @aha_1980 Yeah I have already used it but still the same problem.

                    As said, we cannot help you if you don't use QSerialPorts functions.
                    So the only thing you need to do after open is configuring it to RS-422?

                    What is tty2?

                    You should also double check if the serial line settings are identical on both sides (bitrate, start, stop, parity, flow control).

                    Qt has to stay free or it will die.

                    N 1 Reply Last reply
                    3
                    • K Offline
                      K Offline
                      kuzulis
                      Qt Champions 2020
                      wrote on last edited by kuzulis
                      #10

                      Seems, you don't need to open a device twice. Just try to use QSerialPort::handle() after opening to do vendor-specific IOCTL's.

                      OOPS: I'm too late.. :)

                      aha_1980A N 3 Replies Last reply
                      4
                      • K kuzulis

                        Seems, you don't need to open a device twice. Just try to use QSerialPort::handle() after opening to do vendor-specific IOCTL's.

                        OOPS: I'm too late.. :)

                        aha_1980A Offline
                        aha_1980A Offline
                        aha_1980
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        @kuzulis But your answers are very appreciated :)

                        Qt has to stay free or it will die.

                        1 Reply Last reply
                        1
                        • aha_1980A aha_1980

                          @Nimika

                          then if I already open the slot of RS-422 so no need to reopen it through QSerialPort

                          Wrong. QSerialPort is a class wrapping O/S specific functions, so you cannot simply omit it's open() function

                          @aha_1980 Yeah I have already used it but still the same problem.

                          As said, we cannot help you if you don't use QSerialPorts functions.
                          So the only thing you need to do after open is configuring it to RS-422?

                          What is tty2?

                          You should also double check if the serial line settings are identical on both sides (bitrate, start, stop, parity, flow control).

                          N Offline
                          N Offline
                          Nimika
                          wrote on last edited by Nimika
                          #12

                          @aha_1980 I haven't used tty2.
                          But if I am opening the serial port using QSerialPort then I am unable to configure the RS 422 channel accordingly so I need to open it though what I did in the program that's the basic opening of the serial port.
                          I am setting the serial line according to Qt and it is opening when I am setting the port using QSerialPort.

                          What do you mean by this:
                          QSerialPort is a class wrapping O/S specific functions.

                          1 Reply Last reply
                          0
                          • K kuzulis

                            Seems, you don't need to open a device twice. Just try to use QSerialPort::handle() after opening to do vendor-specific IOCTL's.

                            OOPS: I'm too late.. :)

                            N Offline
                            N Offline
                            Nimika
                            wrote on last edited by
                            #13

                            @kuzulis I will try and then will let you know.

                            aha_1980A 1 Reply Last reply
                            0
                            • N Nimika

                              @kuzulis I will try and then will let you know.

                              aha_1980A Offline
                              aha_1980A Offline
                              aha_1980
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              @Nimika

                              Can you please change the code according to our suggestions and upload a minimal example again?

                              Your current example is already quite complex. Thanks.

                              Qt has to stay free or it will die.

                              N 1 Reply Last reply
                              0
                              • aha_1980A aha_1980

                                @Nimika

                                Can you please change the code according to our suggestions and upload a minimal example again?

                                Your current example is already quite complex. Thanks.

                                N Offline
                                N Offline
                                Nimika
                                wrote on last edited by
                                #15

                                @aha_1980 Sure I will. Thanks.

                                1 Reply Last reply
                                0
                                • K kuzulis

                                  Seems, you don't need to open a device twice. Just try to use QSerialPort::handle() after opening to do vendor-specific IOCTL's.

                                  OOPS: I'm too late.. :)

                                  N Offline
                                  N Offline
                                  Nimika
                                  wrote on last edited by
                                  #16

                                  @kuzulis It is not working yet. Can you help me out?

                                  aha_1980A kshegunovK 2 Replies Last reply
                                  0
                                  • N Nimika

                                    @kuzulis It is not working yet. Can you help me out?

                                    aha_1980A Offline
                                    aha_1980A Offline
                                    aha_1980
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #17

                                    @Nimika Can you please post your current code again?

                                    Qt has to stay free or it will die.

                                    1 Reply Last reply
                                    0
                                    • K Offline
                                      K Offline
                                      kuzulis
                                      Qt Champions 2020
                                      wrote on last edited by
                                      #18

                                      What does not working? I do not see any your code, sorry, but I am not a psychic.

                                      1 Reply Last reply
                                      1
                                      • N Nimika

                                        @kuzulis It is not working yet. Can you help me out?

                                        kshegunovK Offline
                                        kshegunovK Offline
                                        kshegunov
                                        Moderators
                                        wrote on last edited by
                                        #19

                                        Creating QObjects before the QCoreApplication is up and running is not supported. I imagine you get some warnings in the debug output, that need to be addressed before anything else.

                                        Read and abide by the Qt Code of Conduct

                                        aha_1980A 1 Reply Last reply
                                        4
                                        • kshegunovK kshegunov

                                          Creating QObjects before the QCoreApplication is up and running is not supported. I imagine you get some warnings in the debug output, that need to be addressed before anything else.

                                          aha_1980A Offline
                                          aha_1980A Offline
                                          aha_1980
                                          Lifetime Qt Champion
                                          wrote on last edited by
                                          #20

                                          @kshegunov

                                          By looking at the code again, I also see that QSerialPort is a global variable. That is bad also as it is created at unknown time and may be accessed before it its constructor is run.

                                          Qt has to stay free or it will die.

                                          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