Results 1 to 2 of 2

Thread: SerialPort Connection Issue

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,323
    Thanks
    316
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: SerialPort Connection Issue

    serial : new QSerialPort(this);
    I am not sure what you think this is supposed to do. It isn't assigning anything to the global variable "serial" you already defined.

    It is actually defining a labeled statement (named "serial"), followed by a line of code which creates a new QSerialPort instance that immediately gets turned into a zombie because the return value from the new() operator isn't saved anywhere.

    Global variables are considered a bad thing in C++. You should define "serial" as a member variable of the MainWindow class.

    if(serial.open(QIODevice::ReadWrite)){
    QMessageBox::information(this, "New Box", "Baglandi");
    }

    if (serial.open(QSerialPort::ReadOnly))
    {
    Either you open it read / write or you open it read only. This code tries to do both. If opening for read / write succeeds, then the next clause will fail, because it tries to open an already open port using a different mode.

    Have you looked at the Qt Serial Port examples?
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  2. The following user says thank you to d_stranz for this useful post:

    akincio (22nd February 2021)

Similar Threads

  1. Use newest Qt5 serialport library in Qt4?
    By nyaruko in forum Qt Programming
    Replies: 1
    Last Post: 13th November 2014, 00:03
  2. QtSerialport Inconsistant Connection Issue
    By awpitt13 in forum Qt Programming
    Replies: 1
    Last Post: 11th December 2013, 07:14
  3. Communication via SerialPort
    By porterneon in forum Newbie
    Replies: 1
    Last Post: 2nd August 2011, 23:34
  4. Replies: 1
    Last Post: 2nd April 2010, 06:42
  5. qt4 serialport
    By nmn in forum Qt Programming
    Replies: 3
    Last Post: 27th November 2006, 03:55

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.