Reading values works now
This commit is contained in:
parent
e66d13e5a3
commit
ffb81213bb
@ -6,7 +6,7 @@
|
|||||||
#include "GcLogger.h"
|
#include "GcLogger.h"
|
||||||
|
|
||||||
|
|
||||||
void GcLogger::setup(const string& deviceSerialPort, int deviceBaudRate) {
|
void GcLogger::setup(const string &deviceSerialPort, int deviceBaudRate) {
|
||||||
baudRate = deviceBaudRate;
|
baudRate = deviceBaudRate;
|
||||||
serialPort = deviceSerialPort;
|
serialPort = deviceSerialPort;
|
||||||
|
|
||||||
@ -14,7 +14,7 @@ void GcLogger::setup(const string& deviceSerialPort, int deviceBaudRate) {
|
|||||||
|
|
||||||
device = new GmcDevice(deviceSerialPort, deviceBaudRate);
|
device = new GmcDevice(deviceSerialPort, deviceBaudRate);
|
||||||
|
|
||||||
if(!device->isConnected()) {
|
if (!device->isConnected()) {
|
||||||
cout << "Failed to connect to device!" << endl;
|
cout << "Failed to connect to device!" << endl;
|
||||||
} else {
|
} else {
|
||||||
cout << "Device connected!" << endl;
|
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;
|
cout << "Reading configuration from: " << filePath << endl;
|
||||||
|
|
||||||
ifstream is(filePath);
|
ifstream is(filePath);
|
||||||
|
@ -21,10 +21,11 @@ class GcLogger {
|
|||||||
inipp::Ini<char> ini;
|
inipp::Ini<char> ini;
|
||||||
GmcDevice *device;
|
GmcDevice *device;
|
||||||
|
|
||||||
void setup(const string&, int);
|
void setup(const string &, int);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void readIni(const string&);
|
void readIni(const string &);
|
||||||
|
|
||||||
int run();
|
int run();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -4,14 +4,14 @@
|
|||||||
|
|
||||||
#include "GmcDevice.h"
|
#include "GmcDevice.h"
|
||||||
|
|
||||||
GmcDevice::GmcDevice(const string& serialPort, int baud) {
|
GmcDevice::GmcDevice(const string &serialPort, int baud) {
|
||||||
device = new SerialPort(serialPort, baud);
|
device = new SerialPort(serialPort, baud);
|
||||||
|
|
||||||
if(!device->isOpen()) {
|
if (!device->isOpen()) {
|
||||||
cout << "Failed to open gmc device!" << endl;
|
cout << "Failed to open gmc device!" << endl;
|
||||||
} else {
|
} else {
|
||||||
cout << "Connection to gmc device established!" << endl;
|
cout << "Connection to gmc device established!" << endl;
|
||||||
if(!setHeartbeatOff()) {
|
if (!setHeartbeatOff()) {
|
||||||
cout << "Failed to disable heartbeat!" << endl;
|
cout << "Failed to disable heartbeat!" << endl;
|
||||||
device->serialClose();
|
device->serialClose();
|
||||||
}
|
}
|
||||||
@ -27,16 +27,16 @@ bool GmcDevice::close() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int GmcDevice::getCPM() {
|
int GmcDevice::getCPM() {
|
||||||
if(!device->isOpen()) {
|
if (!device->isOpen()) {
|
||||||
cout << "Device is not connected, failed to read CPM!" << endl;
|
cout << "Device is not connected, failed to read CPM!" << endl;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
string cmd = "<GETCPM>>";
|
string cmd = "<GETCPM>>";
|
||||||
string result;
|
vector<uint8_t> result;
|
||||||
|
|
||||||
if(device->serialWrite(cmd) == cmd.length()) {
|
if (device->serialWrite(cmd) == cmd.length()) {
|
||||||
result = device->serialRead(2); // cpm result has size 2
|
result = device->serialRead(2); // cpm result has size 2
|
||||||
} else {
|
} else {
|
||||||
cout << "Failed to send command to device!" << endl;
|
cout << "Failed to send command to device!" << endl;
|
||||||
}
|
}
|
||||||
@ -45,47 +45,49 @@ int GmcDevice::getCPM() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
float GmcDevice::getTemperature() {
|
float GmcDevice::getTemperature() {
|
||||||
if(!device->isOpen()) {
|
if (!device->isOpen()) {
|
||||||
cout << "Device is not connected, failed to read temperature!" << endl;
|
cout << "Device is not connected, failed to read temperature!" << endl;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
string cmd = "<GETTEMP>>";
|
string cmd = "<GETTEMP>>";
|
||||||
string result;
|
vector<uint8_t> result;
|
||||||
|
|
||||||
if(device->serialWrite(cmd) == cmd.length()) {
|
if (device->serialWrite(cmd) == cmd.length()) {
|
||||||
result = device->serialRead(4); // temp result has size 4
|
result = device->serialRead(4); // temp result has size 4
|
||||||
} else {
|
} else {
|
||||||
cout << "Failed to send command to device!" << endl;
|
cout << "Failed to send command to device!" << endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
int sign = result[2] == 0 ? 1 : -1;
|
int sign = result[2] == 0 ? 1 : -1;
|
||||||
float temp = result[0];
|
float temp = result[0]; // integer part
|
||||||
temp += (float) result[1] / 10;
|
temp += static_cast<float>(result[1] / 10.0); // float part
|
||||||
temp = temp * sign;
|
temp = temp * sign;
|
||||||
return temp;
|
return temp;
|
||||||
}
|
}
|
||||||
|
|
||||||
string GmcDevice::getVersion() {
|
string GmcDevice::getVersion() {
|
||||||
if(!device->isOpen()) {
|
if (!device->isOpen()) {
|
||||||
cout << "Device is not connected, failed to read version!" << endl;
|
cout << "Device is not connected, failed to read version!" << endl;
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
string cmd = "<GETVER>>";
|
string cmd = "<GETVER>>";
|
||||||
string result;
|
vector<uint8_t> result;
|
||||||
|
|
||||||
if(device->serialWrite(cmd) == cmd.length()) {
|
if (device->serialWrite(cmd) == cmd.length()) {
|
||||||
result = device->serialRead(14); // version result has size 14
|
result = device->serialRead(14); // version result has size 14
|
||||||
} else {
|
} else {
|
||||||
cout << "Failed to send command to device!" << endl;
|
cout << "Failed to send command to device!" << endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
string strResult(result.begin(), result.end());
|
||||||
|
|
||||||
|
return strResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool GmcDevice::setHeartbeatOff() {
|
bool GmcDevice::setHeartbeatOff() {
|
||||||
if(!device->isOpen()) {
|
if (!device->isOpen()) {
|
||||||
cout << "Device is not connected, failed to disable heartbeat!" << endl;
|
cout << "Device is not connected, failed to disable heartbeat!" << endl;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -93,8 +95,8 @@ bool GmcDevice::setHeartbeatOff() {
|
|||||||
string cmd = "<HEARTBEAT0>>";
|
string cmd = "<HEARTBEAT0>>";
|
||||||
string result;
|
string result;
|
||||||
|
|
||||||
if(device->serialWrite(cmd) == cmd.length()) {
|
if (device->serialWrite(cmd) == cmd.length()) {
|
||||||
return device->clearInput(100); // clear 100 chars
|
return device->clearInput(100); // clear 100 chars
|
||||||
} else {
|
} else {
|
||||||
cout << "Failed to send command to device!" << endl;
|
cout << "Failed to send command to device!" << endl;
|
||||||
}
|
}
|
||||||
|
@ -11,15 +11,20 @@ class GmcDevice {
|
|||||||
SerialPort *device;
|
SerialPort *device;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
GmcDevice(const string& serialPort, int baud);
|
GmcDevice(const string &serialPort, int baud);
|
||||||
|
|
||||||
~GmcDevice();
|
~GmcDevice();
|
||||||
|
|
||||||
bool close();
|
bool close();
|
||||||
|
|
||||||
bool isConnected();
|
bool isConnected();
|
||||||
|
|
||||||
int getCPM();
|
int getCPM();
|
||||||
|
|
||||||
float getTemperature();
|
float getTemperature();
|
||||||
|
|
||||||
string getVersion();
|
string getVersion();
|
||||||
|
|
||||||
bool setHeartbeatOff();
|
bool setHeartbeatOff();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -60,12 +60,12 @@ SerialPort::~SerialPort() {
|
|||||||
serialClose();
|
serialClose();
|
||||||
}
|
}
|
||||||
|
|
||||||
string SerialPort::serialRead(int length) {
|
vector<uint8_t> SerialPort::serialRead(int length) {
|
||||||
char *buf = (char *) calloc(length + 1, sizeof(char));
|
auto *buf = reinterpret_cast<uint8_t *> (calloc(length, sizeof(uint8_t)));
|
||||||
|
|
||||||
read(fd, buf, length);
|
read(fd, buf, length);
|
||||||
|
|
||||||
string result(buf);
|
vector<uint8_t> result(buf, buf + length);
|
||||||
free(buf);
|
free(buf);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
@ -7,6 +7,7 @@
|
|||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#include <termios.h> // POSIX terminal control definitions
|
#include <termios.h> // POSIX terminal control definitions
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
@ -19,19 +20,24 @@ class SerialPort {
|
|||||||
struct termios tio;
|
struct termios tio;
|
||||||
int tioBaud;
|
int tioBaud;
|
||||||
|
|
||||||
int getTioBaud(int);
|
static int getTioBaud(int);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// Constructor
|
// Constructor
|
||||||
SerialPort();
|
SerialPort();
|
||||||
SerialPort(const string&, int);
|
|
||||||
|
SerialPort(const string &, int);
|
||||||
|
|
||||||
~SerialPort();
|
~SerialPort();
|
||||||
|
|
||||||
bool isOpen();
|
bool isOpen();
|
||||||
|
|
||||||
bool serialClose();
|
bool serialClose();
|
||||||
|
|
||||||
string serialRead(int);
|
vector<uint8_t> serialRead(int);
|
||||||
int serialWrite(const string&);
|
|
||||||
|
int serialWrite(const string &);
|
||||||
|
|
||||||
bool clearInput(int);
|
bool clearInput(int);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user