Serial.write crashes program in Qt (C++) -


i attempting write command serial port change tv input, when try write necessary command port, strange output.

#include <qcoreapplication> #include <qserialport> #include <windows.h>  int set_id = 0; int fd = -1; int main(int argc, char *argv[]) {     qcoreapplication app{argc, argv};  int timeinseconds=0; while(timeinseconds>0) {     printf("%d...\n",timeinseconds);     sleep(1000);     timeinseconds--; }   qserialport serial; serial.setbaudrate(qserialport::baud9600); serial.setdatabits(qserialport::data8); serial.setparity(qserialport::noparity); serial.setstopbits(qserialport::onestop); serial.setflowcontrol(qserialport::noflowcontrol); serial.setportname("com3");  serial.open(qiodevice::readwrite);  /*char cmd1='x'; char cmd2='b'; int value=20; char cmd[20]; int len; if (value >= 0x100)     len = sprintf(cmd, "%c%c %02x %02x %02x\r", cmd1, cmd2, set_id, value>>8, value&255); else {     len = sprintf(cmd, "%c%c %02x %02x\r", cmd1, cmd2, set_id, value); }  serial.write(cmd, len); */  char cmdhex[5]="0xc5"; serial.write(cmdhex); serial.close();  int stall; scanf("%d",&stall);  //return a.exec(); } 

the code supposed change tv input av. (starting hdmi 1) also, block commented out first attempt @ sending command.

you should study qserialport examples shipped qt. there's terminal project, interactive (nice testing ports), , cwritersync you're trying do.

here full cwritersync code:

/**************************************************************************** ** ** copyright (c) 2013 laszlo papp <lpapp@kde.org> ** contact: http://www.qt.io/licensing/ ** ** file part of qtserialport module of qt toolkit. ** ** $qt_begin_license:lgpl21$ ** commercial license usage ** licensees holding valid commercial qt licenses may use file in ** accordance commercial license agreement provided ** software or, alternatively, in accordance terms contained in ** written agreement between , qt company. licensing terms ** , conditions see http://www.qt.io/terms-conditions. further ** information use contact form @ http://www.qt.io/contact-us. ** ** gnu lesser general public license usage ** alternatively, file may used under terms of gnu lesser ** general public license version 2.1 or version 3 published free ** software foundation , appearing in file license.lgplv21 , ** license.lgplv3 included in packaging of file. please review ** following information ensure gnu lesser general public license ** requirements met: https://www.gnu.org/licenses/lgpl.html , ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. ** ** special exception, qt company gives additional ** rights. these rights described in qt company lgpl exception ** version 1.1, included in file lgpl_exception.txt in package. ** ** $qt_end_license$ ** ****************************************************************************/  #include <qtserialport/qserialport>  #include <qtextstream> #include <qcoreapplication> #include <qfile> #include <qstringlist>  qt_use_namespace  int main(int argc, char *argv[]) {     qcoreapplication coreapplication(argc, argv);     int argumentcount = qcoreapplication::arguments().size();     qstringlist argumentlist = qcoreapplication::arguments();      qtextstream standardoutput(stdout);      if (argumentcount == 1) {         standardoutput << qobject::tr("usage: %1 <serialportname> [baudrate]").arg(argumentlist.first()) << endl;         return 1;     }      qserialport serialport;     qstring serialportname = argumentlist.at(1);     serialport.setportname(serialportname);      int serialportbaudrate = (argumentcount > 2) ? argumentlist.at(2).toint() : qserialport::baud9600;     serialport.setbaudrate(serialportbaudrate);      if (!serialport.open(qiodevice::writeonly)) {         standardoutput << qobject::tr("failed open port %1, error: %2").arg(serialportname).arg(serialport.errorstring()) << endl;         return 1;     }      qfile datafile;     if (!datafile.open(stdin, qiodevice::readonly)) {         standardoutput << qobject::tr("failed open stdin reading") << endl;         return 1;     }      qbytearray writedata(datafile.readall());     datafile.close();      if (writedata.isempty()) {         standardoutput << qobject::tr("either no data available on standard input reading, or error occurred port %1, error: %2").arg(serialportname).arg(serialport.errorstring()) << endl;         return 1;     }      qint64 byteswritten = serialport.write(writedata);      if (byteswritten == -1) {         standardoutput << qobject::tr("failed write data port %1, error: %2").arg(serialportname).arg(serialport.errorstring()) << endl;         return 1;     } else if (byteswritten != writedata.size()) {         standardoutput << qobject::tr("failed write data port %1, error: %2").arg(serialportname).arg(serialport.errorstring()) << endl;         return 1;     } else if (!serialport.waitforbyteswritten(5000)) {         standardoutput << qobject::tr("operation timed out or error occurred port %1, error: %2").arg(serialportname).arg(serialport.errorstring()) << endl;         return 1;     }      standardoutput << qobject::tr("data sent port %1").arg(serialportname) << endl;      return 0; } 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -