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

@ -25,6 +25,7 @@ class GcLogger {
public:
void readIni(const string &);
int run();
};

View File

@ -33,7 +33,7 @@ int GmcDevice::getCPM() {
}
string cmd = "<GETCPM>>";
string result;
vector<uint8_t> result;
if (device->serialWrite(cmd) == cmd.length()) {
result = device->serialRead(2); // cpm result has size 2
@ -51,7 +51,7 @@ float GmcDevice::getTemperature() {
}
string cmd = "<GETTEMP>>";
string result;
vector<uint8_t> result;
if (device->serialWrite(cmd) == cmd.length()) {
result = device->serialRead(4); // temp result has size 4
@ -60,8 +60,8 @@ float GmcDevice::getTemperature() {
}
int sign = result[2] == 0 ? 1 : -1;
float temp = result[0];
temp += (float) result[1] / 10;
float temp = result[0]; // integer part
temp += static_cast<float>(result[1] / 10.0); // float part
temp = temp * sign;
return temp;
}
@ -73,7 +73,7 @@ string GmcDevice::getVersion() {
}
string cmd = "<GETVER>>";
string result;
vector<uint8_t> result;
if (device->serialWrite(cmd) == cmd.length()) {
result = device->serialRead(14); // version result has size 14
@ -81,7 +81,9 @@ string GmcDevice::getVersion() {
cout << "Failed to send command to device!" << endl;
}
return result;
string strResult(result.begin(), result.end());
return strResult;
}
bool GmcDevice::setHeartbeatOff() {

View File

@ -12,14 +12,19 @@ class GmcDevice {
public:
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();
bool isOpen();
bool serialClose();
string serialRead(int);
vector<uint8_t> serialRead(int);
int serialWrite(const string &);
bool clearInput(int);
};