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. [SLOVED]how to save \0 in my array coming from serial
Forum Updated to NodeBB v4.3 + New Features

[SLOVED]how to save \0 in my array coming from serial

Scheduled Pinned Locked Moved General and Desktop
14 Posts 4 Posters 5.0k 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.
  • D Offline
    D Offline
    Dharmendra
    wrote on last edited by
    #3

    Thank you Greetz for your Respone..
    you are absolutly correct buffer can able to hold the data upto first zero after that it is not showing any thing.
    here is my code. help how to store the data including 0x00 in between the defined array as readAll function read upto it find first zero after that it terminates .

    //---------------------------------------INITIALIZATION------------------------------------------------------//
    char String[11]={0x05,0x04,0x01,0x01,0x00,0x0A,0x71,0x89,0x00,'\0'};
    char StringOne[10]={"AB CBBB--"};
    char test[10];
    char *str;
    int i=0,a=5;
    char DATA[50]={0x05,0x04,0x00,0x00,0x00,0x30,0x71,0x89,0x00,'\0'};
    QString buffer;
    Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
    {
    ui->setupUi(this);
    ui->stringOne->setText(String);
    ui->stringTwo->setText(StringOne);
    //----------------------------------------PORTSETTING---------------------------------------------------//

    ui->portBox->addItem("ttyUSB0");
    ui->portBox->addItem("ttyUSB1");
    //make sure user can input their own port name!
    ui->portBox->setEditable(true);
    

    /--------------------------------------DATA RECEIVE SECTION----------------------------------------//
    void Dialog::onReadyRead()
    {
    if (port->bytesAvailable()) {
    int avail=port->bytesAvailable();
    ui->recvEdit->moveCursor(QTextCursor::End);
    buffer=QString::fromLatin1(port->readAll());
    ui->recvEdit->insertPlainText(buffer);
    QByteArray ba;
    ba=buffer.toLatin1();
    str=ba.data();
    for(int i=0;i<avail;i++){test[i]=str[i]+48;}
    ui->stringOne->setText(test);
    }
    }
    //--------------------------------------DATA RECEIVE SECTION----------------------------------------//

    //--------------------------------------DATA TRANSMIT SECTION-(MY SLOT)---------------------------------------------//
    void Dialog::MySlot()
    {
    if (port->isOpen()){port->write(String);}
    }
    //------------------------------------DATA TRANSMIT SECTION-(MY SLOT)----------------------------------------------//

    1 Reply Last reply
    0
    • D Offline
      D Offline
      Dharmendra
      wrote on last edited by
      #4

      //----------------------------------------------------SIGNAL SLOT CONNECTION------------------------------------------//
      connect(ui->baudRateBox, SIGNAL(currentIndexChanged(int)), SLOT(onBaudRateChanged(int)));
      connect(ui->parityBox, SIGNAL(currentIndexChanged(int)), SLOT(onParityChanged(int)));
      connect(ui->dataBitsBox, SIGNAL(currentIndexChanged(int)), SLOT(onDataBitsChanged(int)));
      connect(ui->stopBitsBox, SIGNAL(currentIndexChanged(int)), SLOT(onStopBitsChanged(int)));
      connect(ui->queryModeBox, SIGNAL(currentIndexChanged(int)), SLOT(onQueryModeChanged(int)));
      connect(ui->timeoutBox, SIGNAL(valueChanged(int)), SLOT(onTimeoutChanged(int)));
      connect(timer, SIGNAL(timeout()), SLOT(onReadyRead()));
      connect(port, SIGNAL(readyRead()), SLOT(onReadyRead()));
      connect(timer1,SIGNAL(timeout()),this,SLOT(MySlot()));
      connect(enumerator, SIGNAL(deviceDiscovered(QextPortInfo)), SLOT(onPortAddedOrRemoved()));
      connect(enumerator, SIGNAL(deviceRemoved(QextPortInfo)), SLOT(onPortAddedOrRemoved()));
      //----------------------------------------------------SIGNAL SLOT CONNECTION------------------------------------------//
      //---------------------------------------------------PORT OPENING-----------------------------------------------------//
      port->setPortName(ui->portBox->currentText());
      port->open(QIODevice::ReadWrite);
      //If using polling mode, we need a QTimer
      if (port->isOpen() && port->queryMode() == QextSerialPort::Polling)
      timer->start();
      else
      timer->stop();
      //update led's status
      ui->led->turnOn(port->isOpen());
      //---------------------------------------------------PORT OPENING-----------------------------------------------------//
      }
      Dialog::~Dialog()
      {
      delete ui;
      delete port;
      }
      void Dialog::changeEvent(QEvent *e)
      {
      QDialog::changeEvent(e);
      switch (e->type()) {
      case QEvent::LanguageChange:
      ui->retranslateUi(this);
      break;
      default:
      break;
      }
      }
      void Dialog::onBaudRateChanged(int idx)
      {
      port->setBaudRate((BaudRateType)ui->baudRateBox->itemData(idx).toInt());
      }

      void Dialog::onParityChanged(int idx)
      {
      port->setParity((ParityType)ui->parityBox->itemData(idx).toInt());
      }

      void Dialog::onDataBitsChanged(int idx)
      {
      port->setDataBits((DataBitsType)ui->dataBitsBox->itemData(idx).toInt());
      }

      void Dialog::onStopBitsChanged(int idx)
      {
      port->setStopBits((StopBitsType)ui->stopBitsBox->itemData(idx).toInt());
      }

      void Dialog::onQueryModeChanged(int idx)
      {
      port->setQueryMode((QextSerialPort::QueryMode)ui->queryModeBox->itemData(idx).toInt());
      }

      void Dialog::onTimeoutChanged(int val)
      {
      port->setTimeout(val);
      }

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

        Hi,

        Please enclose your code with coding tags (one @ at the beginning and another at the end of your code)

        Otherwise it's pretty much unreadable

        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
        • D Offline
          D Offline
          Dharmendra
          wrote on last edited by
          #6

          @ //———————————————————-INITIALIZATION———————————————————————————//
          char String11={0×05,0×04,0×01,0×01,0×00,0×0A,0×71,0×89,0×00,’\0’};
          char StringOne10={“AB CBBB—”};
          char test10;
          char *str;
          int i=0,a=5;
          char DATA50={0×05,0×04,0×00,0×00,0×00,0×30,0×71,0×89,0×00,’\0’};
          QString buffer;
          Dialog::Dialog(QWidget *parent) :
          QDialog(parent), ui(new Ui::Dialog)
          { ui->setupUi(this); ui->stringOne->setText(String); ui->stringTwo->setText(StringOne); //————————————————————PORTSETTING—————————————————————————-//
          ui->portBox->addItem(“ttyUSB0”); ui->portBox->addItem(“ttyUSB1”); //make sure user can input their own port name! ui->portBox->setEditable(true);

          /———————————————————DATA RECEIVE SECTION————————————————————//
          void Dialog::onReadyRead()
          { if (port->bytesAvailable()) { int avail=port->bytesAvailable(); ui->recvEdit->moveCursor(QTextCursor::End); buffer=QString::fromLatin1(port->readAll()); ui->recvEdit->insertPlainText(buffer); QByteArray ba; ba=buffer.toLatin1(); str=ba.data(); for(int i=0;i<avail;i++){test[i]=str[i]+48;} ui->stringOne->setText(test); }
          }
          //———————————————————DATA RECEIVE SECTION————————————————————//

          //———————————————————DATA TRANSMIT SECTION-(MY SLOT)——————————————————————-//
          void Dialog::MySlot()
          { if (port->isOpen()){port->write(String);}
          }
          //——————————————————DATA TRANSMIT SECTION-(MY SLOT)———————————————————————//@

          1 Reply Last reply
          0
          • D Offline
            D Offline
            Dharmendra
            wrote on last edited by
            #7

            @

            //——————————————————————————SIGNAL SLOT CONNECTION—————————————————————// connect(ui->baudRateBox, SIGNAL), SLOT)); connect(ui->parityBox, SIGNAL), SLOT)); connect(ui->dataBitsBox, SIGNAL), SLOT)); connect(ui->stopBitsBox, SIGNAL), SLOT)); connect(ui->queryModeBox, SIGNAL), SLOT)); connect(ui->timeoutBox, SIGNAL), SLOT)); connect(timer, SIGNAL), SLOT)); connect(port, SIGNAL), SLOT)); connect(timer1,SIGNAL),this,SLOT)); connect(enumerator, SIGNAL), SLOT)); connect(enumerator, SIGNAL), SLOT));
            //——————————————————————————SIGNAL SLOT CONNECTION—————————————————————//
            //—————————————————————————-PORT OPENING——————————————————————————-// port->setPortName(ui->portBox->currentText()); port->open(QIODevice::ReadWrite); //If using polling mode, we need a QTimer if (port->isOpen() && port->queryMode() == QextSerialPort::Polling) timer->start(); else timer->stop(); //update led’s status ui->led->turnOn(port->isOpen());
            //—————————————————————————-PORT OPENING——————————————————————————-//
            }
            Dialog::~Dialog()
            { delete ui; delete port;
            }
            void Dialog::changeEvent(QEvent *e)
            { QDialog::changeEvent(e); switch (e->type()) { case QEvent::LanguageChange: ui->retranslateUi(this); break; default: break; }
            }
            void Dialog::onBaudRateChanged(int idx)
            { port->setBaudRate((BaudRateType)ui->baudRateBox->itemData(idx).toInt());
            }

            void Dialog::onParityChanged(int idx)
            { port->setParity((ParityType)ui->parityBox->itemData(idx).toInt());
            }

            void Dialog::onDataBitsChanged(int idx)
            { port->setDataBits((DataBitsType)ui->dataBitsBox->itemData(idx).toInt());
            }

            void Dialog::onStopBitsChanged(int idx)
            { port->setStopBits((StopBitsType)ui->stopBitsBox->itemData(idx).toInt());
            }

            void Dialog::onQueryModeChanged(int idx)
            { port->setQueryMode((QextSerialPort::QueryMode)ui->queryModeBox->itemData(idx).toInt());
            }

            void Dialog::onTimeoutChanged(int val)
            { port->setTimeout(val);
            }
            @

            1 Reply Last reply
            0
            • D Offline
              D Offline
              Dharmendra
              wrote on last edited by
              #8

              can any one provide some help , how to make '\0' non termination for serial communication so that i can able to retrive my complete set of data at receiver or slave end when i am transmitting from Master End.

              1 Reply Last reply
              0
              • JKSHJ Offline
                JKSHJ Offline
                JKSH
                Moderators
                wrote on last edited by
                #9

                You can't use strings, because 0x00 == '\0' is a termination character. Use a raw QByteArray.

                Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  Dharmendra
                  wrote on last edited by
                  #10

                  when My String is :
                  @
                  char String[11]={0x05,0x04,0x01,0x01,0x01,0x0A,0x71,0x89,'\0'};
                  @
                  is this then I i can able to see on label defined and value of avail=8 and and data i can seen in my Temp_Buf;

                  but when My string is :
                  @
                  char String[11]={0x05,0x04,0x00,0x00,0x00,0x0A,0x71,0x89,0x00,'\0'};
                  @

                  then the value of avail=2; and it is get terminated by the function.
                  @

                  void Dialog::onReadyRead()
                  {
                  int avail = port->readData(Temp_Buf,256);

                  for(int i=0;i<avail;i++)
                  {test[i]=Temp_Buf[i]+48;}
                  ui->stringOne->setText(test);
                      ui->stringTwo->setNum(avail);
                  

                  }
                  @

                  When I tried diffrently :
                  @
                  void Dialog::onReadyRead()
                  {
                  if (port->bytesAvailable())
                  {
                  int avail=port->bytesAvailable();
                  QByteArray data = port->readAll();
                  ui->recvEdit->insel);rtPlainText(data);
                  for(int i=0;i<8;i++)
                  {
                  test[i]=data[i]+48;
                  }
                  ui->stringOne->setText(test);
                  ui->stringTwo->setNum(avail);
                  }
                  }
                  @
                  @
                  Same thing happens fuction read all when it encounter 0 it get terminated is there any fuction which not terminate Zero or i have to modify the fuction
                  @
                  bytesAvailable();
                  @
                  because when i am reading bytes available in case of array contating zero it terminates but in case of array contating non-zero show complete value.

                  Thank you

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

                    As said by JKSH the 0x00 is equivalent to '\0' which terminates a string (it's not QString specific)

                    If you only want to show the values from the byte array you could consider the hexadecimal representation. Otherwise you have to parse your byte array and translate it to something that can be shown in a string.

                    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
                    • D Offline
                      D Offline
                      Dharmendra
                      wrote on last edited by
                      #12

                      Sir ,
                      Actully From MODBUS protocol i will be receving same pattern
                      as an array named String ;
                      As I am in problem i know if it getting 0 it will terminate

                      @
                      char String[11]={0x05,0x04,0x00,0x00,0x00,0x0A,0x71,0x89,0x00,'\0'};

                      int avail =port->bytesAvailable(); // is also providing the value upto it 
                      

                      ///My Updated Code is Code is :

                      void Dialog::onReadyRead()
                      {
                       
                          if (port->bytesAvailable())
                       {
                             int avail=port->bytesAvailable();
                      
                             QByteArray buf = port->readAll();
                      
                             for(int i=0;i<avail;i++)
                             {
                                  test[i]=buf[i]+48;
                              }
                             ui->stringOne->setText(test);
                              ui->stringTwo->setNum(avail);
                          }
                      }
                      

                      @
                      /======/code for transmitting------//
                      @
                      void Dialog::MySlot()
                      {
                      if (port->isOpen()){port->write(String);
                      }
                      // I am transmitting it from RS 232 and making Rx-Tx short for testing -
                      @
                      Now i am not dealing with Qstring.
                      Just Handling QByteArray buf array to represent the data I received and and make a conversion to make it visible.

                      if you help in some coding pls do so...need help
                      And if --- > Raw QByteArray is solution then pls provide some example I will be very thankfull to you..
                      till now i have tried with readData , readall() ,readLine in all cases the problem is same.

                      Thankyou.

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

                        If you are working with hexadecimal values you can use QByteArray::toHex() to show the byte array content.

                        Don't use write(const char *), as the documentation states, it's for null terminated strings. Use write(const char *, int), and give it the size of your String variable.

                        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
                        • D Offline
                          D Offline
                          Dharmendra
                          wrote on last edited by
                          #14

                          it working fine by using
                          @
                          QByteArray data = QByteArray::fromRawData(port->readAll(),sizeof(port->readAll()));
                          @

                          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