<commit_before>#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
	// show MainWindow (GUI)
	ui->setupUi(this);

	// display message in GUI
	ui->textEdit->insertHtml("Scanning for serial ports... ");

	// get a list of available serial ports.
	// this is not used in the code and only for demontration.
	QList <QextPortInfo> ports = QextSerialEnumerator::getPorts();

	ui->textEdit->insertHtml("Done.<br><br>");

	// for displaying the number of found ports
	n = 1;

	foreach (QextPortInfo portInfo, ports)
	{
		// display found ports in GUI
		ui->textEdit->insertHtml(QString("<b><u>Port %1</u></b><br>").arg(n));

		showPorts(portInfo);

		// n plus 1
		n++;
	}

	ui->textEdit->insertHtml("<br><br>");

	// if a USB device is added or removed, call the Slot onPortAddedOrRemoved
	enumerator = new QextSerialEnumerator(this);
	enumerator->setUpNotifications();

	connect(enumerator, SIGNAL(deviceDiscovered(QextPortInfo)), SLOT(onPortAdded(QextPortInfo)));
	connect(enumerator, SIGNAL(deviceRemoved(QextPortInfo)), SLOT(onPortRemoved(QextPortInfo)));

	//--------------------------------------------------------------------------------------------------
	// the settings for the serial port
	// Be aware, that the Arduino has to use the same speed (9600 Baud)
	PortSettings settings = {BAUD9600, DATA_8, PAR_NONE, STOP_1, FLOW_OFF, 10}; // 10 = timeout in ms
	//--------------------------------------------------------------------------------------------------

	//--------------------------------------------------------------------------------------------------
	// the name of the serial port
	// on Windows, this would be i.e. COM5
	serialPortName = "/dev/tty.usbmodemfd1411";
	//--------------------------------------------------------------------------------------------------

	// create the serial port object.
	// we get the serial data on the port asynchronously!
	port = new QextSerialPort(serialPortName, settings);

	// try to open Arduino serial port
	initArduino();
}


MainWindow::~MainWindow()
{
	delete ui;
	delete port;
}


void MainWindow::timerSlot()
{
	QTime startTime; // For measuring elapsed time while waiting for an answer on the serial port
	qint64 ba = 0; // bytes available on the serial port
	QByteArray receivedData; // the data received from the serial port
	QString str;  // a string to show the received data
	QChar ch = 0; // the char of the received data
	int dec = 0;  // the int of the received data


	// send values to Arduino (insert your own initialisation here!)
	sendValue('*');
	sendValue('r');
	sendValue('e');
	sendValue('#');

	ui->textEdit->insertHtml("<br><b>Waiting for Arduino answer...</b><br><br>");

	// just to make sure...
	if (port->isOpen() == false)
	{
		ui->textEdit->insertHtml("ERROR: serial port not opened!");

		return;
	}

	// check if the Arduino sends all data within an wanted time...
	startTime.start();

	do
	{
		// how many bytes are available?
		ba = port->bytesAvailable();

		// position in the string (index!)
		n = 0;

		// if data available (should _always_ be the case, since this method is called automatically by an event)
		if (ba > 0)
		{
			// read data and convert them to a QString
			receivedData = port->readAll();

			// convert from QByteArray to QString
			str = QString::fromUtf8(receivedData.constData());

			// show received data as QString
			ui->textEdit->insertHtml(QString("<em>%1 byte(s) received. ASCII: %2</em><br>").arg(ba).arg(str));

			// show each byte
			while (n < receivedData.length())
			{
				// show DEC of each char
				//
				// convert one byte to QChar
				ch = receivedData.at(n);

				// convert to int
				dec = (int) ch.toAscii();

				// show in GUI
				ui->textEdit->insertHtml(QString("Byte No.%1: %2 (DEC)<br>").arg(n+1).arg(dec));

				// counter +1
				n++;
			}
		}
	} while (startTime.elapsed() < serialReadTimout);
}


void MainWindow::initArduino()<commit_msg>Test with read method instead of readAll - crashes.<commit_after>#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
	// show MainWindow (GUI)
	ui->setupUi(this);

	// display message in GUI
	ui->textEdit->insertHtml("Scanning for serial ports... ");

	// get a list of available serial ports.
	// this is not used in the code and only for demontration.
	QList <QextPortInfo> ports = QextSerialEnumerator::getPorts();

	ui->textEdit->insertHtml("Done.<br><br>");

	// for displaying the number of found ports
	n = 1;

	foreach (QextPortInfo portInfo, ports)
	{
		// display found ports in GUI
		ui->textEdit->insertHtml(QString("<b><u>Port %1</u></b><br>").arg(n));

		showPorts(portInfo);

		// n plus 1
		n++;
	}

	ui->textEdit->insertHtml("<br><br>");

	// if a USB device is added or removed, call the Slot onPortAddedOrRemoved
	enumerator = new QextSerialEnumerator(this);
	enumerator->setUpNotifications();

	connect(enumerator, SIGNAL(deviceDiscovered(QextPortInfo)), SLOT(onPortAdded(QextPortInfo)));
	connect(enumerator, SIGNAL(deviceRemoved(QextPortInfo)), SLOT(onPortRemoved(QextPortInfo)));

	//--------------------------------------------------------------------------------------------------
	// the settings for the serial port
	// Be aware, that the Arduino has to use the same speed (9600 Baud)
	PortSettings settings = {BAUD9600, DATA_8, PAR_NONE, STOP_1, FLOW_OFF, 10}; // 10 = timeout in ms
	//--------------------------------------------------------------------------------------------------

	//--------------------------------------------------------------------------------------------------
	// the name of the serial port
	// on Windows, this would be i.e. COM5
	serialPortName = "/dev/tty.usbmodemfd1411";
	//--------------------------------------------------------------------------------------------------

	// create the serial port object.
	// we get the serial data on the port asynchronously!
	port = new QextSerialPort(serialPortName, settings);

	// try to open Arduino serial port
	initArduino();
}


MainWindow::~MainWindow()
{
	delete ui;
	delete port;
}


void MainWindow::timerSlot()
{
	QTime startTime; // For measuring elapsed time while waiting for an answer on the serial port
	qint64 ba = 0; // bytes available on the serial port
	QByteArray receivedData; // the data received from the serial port
	QString str;  // a string to show the received data
	QChar ch = 0; // the char of the received data
	int dec = 0;  // the int of the received data

	char buf[1024];



	// send values to Arduino (insert your own initialisation here!)
	sendValue('*');
	sendValue('r');
	sendValue('e');
	sendValue('#');

	ui->textEdit->insertHtml("<br><b>Waiting for Arduino answer...</b><br><br>");

	// just to make sure...
	if (port->isOpen() == false)
	{
		ui->textEdit->insertHtml("ERROR: serial port not opened!");

		return;
	}

	// check if the Arduino sends all data within an wanted time...
	startTime.start();

	do
	{
		// how many bytes are available?
		ba = port->bytesAvailable();

		// position in the string (index!)
		n = 0;

		// if data available (should _always_ be the case, since this method is called automatically by an event)
		if (ba > 0)
		{
			// read available bytes as char *
			n = port->read(buf, ba);

			// ERROR
			if ((n < ba) || (n == -1))
			{
				// get the error code
				n = port->lastError();

				// show error code and message
				ui->textEdit->insertHtml(QString("ERROR %1 at readData: %2").arg(n).arg(port->errorString()));

				return;
			}

			// convert from char * to QBytearray  -  just for convenience
			receivedData = QByteArray::fromRawData(buf, sizeof(buf));

			// convert from QByteArray to QString  -  just for convenience
			str = QString::fromUtf8(receivedData.constData());

			// show received data as QString
			ui->textEdit->insertHtml(QString("<em>%1 byte(s) received. ASCII: %2</em><br>").arg(ba).arg(str));

			// show each byte
			while (n < receivedData.length())
			{
				// show DEC of each char
				//
				// convert one byte to QChar
				ch = receivedData.at(n);

				// convert to int
				dec = (int) ch.toAscii();

				// show in GUI
				ui->textEdit->insertHtml(QString("Byte No.%1: %2 (DEC)<br>").arg(n+1).arg(dec));

				// counter +1
				n++;
			}
		}
	} while (startTime.elapsed() < serialReadTimout);
}


void MainWindow::initArduino()
{'fraction_non_alphanumeric': 0.2222222222222222, 'fraction_numerical': 0.009387997623291741, 'mean_word_length': 2.370444533440128, 'pattern_counts': {'":': 0, '<': 46, '<?xml version=': 0, '>': 89, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4201187', 'n_tokens_mistral': 2807, 'n_tokens_neox': 2503, 'n_words': 789}