Browse Source

Initial commit

semlanik 5 years ago
commit
e32e1a3ab4
5 changed files with 181 additions and 0 deletions
  1. 66 0
      .gitignore
  2. 8 0
      LICENSE
  3. 2 0
      README.md
  4. 19 0
      SimpleSerial.pro
  5. 86 0
      main.cpp

+ 66 - 0
.gitignore

@@ -0,0 +1,66 @@
+# ---> Qt
+# C++ objects and libs
+
+*.slo
+*.lo
+*.o
+*.a
+*.la
+*.lai
+*.so
+*.dll
+*.dylib
+
+# Qt-es
+
+/.qmake.cache
+/.qmake.stash
+*.pro.user
+*.pro.user.*
+*.qbs.user
+*.qbs.user.*
+*.moc
+moc_*.cpp
+qrc_*.cpp
+ui_*.h
+Makefile*
+*-build-*
+
+# QtCreator
+
+*.autosave
+
+#QtCtreator Qml
+*.qmlproject.user
+*.qmlproject.user.*
+
+# ---> C++
+# Compiled Object files
+*.slo
+*.lo
+*.o
+*.obj
+
+# Precompiled Headers
+*.gch
+*.pch
+
+# Compiled Dynamic libraries
+*.so
+*.dylib
+*.dll
+
+# Fortran module files
+*.mod
+
+# Compiled Static libraries
+*.lai
+*.la
+*.a
+*.lib
+
+# Executables
+*.exe
+*.out
+*.app
+

+ 8 - 0
LICENSE

@@ -0,0 +1,8 @@
+MIT License
+Copyright (c) <year> <copyright holders>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

+ 2 - 0
README.md

@@ -0,0 +1,2 @@
+# SimpleSerial
+

+ 19 - 0
SimpleSerial.pro

@@ -0,0 +1,19 @@
+QT -= gui
+QT += serialport
+
+CONFIG += c++11 console
+CONFIG -= app_bundle
+
+# The following define makes your compiler emit warnings if you use
+# any feature of Qt which as been marked deprecated (the exact warnings
+# depend on your compiler). Please consult the documentation of the
+# deprecated API in order to know how to port your code away from it.
+DEFINES += QT_DEPRECATED_WARNINGS
+
+# You can also make your code fail to compile if you use deprecated APIs.
+# In order to do so, uncomment the following line.
+# You can also select to disable deprecated APIs only up to a certain version of Qt.
+#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
+
+SOURCES += \
+        main.cpp

+ 86 - 0
main.cpp

@@ -0,0 +1,86 @@
+#include <QCoreApplication>
+#include <QSerialPort>
+#include <QFile>
+#include <QDebug>
+#include <QFileSystemWatcher>
+#include <QDir>
+#include <QScopedPointer>
+#include <QTimer>
+#include <iostream>
+
+int main(int argc, char *argv[])
+{
+    QString comPortPath;
+    QString commandFile;
+
+    QCoreApplication a(argc, argv);
+
+    if(a.arguments().count() != 3) {
+        qCritical() << "Usage SimpleSerial <COM Port> <File name>";
+        return 1;
+    }
+
+    commandFile = QDir(".").absoluteFilePath(a.arguments().at(2));
+    comPortPath = a.arguments().at(1);
+
+    QSerialPort* port(new QSerialPort(comPortPath));
+    port->setFlowControl(QSerialPort::SoftwareControl);
+    port->setBaudRate(QSerialPort::Baud115200);
+    port->setStopBits(QSerialPort::OneStop);
+    port->setDataBits(QSerialPort::Data8);
+    port->setParity(QSerialPort::NoParity);
+    port->setReadBufferSize(100000);
+    QObject::connect(port, &QSerialPort::readyRead, [&](){
+        std::cout << port->readAll().data();
+    });
+
+    QObject::connect(port, &QSerialPort::errorOccurred, [&](QSerialPort::SerialPortError serialPortError){
+        qCritical() << serialPortError;
+    });
+
+    if(!port->open(QIODevice::ReadWrite)) {
+        qCritical() << "Unable to open" << port->portName();
+        delete port;
+        return 1;
+    }
+
+    QFileSystemWatcher* watcher(new QFileSystemWatcher(QStringList() << commandFile));
+    QObject::connect(watcher, &QFileSystemWatcher::fileChanged, [&](const QString &path){
+        qDebug() << "watcher alarm";
+        if(path == commandFile) {
+            QFile file(commandFile);
+            if(!file.open(QFile::ReadOnly)) {
+                qCritical() << "Unable to open command file: " << commandFile;
+                a.exit(1);
+            }
+            while(!file.atEnd()) {
+                QByteArray command = file.readLine();
+                port->write(command);
+            }
+            file.close();
+            watcher->addPath(commandFile);
+        }
+    });
+
+    QTimer testTimer;
+    testTimer.setInterval(1000);
+    testTimer.setSingleShot(true);
+    QObject::connect(&testTimer, &QTimer::timeout, [&]() {
+        QFile file(commandFile);
+        if(!file.open(QFile::ReadOnly)) {
+            qCritical() << "Unable to open command file: " << commandFile;
+            a.exit(1);
+        }
+
+        while(!file.atEnd()) {
+            QByteArray command = file.readLine();
+            port->write(command);
+        }
+        file.close();
+    });
+    testTimer.start();
+    int retVal =  a.exec();
+    port->close();
+    delete port;
+    return retVal;
+}