Click here to Skip to main content
15,892,269 members

Qt and serial communication - unidentified crash

Tiemer asked:

Open original thread
I've written an application which sends a value (later this will be the value from QTextEdit) to an Arduino by clicking a button. I'm able to build it, but when I run the application, it crashes...

I can't seem to find the problem...

Thanks in advance!

main.cpp
C++
#include "serialcommunication.h"
#include <QtGui>
#include <qextserialport.h>

int main(int argc, char *argv[])
{
	QApplication a(argc, argv);
	SerialCommunication w;
	w.show();
	return a.exec();
}


serialcommunication.h
C++
#ifndef SERIALCOMMUNICATION_H
#define SERIALCOMMUNICATION_H

#include <QtGui>
#include "ui_serialcommunication.h"
#include <qextserialport.h>

class SerialCommunication : public QWidget
{
	Q_OBJECT

public:
	SerialCommunication();
	~SerialCommunication();
	QextSerialPort *port;
	void init_port();
	void transmitCmd(int value);

private slots:
	void clickclick();

private:
	Ui::SerialCommunicationClass ui;

	QTextEdit *textedit;
	QPushButton *pushbutton;
};

#endif // SERIALCOMMUNICATION_H


serialcommunication.cpp
<pre lang="c++">#include "serialcommunication.h"

SerialCommunication::SerialCommunication():QWidget()
{
	ui.setupUi(this);

	textedit = new QTextEdit;
	pushbutton = new QPushButton("PUSHBUTTON");

	init_port();

	connect(pushbutton, SIGNAL(clicked()), this, SLOT(clickclick()));

	QVBoxLayout *layout = new QVBoxLayout;
	layout->addWidget(textedit);
	layout->addWidget(pushbutton);

	setLayout(layout);
}

SerialCommunication::~SerialCommunication()
{
	
}

void SerialCommunication::clickclick()
{
	transmitCmd(123);	
}

void SerialCommunication::init_port()
{
    port = new QextSerialPort("COM4"); //we create the port

	port->open(QIODevice::ReadWrite | QIODevice::Unbuffered); //we open the port

    if(!port->isOpen())
    {
        QMessageBox::warning(this, "port error", "Impossible to open the port!");
    }
	else if(port->isOpen());

    //we set the port properties
    port->setBaudRate(BAUD9600);
    port->setFlowControl(FLOW_OFF);
    port->setParity(PAR_NONE);
    port->setDataBits(DATA_8);
    port->setStopBits(STOP_1);
}

void SerialCommunication::transmitCmd(int value)
{
  if(value < 0 || value >255)return; //if the value is not between 0 and 255
                                     //no transmission

  char *buf; //creation of a buffer
  *buf = value;

  //for messages longer than 1 character :
  /*
  char *buf[255]; //buffer size = 255char;
  sprintf(buf,"%c",value);
  */

  port->write(buf); //send the buffer
}


screenshot error
http://dl.dropbox.com/u/12844842/error.JPG[^]
Tags: C++, Arduino, QT, Serial port

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900