Reading values works now

This commit is contained in:
Christoph Haas 2019-05-08 18:07:23 +02:00
parent e66d13e5a3
commit ffb81213bb
6 changed files with 49 additions and 35 deletions

View File

@ -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);

View File

@ -21,10 +21,11 @@ class GcLogger {
inipp::Ini<char> ini;
GmcDevice *device;
void setup(const string&, int);
void setup(const string &, int);
public:
void readIni(const string&);
void readIni(const string &);
int run();
};

View File

@ -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 = "<GETCPM>>";
string result;
vector<uint8_t> 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 = "<GETTEMP>>";
string result;
vector<uint8_t> 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<float>(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 = "<GETVER>>";
string result;
vector<uint8_t> 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 = "<HEARTBEAT0>>";
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;
}

View File

@ -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();
};

View File

@ -60,12 +60,12 @@ SerialPort::~SerialPort() {
serialClose();
}
string SerialPort::serialRead(int length) {
char *buf = (char *) calloc(length + 1, sizeof(char));
vector<uint8_t> SerialPort::serialRead(int length) {
auto *buf = reinterpret_cast<uint8_t *> (calloc(length, sizeof(uint8_t)));
read(fd, buf, length);
string result(buf);
vector<uint8_t> result(buf, buf + length);
free(buf);
return result;

View File

@ -7,6 +7,7 @@
#include <iostream>
#include <string>
#include <vector>
#include <termios.h> // POSIX terminal control definitions
#include <unistd.h>
@ -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<uint8_t> serialRead(int);
int serialWrite(const string &);
bool clearInput(int);
};