Posts

Showing posts from September, 2017

Serial GUI plot without timer

mainwindow.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QtSerialPort/QSerialPort> namespace Ui { class MainWindow ; } class MainWindow : public QMainWindow { Q_OBJECT QSerialPort * serial ; QVector < double > dataArray ; QVector < double > counterArray ; double counter = 0 ; public : explicit MainWindow ( QWidget *parent = 0 ); ~ MainWindow (); private : Ui :: MainWindow * ui ; private slots : void readData(); void on_btn_start_clicked(); void on_btn_stop_clicked(); void plot(); }; #endif // MAINWINDOW_H mainwindow.cpp #include "mainwindow.h" #include "ui_mainwindow.h" #include <QtSerialPort/QSerialPort> #include <QDebug> #include "qcustomplot.h" MainWindow ::MainWindow( QWidget *parent) : QMainWindow (parent), ui ( new Ui :: MainWindow ) ...

SerialGUI with fast display

1) Add Qcustomplot.h and qcustomplot.cpp to project 2)  QT += core gui serialport printsupport in .pro file mainwindow.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QSerialPort> #include <QDebug> #include <QTimer> namespace Ui { class MainWindow ; } class MainWindow : public QMainWindow { Q_OBJECT QTimer * dataCaptTimer ; QSerialPort serial ; QVector < double > dataArray ; QVector < double > CounterArray ; double count = 0 ; public : explicit MainWindow ( QWidget *parent = 0 ); ~ MainWindow (); private slots : void on_start_btn_pressed(); void on_stop_btn_pressed(); private : Ui :: MainWindow * ui ; public slots : void startDataCapture(); void plot(); }; #endif // MAINWINDOW_H mainwindow.cpp #include "mainwindow.h" #include "ui_mainwindow.h" M...

Plotting in Qt

Image
1) First include qcustomplot.h and qcustomplot.cpp in header and source file respectively 2) add printsupport in qt+= expression in  .pro file i.e.,       QT += core gui serialport printsupport 3)  Place a regular QWidget on your form in the desired location. Right click on it and hit   Promote to... In the appearing dialog, enter  QCustomPlot  in the input field next to  Promoted class name . The input next to  Header file should automatically fill with the correct  qcustomplot.h  value. Hit  Add  to add QCustomPlot to the promoted classes list and finally hit  Promote  to turn the QWidget on your form into a QCustomPlot. mainwindow.cpp #include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) :     QMainWindow(parent),     ui(new Ui::MainWindow) {     ui->setupUi(this);     ui->plot->addG...

Serial data capture through timer (since timer is implemented as thread)

1) declare slots in header file and 2) define them in its .cpp file mainwindow.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QSerialPort> #include <QDebug> #include <QTimer> namespace Ui { class MainWindow ; } class MainWindow : public QMainWindow { Q_OBJECT QTimer * dataCaptTimer ; QSerialPort serial ; public : explicit MainWindow ( QWidget *parent = 0 ); ~ MainWindow (); private slots : void on_start_btn_pressed(); void on_stop_btn_pressed(); private : Ui :: MainWindow * ui ; public slots : void startDataCapture(); void stopDataCapture(); }; #endif // MAINWINDOW_H mainwindow.cpp file #include "mainwindow.h" #include "ui_mainwindow.h" MainWindow ::MainWindow( QWidget *parent) : QMainWindow (parent), ui ( new Ui :: MainWindow ) { dataCaptTimer = new QTimer ; dataCapt...

Read Continuously from Serial Port in QT

#include <QCoreApplication> #include <QtSerialPort/QSerialPort> #include <Qdebug> QSerialPort serial; int main() { //COM Port Configuration serial.setPortName( "COM6" ); serial.setBaudRate( QSerialPort :: Baud9600 ); serial.setDataBits( QSerialPort :: Data8 ); serial.setParity( QSerialPort :: NoParity ); serial.setStopBits( QSerialPort :: OneStop ); serial.setFlowControl( QSerialPort :: NoFlowControl ); serial. open ( QIODevice :: ReadWrite ); char buffer[ 50 ]; while ( 1 ) { serial.waitForReadyRead(); // blocks calls until new data is available for reading. serial.readLine(buffer, 50 ); qDebug ()<<buffer; } serial. close (); }

QT Console Application for Serial Data Read

#include <QCoreApplication> #include <QtSerialPort/QSerialPort> #include <Qdebug> QSerialPort serial; int main() { //COM Port Configuration serial.setPortName( "COM6" ); serial.setBaudRate( QSerialPort :: Baud9600 ); serial.setDataBits( QSerialPort :: Data8 ); serial.setParity( QSerialPort :: NoParity ); serial.setStopBits( QSerialPort :: OneStop ); serial.setFlowControl( QSerialPort :: NoFlowControl ); serial. open ( QIODevice :: ReadWrite ); char buffer[ 50 ]; serial.waitForReadyRead(); serial.readLine(buffer, 50 ); qDebug ()<<buffer<<endl; serial.waitForReadyRead(); serial.readLine(buffer, 50 ); qDebug ()<<buffer<<endl; serial.waitForReadyRead(); serial.readLine(buffer, 50 ); qDebug ()<<buffer<<endl; serial.waitForReadyRead(); serial.readLine(buffer, 50 ); qDebug ()<<buffer<...

Python: Check if a string is a valid float

isfloat() function def isfloat(value):   try:     float(value)     return True   except ValueError:     return False Use: if( isfloat(arduinoString) ):         data = float(arduinoString)         dataArray.append(arduinoString)         index=index+1

Plotting 555 Timer Data

Arduino Code #define timerDataPin 5 float timerData; void setup() {   Serial.begin(250000);   pinMode(timerDataPin, INPUT); } void loop() {   timerData = analogRead(timerDataPin);   Serial.println(timerData); } Python Code import serial #import serial lib import numpy  #import numpy lib for array operations import matplotlib.pyplot as plt #import matplotlib from drawnow import * arduinoSerialData = serial.Serial('COM6',250000) dataArray=[] index=0 count=100 #After the count value the graph will start moving def isfloat(value):   try:     float(value)     return True   except ValueError:     return False def makeFig():     plt.plot(dataArray,'bo-')     while True:     while (arduinoSerialData.inWaiting() == 0): #Wait until data is there         pass     arduinoString = arduinoSerialData.readline();     print a...

Plotting streaming data in Python

import serial #import serial lib import numpy  #import numpy lib for array operations import matplotlib.pyplot as plt #import matplotlib from drawnow import * arduinoSerialData = serial.Serial('COM6',9600) plt.ion() #Tell matplotlib you want interactive mode to plot live data data1Array=[]   #Data Arrays to be plotted are initialized data2Array=[]   #Data Arrays to be plotted are initialized cnt=0 def makeFig():     plt.ylim(0,25)             #stop autoscaling by defining limits as: min.=-1 and max.=25     plt.plot(data1Array, 'ro-', label='data1')   #plot in red color and dots connected by line     plt.title('Streaming Serial data')     plt.ylabel('Sawtooth data 1')     plt.legend(loc='upper left')     plt2=plt.twinx()     plt2.plot(data2Array,'b^-', label='data2')     plt.ylim(0,25)     plt2.legend(loc='upper right')   ...