diff --git a/src/GcLogger.cpp b/src/GcLogger.cpp index 85c6d88..dea5b9f 100644 --- a/src/GcLogger.cpp +++ b/src/GcLogger.cpp @@ -33,6 +33,12 @@ int GcLogger::run() { string deviceVersion = device->getVersion(); cout << "GMC Device version: " << deviceVersion << endl; + int cpm = device->getCPM(); + cout << "CPM: " << cpm << endl; + + float temperature = device->getTemperature(); + cout << "Temperature: " << temperature << endl; + device->close(); return EXIT_SUCCESS; } diff --git a/src/GmcDevice.cpp b/src/GmcDevice.cpp index 3c6a5bb..88c6bd9 100644 --- a/src/GmcDevice.cpp +++ b/src/GmcDevice.cpp @@ -36,7 +36,9 @@ int GmcDevice::getCPM() { string result; if(device->serialWrite(cmd) == cmd.length()) { - device->serialRead(result, 2); // cpm result has size 2 + result = device->serialRead(2); // cpm result has size 2 + } else { + cout << "Failed to send command to device!" << endl; } return result[0] * 256 + result[1]; @@ -52,7 +54,9 @@ float GmcDevice::getTemperature() { string result; if(device->serialWrite(cmd) == cmd.length()) { - device->serialRead(result, 4); // temp result has size 4 + 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; @@ -72,7 +76,9 @@ string GmcDevice::getVersion() { string result; if(device->serialWrite(cmd) == cmd.length()) { - device->serialRead(result, 14); // version result has size 14 + result = device->serialRead(14); // version result has size 14 + } else { + cout << "Failed to send command to device!" << endl; } return result; @@ -84,6 +90,15 @@ bool GmcDevice::setHeartbeatOff() { return false; } + string cmd = ">"; + string result; + + if(device->serialWrite(cmd) == cmd.length()) { + return device->clearInput(100); // clear 100 chars + } else { + cout << "Failed to send command to device!" << endl; + } + return false; } diff --git a/src/SerialPort.cpp b/src/SerialPort.cpp index 0d4f438..d8496d9 100644 --- a/src/SerialPort.cpp +++ b/src/SerialPort.cpp @@ -60,11 +60,18 @@ SerialPort::~SerialPort() { serialClose(); } -int SerialPort::serialRead(string buffer, int length) { - return read(fd, buffer.data(), length); +string SerialPort::serialRead(int length) { + char *buf = (char *) calloc(length + 1, sizeof(char)); + + read(fd, buf, length); + + string result(buf); + free(buf); + + return result; } -int SerialPort::serialWrite(const string& data) { +int SerialPort::serialWrite(const string &data) { return write(fd, data.c_str(), data.length()); } diff --git a/src/SerialPort.h b/src/SerialPort.h index 75f7cce..c65ae2e 100644 --- a/src/SerialPort.h +++ b/src/SerialPort.h @@ -30,7 +30,7 @@ public: bool isOpen(); bool serialClose(); - int serialRead(string, int); + string serialRead(int); int serialWrite(const string&); bool clearInput(int); };