gclogger/src/GmcDevice.cpp

110 lines
2.6 KiB
C++
Raw Normal View History

2019-05-07 23:14:21 +02:00
//
// Created by h44z on 07.05.19.
//
#include "GmcDevice.h"
2019-05-08 18:07:23 +02:00
GmcDevice::GmcDevice(const string &serialPort, int baud) {
2019-05-07 23:14:21 +02:00
device = new SerialPort(serialPort, baud);
2019-05-08 18:07:23 +02:00
if (!device->isOpen()) {
2019-05-07 23:14:21 +02:00
cout << "Failed to open gmc device!" << endl;
} else {
cout << "Connection to gmc device established!" << endl;
2019-05-08 18:07:23 +02:00
if (!setHeartbeatOff()) {
2019-05-07 23:14:21 +02:00
cout << "Failed to disable heartbeat!" << endl;
device->serialClose();
}
}
}
GmcDevice::~GmcDevice() {
close();
}
bool GmcDevice::close() {
return device->serialClose();
}
int GmcDevice::getCPM() {
2019-05-08 18:07:23 +02:00
if (!device->isOpen()) {
2019-05-07 23:14:21 +02:00
cout << "Device is not connected, failed to read CPM!" << endl;
return -1;
}
string cmd = "<GETCPM>>";
2019-05-08 18:07:23 +02:00
vector<uint8_t> result;
2019-05-07 23:14:21 +02:00
2019-05-08 18:07:23 +02:00
if (device->serialWrite(cmd) == cmd.length()) {
result = device->serialRead(2); // cpm result has size 2
2019-05-07 23:39:30 +02:00
} else {
cout << "Failed to send command to device!" << endl;
2019-05-07 23:14:21 +02:00
}
return result[0] * 256 + result[1];
}
float GmcDevice::getTemperature() {
2019-05-08 18:07:23 +02:00
if (!device->isOpen()) {
2019-05-07 23:14:21 +02:00
cout << "Device is not connected, failed to read temperature!" << endl;
return -1;
}
string cmd = "<GETTEMP>>";
2019-05-08 18:07:23 +02:00
vector<uint8_t> result;
2019-05-07 23:14:21 +02:00
2019-05-08 18:07:23 +02:00
if (device->serialWrite(cmd) == cmd.length()) {
result = device->serialRead(4); // temp result has size 4
2019-05-07 23:39:30 +02:00
} else {
cout << "Failed to send command to device!" << endl;
2019-05-07 23:14:21 +02:00
}
2019-05-08 18:07:23 +02:00
int sign = result[2] == 0 ? 1 : -1;
float temp = result[0]; // integer part
temp += static_cast<float>(result[1] / 10.0); // float part
2019-05-07 23:14:21 +02:00
temp = temp * sign;
return temp;
}
string GmcDevice::getVersion() {
2019-05-08 18:07:23 +02:00
if (!device->isOpen()) {
2019-05-07 23:14:21 +02:00
cout << "Device is not connected, failed to read version!" << endl;
return "";
}
string cmd = "<GETVER>>";
2019-05-08 18:07:23 +02:00
vector<uint8_t> result;
2019-05-07 23:14:21 +02:00
2019-05-08 18:07:23 +02:00
if (device->serialWrite(cmd) == cmd.length()) {
result = device->serialRead(14); // version result has size 14
2019-05-07 23:39:30 +02:00
} else {
cout << "Failed to send command to device!" << endl;
2019-05-07 23:14:21 +02:00
}
2019-05-08 18:07:23 +02:00
string strResult(result.begin(), result.end());
return strResult;
2019-05-07 23:14:21 +02:00
}
bool GmcDevice::setHeartbeatOff() {
2019-05-08 18:07:23 +02:00
if (!device->isOpen()) {
2019-05-07 23:14:21 +02:00
cout << "Device is not connected, failed to disable heartbeat!" << endl;
return false;
}
2019-05-07 23:39:30 +02:00
string cmd = "<HEARTBEAT0>>";
string result;
2019-05-08 18:07:23 +02:00
if (device->serialWrite(cmd) == cmd.length()) {
return device->clearInput(100); // clear 100 chars
2019-05-07 23:39:30 +02:00
} else {
cout << "Failed to send command to device!" << endl;
}
2019-05-07 23:14:21 +02:00
return false;
}
bool GmcDevice::isConnected() {
return device->isOpen();
}