read version of gmc device

This commit is contained in:
Christoph Haas 2019-05-07 23:14:21 +02:00
parent dea4e8253e
commit 724b025795
8 changed files with 275 additions and 11 deletions

@ -1 +1 @@
Subproject commit a12081742209b122108c159517259c16e2335345
Subproject commit da3ae5feb5353fce48cadc34b6ea79de5ac05ca1

View File

@ -1,8 +1,7 @@
# Notice name prefix of this variable, set by CMake according
# to value given with "project()" in the root CMakeLists.txt.
include_directories(${gclogger_SOURCE_DIR}/include)
add_executable(gclogger main.cpp GcLogger.cpp GcLogger.h)
add_executable(gclogger main.cpp GcLogger.cpp GcLogger.h SerialPort.cpp SerialPort.h GmcDevice.cpp GmcDevice.h)
# I assume you want to use LibProject as a library in MainProject.
include_directories(${gclogger_SOURCE_DIR}/libs)
link_directories(${gclogger_SOURCE_DIR}/libs)

View File

@ -6,12 +6,20 @@
#include "GcLogger.h"
void GcLogger::setup(std::string serialPort, int baudRate) {
this->baudRate = baudRate;
this->serialPort = serialPort;
this->isSetup = true;
void GcLogger::setup(const string& deviceSerialPort, int deviceBaudRate) {
baudRate = deviceBaudRate;
serialPort = deviceSerialPort;
cout << "Serial port configuration: port=" << serialPort << " baud=" << baudRate << endl;
cout << "Serial port configuration: port=" << deviceSerialPort << " baud=" << deviceBaudRate << endl;
device = new GmcDevice(deviceSerialPort, deviceBaudRate);
if(!device->isConnected()) {
cout << "Failed to connect to device!" << endl;
} else {
cout << "Device connected!" << endl;
isSetup = true;
}
}
int GcLogger::run() {
@ -22,11 +30,15 @@ int GcLogger::run() {
} else {
cout << "Running gclogger!" << endl;
string deviceVersion = device->getVersion();
cout << "GMC Device version: " << deviceVersion << endl;
device->close();
return EXIT_SUCCESS;
}
}
void GcLogger::readIni(string filePath) {
void GcLogger::readIni(const string& filePath) {
cout << "Reading configuration from: " << filePath << endl;
ifstream is(filePath);

View File

@ -9,6 +9,7 @@
#include <iostream>
#include <string>
#include "GmcDevice.h"
#include "inipp/inipp/inipp.h"
using namespace std;
@ -18,11 +19,12 @@ class GcLogger {
string serialPort;
int baudRate;
inipp::Ini<char> ini;
GmcDevice *device;
void setup(string, int);
void setup(const string&, int);
public:
void readIni(string);
void readIni(const string&);
int run();
};

92
src/GmcDevice.cpp Normal file
View File

@ -0,0 +1,92 @@
//
// 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 = "<GETCPM>>";
string result;
if(device->serialWrite(cmd) == cmd.length()) {
device->serialRead(result, 2); // cpm result has size 2
}
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 = "<GETTEMP>>";
string result;
if(device->serialWrite(cmd) == cmd.length()) {
device->serialRead(result, 4); // temp result has size 4
}
int sign = result[2] == 0 ? 1 : -1;
float temp = result[0];
temp += (float) result[1] / 10;
temp = temp * sign;
return temp;
}
string GmcDevice::getVersion() {
if(!device->isOpen()) {
cout << "Device is not connected, failed to read version!" << endl;
return "";
}
string cmd = "<GETVER>>";
string result;
if(device->serialWrite(cmd) == cmd.length()) {
device->serialRead(result, 14); // version result has size 14
}
return result;
}
bool GmcDevice::setHeartbeatOff() {
if(!device->isOpen()) {
cout << "Device is not connected, failed to disable heartbeat!" << endl;
return false;
}
return false;
}
bool GmcDevice::isConnected() {
return device->isOpen();
}

27
src/GmcDevice.h Normal file
View File

@ -0,0 +1,27 @@
//
// Created by h44z on 07.05.19.
//
#ifndef GCLOGGER_GMCDEVICE_H
#define GCLOGGER_GMCDEVICE_H
#include "SerialPort.h"
class GmcDevice {
SerialPort *device;
public:
GmcDevice(const string& serialPort, int baud);
~GmcDevice();
bool close();
bool isConnected();
int getCPM();
float getTemperature();
string getVersion();
bool setHeartbeatOff();
};
#endif //GCLOGGER_GMCDEVICE_H

93
src/SerialPort.cpp Normal file
View File

@ -0,0 +1,93 @@
//
// Created by h44z on 07.05.19.
//
#include "SerialPort.h"
SerialPort::SerialPort() {
fd = -1;
tioBaud = B0;
tio.c_cflag = CS8 | CREAD | CLOCAL; // 8n1
tio.c_cc[VMIN] = 0;
tio.c_cc[VTIME] = 5;
}
int SerialPort::getTioBaud(int baud) {
switch (baud) {
case 9600:
return B9600;
case 19200:
return B19200;
case 38400:
return B38400;
case 57600:
return B57600;
case 115200:
return B115200;
default:
return B0;
}
}
SerialPort::SerialPort(const string &serialPort, int baud) : SerialPort() {
tioBaud = getTioBaud(baud);
fd = open(serialPort.c_str(), O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1) { // if open is unsucessful
cout << "Unable to open serial port: " << serialPort << endl;
} else {
fcntl(fd, F_SETFL, 0);
cout << "Serial port opened: " << serialPort << endl;
if (cfsetspeed(&tio, tioBaud) == 0) { // set baud speed
if (tcsetattr(fd, TCSANOW, &tio) == 0) { // apply baud speed change
cout << "Serial port initialized successfully to BAUD: " << baud << " (" << tioBaud << ")" << endl;
}
} else {
// something failed
close(fd);
fd = -1;
}
}
}
bool SerialPort::isOpen() {
return fd != -1;
}
SerialPort::~SerialPort() {
serialClose();
}
int SerialPort::serialRead(string buffer, int length) {
return read(fd, buffer.data(), length);
}
int SerialPort::serialWrite(const string& data) {
return write(fd, data.c_str(), data.length());
}
bool SerialPort::clearInput(int size) {
char ch;
// flush input stream
for (int i = 0; i < size; i++) {
if (read(fd, &ch, 1) == 0)
return true; // found end of stream
}
return false; // still data left in the buffer
}
bool SerialPort::serialClose() {
cout << "Closing serial port" << endl;
if (isOpen()) {
bool result = close(fd) == 0;
fd = -1;
return result;
}
return false;
}

39
src/SerialPort.h Normal file
View File

@ -0,0 +1,39 @@
//
// Created by h44z on 07.05.19.
//
#ifndef GCLOGGER_SERIALPORT_H
#define GCLOGGER_SERIALPORT_H
#include <iostream>
#include <string>
#include <termios.h> // POSIX terminal control definitions
#include <unistd.h>
#include <fcntl.h>
using namespace std;
class SerialPort {
int fd;
struct termios tio;
int tioBaud;
int getTioBaud(int);
public:
// Constructor
SerialPort();
SerialPort(const string&, int);
~SerialPort();
bool isOpen();
bool serialClose();
int serialRead(string, int);
int serialWrite(const string&);
bool clearInput(int);
};
#endif //GCLOGGER_SERIALPORT_H