[SOLVED]annoying RessourceError with QSerialPort
-
I have written a program that´s supposed to be able to autoconnect to plugged Arduinos.
Therefore it scans the available Serial Ports for Usb to serial converters manufactured by FTDI.
My system: Windows 7 32bit, Qt 5.1.1Code: https://github.com/thunderbug1/Arduino_Communication
Now the point is that if I connect an Arduino to the computer and try to connect I always get a Ressource Error but If I connect the arduino and first of all connect to the port via Hterm (a serial console) before connecting to the port via my program everything works like a charm as long as I dont unplug and reconnect the arduino to the pc. I have tried several things with the serial port in QT ( reset,clear,flush,clearError) before connecting to the port but the problem stays.
So as it seems it is some kind of Initialization-Bug of QSerialPort but I´m not sure. -
Hi.
You shall give the minimum example of the project which reproduces a problem. Because I have no desire to understand your provided code.
May you reproduce a problem with the Terminal example (from QtSerialPort package)?
In common, the ResourceError is triggered when occurs an unexpected event from an serial port handle.
E.g. when EV_TXEMPTY - in any case or EV_RXCHAR with an empty FIFO buffer. So, these scenarios are regarded as resource error.
-
Now I found out that the Ressource Error was just triggered because of my autoTimeout function that closed the port because no answer comes back.
So the actual problem is that if the arduino is plugged in and I connect I can´t receive anything with my QT program as long as I don´t connect to the port with another program before it.
You´re right here is the simple example:
mainwindow.h:
@#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include <QtCore>
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>namespace Ui {
class MainWindow;
}class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();void send(QString string); void connectTo(QString port, int baud);
public slots:
void dataAvailable();
private:
Ui::MainWindow *ui;QSerialPort serialPort;
};
#endif // MAINWINDOW_H
@
mainwindow.cpp:
@#include "mainwindow.h"
#include "ui_mainwindow.h"void MainWindow::connectTo(QString port,int baud)
{
serialPort.setPortName(port);
serialPort.setBaudRate(baud);
if(serialPort.open(QIODevice::ReadWrite))
{
qDebug()<<"connected to: "<<port;
}
}void MainWindow::send(QString string)
{
qDebug()<<"sending: "<<string;
string.append('\n'); //the arduino needs this to know that the command is finished
serialPort.write(string.toStdString().c_str());}
void MainWindow::dataAvailable()
{
if(serialPort.canReadLine())
{
QString tmp = serialPort.readLine(1000);
qDebug()<<"received: "<<tmp;
}
}MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);connect(&serialPort,SIGNAL(readyRead()),this,SLOT(dataAvailable())); QString port = "COM4"; connectTo(port,115200); //Sleep(1500); send("I");
}
MainWindow::~MainWindow()
{
delete ui;
}
@the arduino code would be this but I don´t think that this is the reason:
@
// comm variables
const int MAX_CMD_SIZE = 256;
char buffer[MAX_CMD_SIZE]; // buffer for serial commands
char serial_char; // value for each byte read in from serial comms
int serial_count = 0; // current length of command
char *strchr_pointer; // just a pointer to find chars in the cmd string like X, Y, Z, E, etc
// end comm variablesvoid setup()
{
Serial.begin(115200);Serial.print("EBB 1.0\n\0"); clear_buffer();
}
void loop() // input loop, looks for manual input and then checks to see if and serial commands are coming in
{
get_command(); // check for Gcodes
}void get_command() // gets commands from serial connection and then calls up subsequent functions to deal with them
{
if (Serial.available() > 0) // each time we see something
{
serial_char = Serial.read(); // read individual byte from serial connectionif (serial_char == '\n' || serial_char == '\r') // end of a command character { buffer[serial_count]=0; process_commands(buffer, serial_count); clear_buffer(); //Serial.write("process: command"); } else // not end of command { buffer[serial_count] = serial_char; // add byte to buffer string serial_count++; if (serial_count > MAX_CMD_SIZE) // overflow, dump and restart { clear_buffer(); Serial.flush(); } } }
}
void clear_buffer() // empties command buffer from serial connection
{
serial_count = 0; // reset buffer placement
}boolean getValue(char key, char command[], double* value)
{
// find key parameter
strchr_pointer = strchr(buffer, key);
if (strchr_pointer != NULL) // We found a key value
{
*value = (double)strtod(&command[strchr_pointer - command + 1], NULL);
return true;
}
return false;
}void process_commands(char command[], int command_length) // deals with standardized input from serial connection
{
if(command_length>0 && command[0] == 'I')
{
Serial.print("ID:1\n");
}}
@ -
You have a wrong initialization sequence (please read documentation):
@
serialPort.setPortName(port);
serialPort.setBaudRate(baud);
if(serialPort.open(QIODevice::ReadWrite))
{
qDebug()<<"connected to: "<<port;
}
@should be like:
@
serialPort.setPortName(port);
if(serialPort.open(QIODevice::ReadWrite))
{
if (serialPort.setBaudRate(baud)) {
qDebug()<<"connected to: "<<port;
}
}
@btw, need to setup and another port properties, like DataBits, StopBits, Parity, FlowControl.
You can use the Terminal example from an /examples/serialport/terminal/
-
Hey it works now :D
Thank you guys for the great Help ;)
PS: Another Thing that I had to add was a delay of the ID request in my code, because if you connect to the arduino´s port the arduino is restarted and it needs some time to boot. If your request is sent during this boot up you also get no answer. Just in case someone else is struggling with that.
THANKS