diff --git a/src/GcLogger.cpp b/src/GcLogger.cpp index dea5b9f..0022edf 100644 --- a/src/GcLogger.cpp +++ b/src/GcLogger.cpp @@ -6,7 +6,7 @@ #include "GcLogger.h" -void GcLogger::setup(const string& deviceSerialPort, int deviceBaudRate) { +void GcLogger::setup(const string &deviceSerialPort, int deviceBaudRate) { baudRate = deviceBaudRate; serialPort = deviceSerialPort; @@ -14,7 +14,7 @@ void GcLogger::setup(const string& deviceSerialPort, int deviceBaudRate) { device = new GmcDevice(deviceSerialPort, deviceBaudRate); - if(!device->isConnected()) { + if (!device->isConnected()) { cout << "Failed to connect to device!" << endl; } else { cout << "Device connected!" << endl; @@ -44,7 +44,7 @@ int GcLogger::run() { } } -void GcLogger::readIni(const 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 dca55f4..53ce1ab 100644 --- a/src/GcLogger.h +++ b/src/GcLogger.h @@ -21,10 +21,11 @@ class GcLogger { inipp::Ini ini; GmcDevice *device; - void setup(const string&, int); + void setup(const string &, int); public: - void readIni(const string&); + void readIni(const string &); + int run(); }; diff --git a/src/GmcDevice.cpp b/src/GmcDevice.cpp index 88c6bd9..d79d40f 100644 --- a/src/GmcDevice.cpp +++ b/src/GmcDevice.cpp @@ -4,14 +4,14 @@ #include "GmcDevice.h" -GmcDevice::GmcDevice(const string& serialPort, int baud) { +GmcDevice::GmcDevice(const string &serialPort, int baud) { device = new SerialPort(serialPort, baud); - if(!device->isOpen()) { + if (!device->isOpen()) { cout << "Failed to open gmc device!" << endl; } else { cout << "Connection to gmc device established!" << endl; - if(!setHeartbeatOff()) { + if (!setHeartbeatOff()) { cout << "Failed to disable heartbeat!" << endl; device->serialClose(); } @@ -27,16 +27,16 @@ bool GmcDevice::close() { } int GmcDevice::getCPM() { - if(!device->isOpen()) { + if (!device->isOpen()) { cout << "Device is not connected, failed to read CPM!" << endl; return -1; } string cmd = ">"; - string result; + vector result; - if(device->serialWrite(cmd) == cmd.length()) { - result = device->serialRead(2); // cpm result has size 2 + if (device->serialWrite(cmd) == cmd.length()) { + result = device->serialRead(2); // cpm result has size 2 } else { cout << "Failed to send command to device!" << endl; } @@ -45,47 +45,49 @@ int GmcDevice::getCPM() { } float GmcDevice::getTemperature() { - if(!device->isOpen()) { + if (!device->isOpen()) { cout << "Device is not connected, failed to read temperature!" << endl; return -1; } string cmd = ">"; - string result; + vector result; - if(device->serialWrite(cmd) == cmd.length()) { - result = device->serialRead(4); // temp result has size 4 + if (device->serialWrite(cmd) == cmd.length()) { + result = device->serialRead(4); // temp result has size 4 } else { cout << "Failed to send command to device!" << endl; } - int sign = result[2] == 0 ? 1 : -1; - float temp = result[0]; - temp += (float) result[1] / 10; + int sign = result[2] == 0 ? 1 : -1; + float temp = result[0]; // integer part + temp += static_cast(result[1] / 10.0); // float part temp = temp * sign; return temp; } string GmcDevice::getVersion() { - if(!device->isOpen()) { + if (!device->isOpen()) { cout << "Device is not connected, failed to read version!" << endl; return ""; } string cmd = ">"; - string result; + vector result; - if(device->serialWrite(cmd) == cmd.length()) { - result = device->serialRead(14); // version result has size 14 + if (device->serialWrite(cmd) == cmd.length()) { + result = device->serialRead(14); // version result has size 14 } else { cout << "Failed to send command to device!" << endl; } - return result; + string strResult(result.begin(), result.end()); + + return strResult; } bool GmcDevice::setHeartbeatOff() { - if(!device->isOpen()) { + if (!device->isOpen()) { cout << "Device is not connected, failed to disable heartbeat!" << endl; return false; } @@ -93,8 +95,8 @@ bool GmcDevice::setHeartbeatOff() { string cmd = ">"; string result; - if(device->serialWrite(cmd) == cmd.length()) { - return device->clearInput(100); // clear 100 chars + if (device->serialWrite(cmd) == cmd.length()) { + return device->clearInput(100); // clear 100 chars } else { cout << "Failed to send command to device!" << endl; } diff --git a/src/GmcDevice.h b/src/GmcDevice.h index f193914..37ed2db 100644 --- a/src/GmcDevice.h +++ b/src/GmcDevice.h @@ -11,15 +11,20 @@ class GmcDevice { SerialPort *device; public: - GmcDevice(const string& serialPort, int baud); + GmcDevice(const string &serialPort, int baud); + ~GmcDevice(); bool close(); + bool isConnected(); int getCPM(); + float getTemperature(); + string getVersion(); + bool setHeartbeatOff(); }; diff --git a/src/SerialPort.cpp b/src/SerialPort.cpp index d8496d9..1d217e8 100644 --- a/src/SerialPort.cpp +++ b/src/SerialPort.cpp @@ -60,12 +60,12 @@ SerialPort::~SerialPort() { serialClose(); } -string SerialPort::serialRead(int length) { - char *buf = (char *) calloc(length + 1, sizeof(char)); +vector SerialPort::serialRead(int length) { + auto *buf = reinterpret_cast (calloc(length, sizeof(uint8_t))); read(fd, buf, length); - string result(buf); + vector result(buf, buf + length); free(buf); return result; diff --git a/src/SerialPort.h b/src/SerialPort.h index c65ae2e..f961969 100644 --- a/src/SerialPort.h +++ b/src/SerialPort.h @@ -7,6 +7,7 @@ #include #include +#include #include // POSIX terminal control definitions #include @@ -19,19 +20,24 @@ class SerialPort { struct termios tio; int tioBaud; - int getTioBaud(int); + static int getTioBaud(int); public: // Constructor SerialPort(); - SerialPort(const string&, int); + + SerialPort(const string &, int); + ~SerialPort(); bool isOpen(); + bool serialClose(); - string serialRead(int); - int serialWrite(const string&); + vector serialRead(int); + + int serialWrite(const string &); + bool clearInput(int); };