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. Serial port
Forum Updated to NodeBB v4.3 + New Features

Serial port

Scheduled Pinned Locked Moved General and Desktop
37 Posts 6 Posters 17.1k 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.
  • S Offline
    S Offline
    shirisha
    wrote on last edited by
    #1

    Hi sir,
    am unable to write datya using serial port in qt.Please help me

    1 Reply Last reply
    0
    • S Offline
      S Offline
      shirisha
      wrote on last edited by
      #2

      @
      #include <QCoreApplication>
      #include <QDebug>
      #include <iostream>

      #include <sys/types.h>
      #include <sys/stat.h>
      #include <fcntl.h>
      #include <termios.h>
      #include <stdio.h>
      #include <string.h>

      #include <stdlib.h>

      #define BAUDRATE B9600 // Change as needed, keep B

      #define MODEMDEVICE "/dev/ttyO1" //Beaglebone Black serial port

      #define _POSIX_SOURCE 1 /* POSIX compliant source */

      #define FALSE 0
      #define TRUE 1
      int nreturn = 0;

      int main(int argc, char *argv[])
      {
      QCoreApplication a(argc, argv);

      int fd, c, res;
           struct termios oldtio, newtio;
           char buf[255];
      
         //  int ret = system&#40;"echo uart1 > /sys/devices/bone_capemgr.9/slots"&#41;;
      
           fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY &#41;;
           if (fd < 0) { perror(MODEMDEVICE); exit(-1); }
      
            bzero(&newtio, sizeof(newtio)); /* clear struct for new port settings */
      
           newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
      
      
           newtio.c_iflag = IGNPAR;
      
      
                newtio.c_oflag = 0;
      
                newtio.c_lflag = ICANON;
      
                tcflush(fd, TCIFLUSH);
                tcsetattr(fd,TCSANOW,&newtio);
      
                while (TRUE) {
                 nreturn =   write(fd, "U", 1);
                 qDebug () << "write" << nreturn;
      
      
                }
                 tcsetattr(fd, TCSANOW, &oldtio);
      
      return a.exec(&#41;;
      

      }
      @

      this is the code which i need to do...recently it worked, but now it getting error has error: 'write' was not declared in this scope

      [edit: added missing coding tags @ SGaist]

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

        Hi,

        This question is not related to Qt. You should go to a Beaglebone Black forum.

        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
        0
        • S Offline
          S Offline
          shirisha
          wrote on last edited by
          #4

          hi sir,
          This is not for BBB only for qt

          1 Reply Last reply
          0
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            well you are only using qDebug.
            The rest is normal linux serial programming.
            The write that it complains about is the normal linux one,
            not anything to do with Qt.
            try including unistd.h if older linux or look in the online help for you compiler where its defined.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              shirisha
              wrote on last edited by
              #6

              Thank a lot sir..it worked

              1 Reply Last reply
              0
              • S Offline
                S Offline
                shirisha
                wrote on last edited by
                #7

                Hi sir..

                @
                #include <QCoreApplication>
                #include <QDebug>
                #include <sys/types.h>
                #include <sys/stat.h>
                #include <fcntl.h>
                #include <termios.h>
                #include <stdio.h>
                #include <stdlib.h>
                #include <sys/types.h>
                #include <unistd.h>
                #include <string.h>
                #include <sys/ioctl.h>
                #include <sys/poll.h>

                #define BAUDRATE B9600 // Change as needed, keep B

                #define MODEMDEVICE "/dev/ttyO0" //Beaglebone Black serial port

                #define _POSIX_SOURCE 1 /* POSIX compliant source */

                int nreturn = 0,ndata = 0;
                int main(int argc, char *argv[])
                {

                QCoreApplication a(argc, argv);
                int fd;
                struct termios oldtio, newtio;
                char buf,ch;
                

                QString str;
                fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY );

                 //    fcntl(fd, F_SETOWN, getpid());                   /* getting pid and owner rigts on serial port   */
                 //          fcntl(fd,F_SETFL,O_NDELAY);
                     tcgetattr(fd,&newtio);                                /* save current port settings                   */
                 //    bzero(&newtio, sizeof(newtio));
                     newtio.c_cflag = BAUDRATE | CS8 | CLOCAL | CREAD;
                   //newtio.c_iflag = IGNPAR;
                    // newtio.c_oflag = 0;                                         /* output mode flags                            */
                    //newtio.c_lflag = 0;                                         /* set input mode (non-canonical,no echo)       */
                    //newtio.c_cc[VTIME]=0;                                       /* blacking until one character arrives         */
                    //newtio.c_cc[VMIN]=1;                                        /* inter-character  timer unused                */
                     tcflush(fd, TCIFLUSH);                               /* flushes the data but not transmitted         */
                     tcsetattr(fd,TCSANOW,&newtio);
                          while (TRUE)
                                     {
                
                              nreturn = read(fd,&buf,1);
                              if(nreturn>0)
                              {
                                   ch =buf;
                                   qDebug () << "Data:" << ch;
                              }
                                       }
                                    tcsetattr(fd, TCSANOW, &oldtio);
                                    return a.exec(&#41;;
                
                 }
                

                @

                here am able to read data ,problem is i would like to use "char ch[50],buf; i.e ch = buf;
                so that i need to use for other functions like if ch[0] = 0x02 shuld display button in green color examples .so how would i equate both char.am getting errors .Please help me sir

                [edit: added missing coding tags @ SGaist]

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  shirisha
                  wrote on last edited by
                  #8

                  @
                  #include <QCoreApplication>
                  #include <QDebug>
                  #include <sys/types.h>
                  #include <sys/stat.h>
                  #include <fcntl.h>
                  #include <termios.h>
                  #include <stdio.h>
                  #include <stdlib.h>
                  #include <sys/types.h>
                  #include <unistd.h>
                  #include <string.h>
                  #include <sys/ioctl.h>
                  #include <sys/poll.h>

                  #define BAUDRATE B9600 // Change as needed, keep B

                  #define MODEMDEVICE “/dev/ttyO0” //Beaglebone Black serial port

                  #define _POSIX_SOURCE 1 /* POSIX compliant source */

                  int nreturn = 0,ndata = 0;
                  int main(int argc, char *argv[])
                  {
                  QCoreApplication a(argc, argv);
                  int fd;
                  struct termios oldtio, newtio;
                  char buf,ch;
                  QString str;
                  fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY );
                  tcgetattr(fd,&newtio);
                  newtio.c_cflag = BAUDRATE | CS8 | CLOCAL | CREAD;
                  tcflush(fd, TCIFLUSH);
                  tcsetattr(fd,TCSANOW,&newtio);

                  while (TRUE)
                  {
                  nreturn = read(fd,&buf,1);
                  if(nreturn>0)
                  {
                  ch =buf;
                  qDebug () << "Data:" << ch;
                  }
                  }
                  tcsetattr(fd, TCSANOW, &oldtio);
                  return a.exec();
                  }
                  @

                  here am able to read data ,problem is i would like to use “char ch50,buf; i.e ch = buf;
                  so that i need to use for other functions like if ch0 = 0×02 shuld display button in green color examples .so how would i equate both char.am getting errors .Please help me sir

                  [edit: added missing coding tags @ SGaist]

                  1 Reply Last reply
                  0
                  • mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    sorry but its very unreadable.
                    I have no idea what
                    ,problem is i would like to use “char ch50,buf; i.e ch = buf;

                    means.
                    I can only give you some hints:

                    why you only read 1 char ?
                    nreturn = read(fd,&buf,1)

                    read returns how many you get so:
                    @
                    char buf[2048];// now a list of char
                    nreturn = read(fd,&buf,2048)

                    then you can look in buf[0], buf[1]
                    if nreturn is 2
                    @

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

                      2 shirisha ,

                      why you just do not use QtSerialPort?

                      http://qt-project.org/doc/qt-5/qtserialport-index.html
                      http://qt-project.org/wiki/QtSerialPort

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        shirisha
                        wrote on last edited by
                        #11

                        hi sir,

                             I could n't use  #include<QtSerialPort> in qt i even included Qt+ = serialport in .pro, what could be the reason ..Please help me .
                        
                        1 Reply Last reply
                        0
                        • K Offline
                          K Offline
                          kuzulis
                          Qt Champions 2020
                          wrote on last edited by
                          #12

                          Did you read Wiki? What version of Qt do you use?

                          Please paste more informative messages, how you tried?

                          1 Reply Last reply
                          0
                          • S Offline
                            S Offline
                            shirisha
                            wrote on last edited by
                            #13

                            hi sir,

                            @
                            QChar chbuf[50];
                            QString ReadData;
                            nSerialreturn = read(fd,chbuf,50);
                            qDebug () << "Timer running:"<< nSerialreturn;
                            for(i = 0;i<nSerialreturn;i++)
                            {
                            ReadData = chbuf[i];
                            qDebug () << "Data:" << ReadData;
                            }
                            @

                            am unable to get the data..and data should be in hexadecimal ..unable to convert it ..please help me sir

                            [edit: added missing coding tags @ SGaist]

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

                              Please answer the questions we are asking you in order for us to help you.

                              Again, it seems that you are not using QSerialPort, if so, then go to the appropriate forum like I already suggested.

                              Otherwise, tell us exactly what version of Qt you are using. Show us the code where you initialize QSerialPort. Tell us what parameter your serial port should use.

                              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
                              0
                              • S Offline
                                S Offline
                                shirisha
                                wrote on last edited by
                                #15

                                hi sir,
                                am using qt 4.8.4

                                1 Reply Last reply
                                0
                                • S Offline
                                  S Offline
                                  shirisha
                                  wrote on last edited by
                                  #16
                                  nreturn = read(fd,buf,1);
                                                  str =buf[i];
                                                  len = str.length();
                                                  qDebug () << "no of bytes:" << len;
                                                  for( i=0;i<len;i++)
                                                       {
                                                             ch = str.at(i);
                                                             n+=ch.unicode();
                                                      }
                                                   qDebug () << "Data:" << n;
                                  

                                  here am getting decimal data..i would like to get hex value please help me

                                  1 Reply Last reply
                                  0
                                  • S Offline
                                    S Offline
                                    shirisha
                                    wrote on last edited by
                                    #17

                                    QSerialPort. is done only in windows ,but i linux r what...in windows am able to get theat ,bt in linux am hving problem sir
                                    ..above menstined code am able to get the data in decimal..but i want in hex data ..please help me

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

                                      QSerialPort works on all support platform so what is your exact problem with linux ?

                                      Most probably you don't have the rights to access the serial port, do you ?

                                      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
                                      0
                                      • McLionM Offline
                                        McLionM Offline
                                        McLion
                                        wrote on last edited by
                                        #19

                                        "This thread":http://qt-project.org/forums/viewthread/44793/ will guide you through my experiences when I added it to 4.6.3.
                                        If you follow all the posts, you should be able to successfully add QtSerialPort to 4.8.4 .. even easier because you may not need all the changes that were needed for 4.6.3.

                                        1 Reply Last reply
                                        0
                                        • S Offline
                                          S Offline
                                          shirisha
                                          wrote on last edited by
                                          #20

                                          thank u sir..now serial port is working , but am not able to read the hex data from serial port

                                          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