#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
udpSocket
= new QUdpSocket(this);
//create UDP Socket(tx) rxUdpSocket
= new QUdpSocket(this);
//create UDP Socket(rx) rxUdpSocket->bind(12345); //found in book
connect(rxUdpSocket, SIGNAL(readyRead()), this, SLOT(processPendingDatagrams()));
findHostIP();
//Set Tab order for UI
setTabOrder(ui->rxIPLE, ui->portLE);
setTabOrder(ui->portLE, ui->rxPortLE);
setTabOrder(ui->rxPortLE, ui->dsbMaxDepth);
setTabOrder(ui->dsbMaxDepth,ui->configButton);
setTabOrder(ui->configButton, ui->packetLE);
setTabOrder(ui->packetLE, ui->sendButton);
//Setup Qwt Stuff
ui->plotDepth->setAxisAutoScale(2, false); //turn off y axis auto scale
ui->plotDepth->setAxisScale(0,-4.0,0,0.5); //set y axis scale
ui->plotDepth->setAutoReplot(true); //automatically update the plot
ui
->plotDepth
->setAxisTitle
(QwtPlot::xBottom,
"Time in Seconds");
ui
->plotDepth
->setAxisTitle
(QwtPlot::yLeft,
"Depth in Meters");
ui->plotDepth->setAxisMaxMinor(2, 30); //x axis minor ticks = 30
myTime
= new QTime;
//QTime for axis myTime->start();
myCurve
= new QwtPlotCurve;
//create the curve for the graph myCurve->attach(ui->plotDepth); //connect the curve to the graph
index = 0; //the index holds the number of points
//Setup the colors of the compass
QPalette *colorGroup
= new QPalette(Qt
::lightGray, Qt
::white, Qt
::white, Qt
::white, Qt
::white, Qt
::white, Qt
::white,
QColor::QColor(65,
105,
225,
255), Qt
::white);
ui->compass->setLineWidth(0);
ui->compass->setScaleTicks(0, 0, 3);
//Create tick marks on the compass
QMap<double, QString> map;
for ( double d = 0.0; d < 360.0; d += 30.0 )
{
label.sprintf("%.0f", d);
map.insert(d, label);
}
ui->compass->setLabelMap(map);
ui->compass->setScale(36, 5, 0);
//Setup the needle on the compass
ui->compass->setPalette(*colorGroup);
ui->compass->setOrigin(270.0);
ui->compass->setValue(0.0);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_sendButton_clicked()
{
QString packet
= ui
->packetLE
->text
();
//create packet QByteArray myDatagram
= packet.
toUtf8();
//convert to datagram
if(userIP == true)
{
udpSocket->writeDatagram(myDatagram.data(), myDatagram.size(),
txIP, rxPort);
}
else
{
udpSocket->writeDatagram(myDatagram.data(), myDatagram.size(),
}
}
void MainWindow::on_configButton_clicked()
{
rxPort = ui->portLE->text().toInt(); //set port
listenPort = ui->rxPortLE->text().toInt(); //set port to listen on
rxUdpSocket->close();
rxUdpSocket->bind(listenPort); //found in book
if(ui->rxIPLE->text() != NULL) //if user added IP address
{
QString txStrIp
= ui
->rxIPLE
->text
();
//create RX IP string txIP = txStrIp; //Convert to IP Address
userIP = true;
}
else
{
userIP = false;
}
ui->barDepth->setMaximum(int(ui->dsbMaxDepth->value()*100));
ui->plotDepth->setAxisScale(0,-1.0*ui->dsbMaxDepth->value(),0,0.5); //set y axis scale
}
void MainWindow::findHostIP() //found this code online, modified it to only show IP addresses (local)
{
ui->listWidget->clear();
{
{
if ( interface.hardwareAddress() != "00:00:00:00:00:00" && entry.ip().toString().contains("."))
items << interface.name() + " " + entry.ip().toString();
}
}
ui->listWidget->addItems(items);
}
void MainWindow::processPendingDatagrams()
{
do
{
rxDatagram.resize(rxUdpSocket->pendingDatagramSize());
rxUdpSocket->readDatagram(rxDatagram.data(), rxDatagram.size());
}
while(rxUdpSocket->hasPendingDatagrams());
rxString = (tr("\"%1\"").arg(rxDatagram.data())); //turn datagram into string
rxString.
remove(QChar('"'), Qt
::CaseInsensitive);
//remove quotation marks //ui->rxPacketLabel->setText(tr("\"%1\"").arg(rxDatagram.data()));
ui->rxPacketLabel->setText(rxString);
packetStream >> depthReadout >> voltsReadout >> ampsReadout >> tempReadout >> headingReadout >> otherReadout;
ui->lcdDepth->display(depthReadout);
ui->barDepth->setValue(depthReadout * 100);
ui->lcdOther->display(otherReadout);
ui->lcdVolts->display(voltsReadout);
ui->lcdAmps->display(ampsReadout);
ui->lcdTemp->display(tempReadout);
ui
->leHeading
->setText
(QString::number(headingReadout
));
ui->barHeading->setValue(headingReadout*10);
//Qwt stuff
myPoints[index].setX(myTime->elapsed());
myPoints[index].setY(-1.0*depthReadout);
index++;
myCurve->setSamples(myPoints);
myCurve
->setRenderHint
(QwtPlotItem::RenderAntialiased,
true);
ui->plotDepth->setAxisScale(2, myTime->elapsed()-10000, myTime->elapsed(), 60000);
ui->compass->setValue(headingReadout);
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
udpSocket = new QUdpSocket(this); //create UDP Socket(tx)
rxUdpSocket = new QUdpSocket(this); //create UDP Socket(rx)
rxUdpSocket->bind(12345); //found in book
connect(rxUdpSocket, SIGNAL(readyRead()), this, SLOT(processPendingDatagrams()));
findHostIP();
//Set Tab order for UI
setTabOrder(ui->rxIPLE, ui->portLE);
setTabOrder(ui->portLE, ui->rxPortLE);
setTabOrder(ui->rxPortLE, ui->dsbMaxDepth);
setTabOrder(ui->dsbMaxDepth,ui->configButton);
setTabOrder(ui->configButton, ui->packetLE);
setTabOrder(ui->packetLE, ui->sendButton);
//Setup Qwt Stuff
ui->plotDepth->setAxisAutoScale(2, false); //turn off y axis auto scale
ui->plotDepth->setAxisScale(0,-4.0,0,0.5); //set y axis scale
ui->plotDepth->setAutoReplot(true); //automatically update the plot
ui->plotDepth->setAxisTitle(QwtPlot::xBottom, "Time in Seconds");
ui->plotDepth->setAxisTitle(QwtPlot::yLeft, "Depth in Meters");
ui->plotDepth->setAxisMaxMinor(2, 30); //x axis minor ticks = 30
ui->plotDepth->axisScaleDraw(2)->enableComponent(QwtAbstractScaleDraw::Labels, false);
myTime = new QTime; //QTime for axis
myTime->start();
myCurve = new QwtPlotCurve; //create the curve for the graph
myCurve->setPen(QPen(QColor::QColor(65,105,225,255))); //set the curve color
myCurve->attach(ui->plotDepth); //connect the curve to the graph
index = 0; //the index holds the number of points
//Setup the colors of the compass
QPalette *colorGroup = new QPalette(Qt::lightGray, Qt::white, Qt::white, Qt::white, Qt::white, Qt::white, Qt::white, QColor::QColor(65,105,225,255), Qt::white);
ui->compass->setLineWidth(0);
ui->compass->setScaleComponents( QwtAbstractScaleDraw::Backbone |
QwtAbstractScaleDraw::Ticks | QwtAbstractScaleDraw::Labels );
ui->compass->setScaleTicks(0, 0, 3);
//Create tick marks on the compass
QMap<double, QString> map;
for ( double d = 0.0; d < 360.0; d += 30.0 )
{
QString label;
label.sprintf("%.0f", d);
map.insert(d, label);
}
ui->compass->setLabelMap(map);
ui->compass->setScale(36, 5, 0);
//Setup the needle on the compass
ui->compass->setPalette(*colorGroup);
ui->compass->setNeedle(new QwtDialSimpleNeedle(QwtDialSimpleNeedle::Ray,
true, QColor::QColor(65,105,225,255)));
ui->compass->setOrigin(270.0);
ui->compass->setValue(0.0);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_sendButton_clicked()
{
QString packet = ui->packetLE->text(); //create packet
QByteArray myDatagram = packet.toUtf8(); //convert to datagram
if(userIP == true)
{
udpSocket->writeDatagram(myDatagram.data(), myDatagram.size(),
txIP, rxPort);
}
else
{
udpSocket->writeDatagram(myDatagram.data(), myDatagram.size(),
QHostAddress::Broadcast, rxPort);
}
}
void MainWindow::on_configButton_clicked()
{
rxPort = ui->portLE->text().toInt(); //set port
listenPort = ui->rxPortLE->text().toInt(); //set port to listen on
rxUdpSocket->close();
rxUdpSocket->bind(listenPort); //found in book
if(ui->rxIPLE->text() != NULL) //if user added IP address
{
QString txStrIp = ui->rxIPLE->text(); //create RX IP string
txIP = txStrIp; //Convert to IP Address
userIP = true;
}
else
{
userIP = false;
}
ui->barDepth->setMaximum(int(ui->dsbMaxDepth->value()*100));
ui->plotDepth->setAxisScale(0,-1.0*ui->dsbMaxDepth->value(),0,0.5); //set y axis scale
}
void MainWindow::findHostIP() //found this code online, modified it to only show IP addresses (local)
{
QStringList items;
ui->listWidget->clear();
foreach(QNetworkInterface interface, QNetworkInterface::allInterfaces())
{
if (interface.flags().testFlag(QNetworkInterface::IsRunning))
foreach (QNetworkAddressEntry entry, interface.addressEntries())
{
if ( interface.hardwareAddress() != "00:00:00:00:00:00" && entry.ip().toString().contains("."))
items << interface.name() + " " + entry.ip().toString();
}
}
ui->listWidget->addItems(items);
}
void MainWindow::processPendingDatagrams()
{
QByteArray rxDatagram;
QString rxString;
do
{
rxDatagram.resize(rxUdpSocket->pendingDatagramSize());
rxUdpSocket->readDatagram(rxDatagram.data(), rxDatagram.size());
}
while(rxUdpSocket->hasPendingDatagrams());
rxString = (tr("\"%1\"").arg(rxDatagram.data())); //turn datagram into string
rxString.remove(QChar('"'), Qt::CaseInsensitive); //remove quotation marks
//ui->rxPacketLabel->setText(tr("\"%1\"").arg(rxDatagram.data()));
ui->rxPacketLabel->setText(rxString);
QTextStream packetStream(&rxString);
packetStream >> depthReadout >> voltsReadout >> ampsReadout >> tempReadout >> headingReadout >> otherReadout;
ui->lcdDepth->display(depthReadout);
ui->barDepth->setValue(depthReadout * 100);
ui->lcdOther->display(otherReadout);
ui->lcdVolts->display(voltsReadout);
ui->lcdAmps->display(ampsReadout);
ui->lcdTemp->display(tempReadout);
ui->leHeading->setText(QString::number(headingReadout));
ui->barHeading->setValue(headingReadout*10);
//Qwt stuff
myPoints.append(QPointF(0,0));
myPoints[index].setX(myTime->elapsed());
myPoints[index].setY(-1.0*depthReadout);
index++;
myCurve->setSamples(myPoints);
myCurve->setRenderHint(QwtPlotItem::RenderAntialiased, true);
ui->plotDepth->setAxisScale(2, myTime->elapsed()-10000, myTime->elapsed(), 60000);
ui->compass->setValue(headingReadout);
}
To copy to clipboard, switch view to plain text mode
Bookmarks