By the QObject::Connect on line 43
#include "serialport.h"
#include <QSerialPort>
#include <QSerialPortInfo>
#include <string>
#include <QDebug>
#include <QQuickView>
#include <QQmlApplicationEngine>
#include <QApplication>
#include <QtQuickWidgets/QQuickWidget>
SerialPort
::SerialPort(QObject *parent
){
arduino = new QSerialPort(this);
serialBuffer = "";
parsed_data = "";
oil_pressure_volt = 0.0;
bool arduino_is_available = false;
foreach(const QSerialPortInfo &serialPortInfo, QSerialPortInfo::availablePorts()){
// check if the serialport has both a product identifier and a vendor identifier
if(serialPortInfo.hasProductIdentifier() && serialPortInfo.hasVendorIdentifier()){
// check if the product ID and the vendor ID match those of the arduino uno
if((serialPortInfo.productIdentifier() == arduino_uno_product_id)
&& (serialPortInfo.vendorIdentifier() == arduino_uno_vendor_id)){
arduino_is_available = true; // arduino uno is available on this port
arduino_uno_port_name = serialPortInfo.portName();
}
}
}
if(arduino_is_available){
qDebug() << "Found the arduino port...\n";
arduino->setPortName(arduino_uno_port_name);
arduino->open(QSerialPort::ReadOnly);
arduino->setBaudRate(QSerialPort::Baud57600);
arduino->setDataBits(QSerialPort::Data8);
arduino->setFlowControl(QSerialPort::NoFlowControl);
arduino->setParity(QSerialPort::NoParity);
arduino->setStopBits(QSerialPort::OneStop);
QObject::connect(arduino,
SIGNAL(readyRead
()),
this,
SLOT(analogRead2
()));
}else{
qDebug() << "Couldn't find the correct port for the arduino.\n";
//QMessageBox::information(this, "Serial Port Error", "Couldn't open serial port to arduino.");
}
}
void SerialPort::analogRead2()
{
if(buffer_split.length() < 3)
{
serialData = arduino->readAll();
serialBuffer
= serialBuffer
+ QString::fromStdString(serialData.
toStdString());
serialData.clear();
}else{
serialBuffer = "";
qDebug() << buffer_split << "\n";
parsed_data = buffer_split[1];
oil_pressure_volt = (0.0048) * (parsed_data.toDouble());
//set_oil_pressure_volt((0.0048) * (parsed_data.toDouble()));
qDebug() << "Pressure: " << oil_pressure_volt << "\n";
}
}
void SerialPort
::updateOilPressure(QString sensor_reading
) {
}
#include "serialport.h"
#include <QSerialPort>
#include <QSerialPortInfo>
#include <string>
#include <QDebug>
#include <QQuickView>
#include <QQmlApplicationEngine>
#include <QApplication>
#include <QtQuickWidgets/QQuickWidget>
SerialPort::SerialPort(QObject *parent)
{
arduino = new QSerialPort(this);
serialBuffer = "";
parsed_data = "";
oil_pressure_volt = 0.0;
bool arduino_is_available = false;
QString arduino_uno_port_name;
foreach(const QSerialPortInfo &serialPortInfo, QSerialPortInfo::availablePorts()){
// check if the serialport has both a product identifier and a vendor identifier
if(serialPortInfo.hasProductIdentifier() && serialPortInfo.hasVendorIdentifier()){
// check if the product ID and the vendor ID match those of the arduino uno
if((serialPortInfo.productIdentifier() == arduino_uno_product_id)
&& (serialPortInfo.vendorIdentifier() == arduino_uno_vendor_id)){
arduino_is_available = true; // arduino uno is available on this port
arduino_uno_port_name = serialPortInfo.portName();
}
}
}
if(arduino_is_available){
qDebug() << "Found the arduino port...\n";
arduino->setPortName(arduino_uno_port_name);
arduino->open(QSerialPort::ReadOnly);
arduino->setBaudRate(QSerialPort::Baud57600);
arduino->setDataBits(QSerialPort::Data8);
arduino->setFlowControl(QSerialPort::NoFlowControl);
arduino->setParity(QSerialPort::NoParity);
arduino->setStopBits(QSerialPort::OneStop);
QObject::connect(arduino, SIGNAL(readyRead()), this, SLOT(analogRead2()));
}else{
qDebug() << "Couldn't find the correct port for the arduino.\n";
//QMessageBox::information(this, "Serial Port Error", "Couldn't open serial port to arduino.");
}
}
void SerialPort::analogRead2()
{
QStringList buffer_split = serialBuffer.split(",");
if(buffer_split.length() < 3)
{
serialData = arduino->readAll();
serialBuffer = serialBuffer + QString::fromStdString(serialData.toStdString());
serialData.clear();
}else{
serialBuffer = "";
qDebug() << buffer_split << "\n";
parsed_data = buffer_split[1];
oil_pressure_volt = (0.0048) * (parsed_data.toDouble());
//set_oil_pressure_volt((0.0048) * (parsed_data.toDouble()));
qDebug() << "Pressure: " << oil_pressure_volt << "\n";
}
}
void SerialPort::updateOilPressure(QString sensor_reading)
{
}
To copy to clipboard, switch view to plain text mode
Bookmarks