From 724b0257954075369b2d7b761d57a0f4d8f04b83 Mon Sep 17 00:00:00 2001 From: Christoph Haas Date: Tue, 7 May 2019 23:14:21 +0200 Subject: [PATCH] read version of gmc device --- libs/inipp | 2 +- src/CMakeLists.txt | 3 +- src/GcLogger.cpp | 24 +++++++++--- src/GcLogger.h | 6 ++- src/GmcDevice.cpp | 92 +++++++++++++++++++++++++++++++++++++++++++++ src/GmcDevice.h | 27 ++++++++++++++ src/SerialPort.cpp | 93 ++++++++++++++++++++++++++++++++++++++++++++++ src/SerialPort.h | 39 +++++++++++++++++++ 8 files changed, 275 insertions(+), 11 deletions(-) create mode 100644 src/GmcDevice.cpp create mode 100644 src/GmcDevice.h create mode 100644 src/SerialPort.cpp create mode 100644 src/SerialPort.h diff --git a/libs/inipp b/libs/inipp index a120817..da3ae5f 160000 --- a/libs/inipp +++ b/libs/inipp @@ -1 +1 @@ -Subproject commit a12081742209b122108c159517259c16e2335345 +Subproject commit da3ae5feb5353fce48cadc34b6ea79de5ac05ca1 diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 8634b9f..143674c 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,8 +1,7 @@ # Notice name prefix of this variable, set by CMake according # to value given with "project()" in the root CMakeLists.txt. include_directories(${gclogger_SOURCE_DIR}/include) -add_executable(gclogger main.cpp GcLogger.cpp GcLogger.h) - +add_executable(gclogger main.cpp GcLogger.cpp GcLogger.h SerialPort.cpp SerialPort.h GmcDevice.cpp GmcDevice.h) # I assume you want to use LibProject as a library in MainProject. include_directories(${gclogger_SOURCE_DIR}/libs) link_directories(${gclogger_SOURCE_DIR}/libs) \ No newline at end of file diff --git a/src/GcLogger.cpp b/src/GcLogger.cpp index 22b71b4..85c6d88 100644 --- a/src/GcLogger.cpp +++ b/src/GcLogger.cpp @@ -6,12 +6,20 @@ #include "GcLogger.h" -void GcLogger::setup(std::string serialPort, int baudRate) { - this->baudRate = baudRate; - this->serialPort = serialPort; - this->isSetup = true; +void GcLogger::setup(const string& deviceSerialPort, int deviceBaudRate) { + baudRate = deviceBaudRate; + serialPort = deviceSerialPort; - cout << "Serial port configuration: port=" << serialPort << " baud=" << baudRate << endl; + cout << "Serial port configuration: port=" << deviceSerialPort << " baud=" << deviceBaudRate << endl; + + device = new GmcDevice(deviceSerialPort, deviceBaudRate); + + if(!device->isConnected()) { + cout << "Failed to connect to device!" << endl; + } else { + cout << "Device connected!" << endl; + isSetup = true; + } } int GcLogger::run() { @@ -22,11 +30,15 @@ int GcLogger::run() { } else { cout << "Running gclogger!" << endl; + string deviceVersion = device->getVersion(); + cout << "GMC Device version: " << deviceVersion << endl; + + device->close(); return EXIT_SUCCESS; } } -void GcLogger::readIni(string filePath) { +void GcLogger::readIni(const string& filePath) { cout << "Reading configuration from: " << filePath << endl; ifstream is(filePath); diff --git a/src/GcLogger.h b/src/GcLogger.h index da657fa..dca55f4 100644 --- a/src/GcLogger.h +++ b/src/GcLogger.h @@ -9,6 +9,7 @@ #include #include +#include "GmcDevice.h" #include "inipp/inipp/inipp.h" using namespace std; @@ -18,11 +19,12 @@ class GcLogger { string serialPort; int baudRate; inipp::Ini ini; + GmcDevice *device; - void setup(string, int); + void setup(const string&, int); public: - void readIni(string); + void readIni(const string&); int run(); }; diff --git a/src/GmcDevice.cpp b/src/GmcDevice.cpp new file mode 100644 index 0000000..3c6a5bb --- /dev/null +++ b/src/GmcDevice.cpp @@ -0,0 +1,92 @@ +// +// Created by h44z on 07.05.19. +// + +#include "GmcDevice.h" + +GmcDevice::GmcDevice(const string& serialPort, int baud) { + device = new SerialPort(serialPort, baud); + + if(!device->isOpen()) { + cout << "Failed to open gmc device!" << endl; + } else { + cout << "Connection to gmc device established!" << endl; + if(!setHeartbeatOff()) { + cout << "Failed to disable heartbeat!" << endl; + device->serialClose(); + } + } +} + +GmcDevice::~GmcDevice() { + close(); +} + +bool GmcDevice::close() { + return device->serialClose(); +} + +int GmcDevice::getCPM() { + if(!device->isOpen()) { + cout << "Device is not connected, failed to read CPM!" << endl; + return -1; + } + + string cmd = ">"; + string result; + + if(device->serialWrite(cmd) == cmd.length()) { + device->serialRead(result, 2); // cpm result has size 2 + } + + return result[0] * 256 + result[1]; +} + +float GmcDevice::getTemperature() { + if(!device->isOpen()) { + cout << "Device is not connected, failed to read temperature!" << endl; + return -1; + } + + string cmd = ">"; + string result; + + if(device->serialWrite(cmd) == cmd.length()) { + device->serialRead(result, 4); // temp result has size 4 + } + + int sign = result[2] == 0 ? 1 : -1; + float temp = result[0]; + temp += (float) result[1] / 10; + temp = temp * sign; + return temp; +} + +string GmcDevice::getVersion() { + if(!device->isOpen()) { + cout << "Device is not connected, failed to read version!" << endl; + return ""; + } + + string cmd = ">"; + string result; + + if(device->serialWrite(cmd) == cmd.length()) { + device->serialRead(result, 14); // version result has size 14 + } + + return result; +} + +bool GmcDevice::setHeartbeatOff() { + if(!device->isOpen()) { + cout << "Device is not connected, failed to disable heartbeat!" << endl; + return false; + } + + return false; +} + +bool GmcDevice::isConnected() { + return device->isOpen(); +} diff --git a/src/GmcDevice.h b/src/GmcDevice.h new file mode 100644 index 0000000..f193914 --- /dev/null +++ b/src/GmcDevice.h @@ -0,0 +1,27 @@ +// +// Created by h44z on 07.05.19. +// + +#ifndef GCLOGGER_GMCDEVICE_H +#define GCLOGGER_GMCDEVICE_H + +#include "SerialPort.h" + +class GmcDevice { + SerialPort *device; + +public: + GmcDevice(const string& serialPort, int baud); + ~GmcDevice(); + + bool close(); + bool isConnected(); + + int getCPM(); + float getTemperature(); + string getVersion(); + bool setHeartbeatOff(); +}; + + +#endif //GCLOGGER_GMCDEVICE_H diff --git a/src/SerialPort.cpp b/src/SerialPort.cpp new file mode 100644 index 0000000..0d4f438 --- /dev/null +++ b/src/SerialPort.cpp @@ -0,0 +1,93 @@ +// +// Created by h44z on 07.05.19. +// + +#include "SerialPort.h" + +SerialPort::SerialPort() { + fd = -1; + tioBaud = B0; + tio.c_cflag = CS8 | CREAD | CLOCAL; // 8n1 + tio.c_cc[VMIN] = 0; + tio.c_cc[VTIME] = 5; +} + +int SerialPort::getTioBaud(int baud) { + switch (baud) { + case 9600: + return B9600; + case 19200: + return B19200; + case 38400: + return B38400; + case 57600: + return B57600; + case 115200: + return B115200; + default: + return B0; + } +} + +SerialPort::SerialPort(const string &serialPort, int baud) : SerialPort() { + tioBaud = getTioBaud(baud); + + fd = open(serialPort.c_str(), O_RDWR | O_NOCTTY | O_NDELAY); + + if (fd == -1) { // if open is unsucessful + cout << "Unable to open serial port: " << serialPort << endl; + } else { + fcntl(fd, F_SETFL, 0); + cout << "Serial port opened: " << serialPort << endl; + + if (cfsetspeed(&tio, tioBaud) == 0) { // set baud speed + if (tcsetattr(fd, TCSANOW, &tio) == 0) { // apply baud speed change + cout << "Serial port initialized successfully to BAUD: " << baud << " (" << tioBaud << ")" << endl; + } + } else { + // something failed + close(fd); + fd = -1; + } + } +} + +bool SerialPort::isOpen() { + return fd != -1; +} + +SerialPort::~SerialPort() { + serialClose(); +} + +int SerialPort::serialRead(string buffer, int length) { + return read(fd, buffer.data(), length); +} + +int SerialPort::serialWrite(const string& data) { + return write(fd, data.c_str(), data.length()); +} + +bool SerialPort::clearInput(int size) { + char ch; + + // flush input stream + for (int i = 0; i < size; i++) { + if (read(fd, &ch, 1) == 0) + return true; // found end of stream + } + + return false; // still data left in the buffer +} + +bool SerialPort::serialClose() { + cout << "Closing serial port" << endl; + + if (isOpen()) { + bool result = close(fd) == 0; + fd = -1; + return result; + } + + return false; +} diff --git a/src/SerialPort.h b/src/SerialPort.h new file mode 100644 index 0000000..75f7cce --- /dev/null +++ b/src/SerialPort.h @@ -0,0 +1,39 @@ +// +// Created by h44z on 07.05.19. +// + +#ifndef GCLOGGER_SERIALPORT_H +#define GCLOGGER_SERIALPORT_H + +#include +#include + +#include // POSIX terminal control definitions +#include +#include + +using namespace std; + +class SerialPort { + int fd; + struct termios tio; + int tioBaud; + + int getTioBaud(int); + +public: + // Constructor + SerialPort(); + SerialPort(const string&, int); + ~SerialPort(); + + bool isOpen(); + bool serialClose(); + + int serialRead(string, int); + int serialWrite(const string&); + bool clearInput(int); +}; + + +#endif //GCLOGGER_SERIALPORT_H