// // 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 = ">"; vector result; if (device->serialWrite(cmd) == cmd.length()) { result = device->serialRead(2); // cpm result has size 2 } else { cout << "Failed to send command to device!" << endl; } 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 = ">"; vector result; 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]; // integer part temp += static_cast(result[1] / 10.0); // float part temp = temp * sign; return temp; } string GmcDevice::getVersion() { if (!device->isOpen()) { cout << "Device is not connected, failed to read version!" << endl; return ""; } string cmd = ">"; vector result; if (device->serialWrite(cmd) == cmd.length()) { result = device->serialRead(14); // version result has size 14 } else { cout << "Failed to send command to device!" << endl; } string strResult(result.begin(), result.end()); return strResult; } bool GmcDevice::setHeartbeatOff() { if (!device->isOpen()) { cout << "Device is not connected, failed to disable heartbeat!" << endl; 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; } bool GmcDevice::isConnected() { return device->isOpen(); }