Click here to Skip to main content
12,077,332 members (43,433 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C++ QT Arduino serialport
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
#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
#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[^]
Posted 2-Apr-12 10:19am
Tiemer128
Comments
pwasser 2-Apr-12 19:27pm
   
There is insufficient information here. This is not a forum to debug your code.
Have you used a debugger to step through this program and find out exactly where it is crashing.
Tiemer 3-Apr-12 17:00pm
   
I'm sorry if there is insufficient information. I thought that the screenshot pointed out where it crashes with a green arrow.

I think it crashes when it tries to open the port.

This is an example of which I found several alike on the internet, so I'm hoping that somebody who tried to use the same code can point out what's wrong with it...

If I can clearify my question even more, feel free to say so.
pwasser 3-Apr-12 18:18pm
   
Set a breakpoint at the first line of init_port() and step through.
Is port ever assigned a valid handle?

I would do something like:

port = NULL

port = new QextSerialPort("COM4"); //we create the port


if(port}
{

}
else
GetLastError()

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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());

This is dangerous code.

It makes assumption COM4 exists and is not in use.

Also port->isOpen test is ignored.

It would be better to write:

port = NULL;
 
port = new QextSerialPort("COM4"); //we create the port

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

    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);
     }
	else
    {
 
       QMessageBox::warning(this, "port error", "Impossible to open the port!");
     }
 
}
else
 GetError()....

init_port() should not return void but an error value.
  Permalink  
Comments
Tiemer 5-Apr-12 11:31am
   
It still crashes as it did before. Anyway, thank you, I'm gonna leave it be for now because my thesis deadline is approaching and this is only an extra feature I wanted to add. Hopefully I'll find the time to add this part too.
(a double joystick controller for a delta robot I'm designing)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web03 | 2.8.160212.1 | Last Updated 3 Apr 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100