reading partially works

This commit is contained in:
Christoph Haas 2019-05-07 23:39:30 +02:00
parent 724b025795
commit 5543d707df
4 changed files with 35 additions and 7 deletions

View File

@ -33,6 +33,12 @@ int GcLogger::run() {
string deviceVersion = device->getVersion();
cout << "GMC Device version: " << deviceVersion << endl;
int cpm = device->getCPM();
cout << "CPM: " << cpm << endl;
float temperature = device->getTemperature();
cout << "Temperature: " << temperature << endl;
device->close();
return EXIT_SUCCESS;
}

View File

@ -36,7 +36,9 @@ int GmcDevice::getCPM() {
string result;
if(device->serialWrite(cmd) == cmd.length()) {
device->serialRead(result, 2); // cpm result has size 2
result = device->serialRead(2); // cpm result has size 2
} else {
cout << "Failed to send command to device!" << endl;
}
return result[0] * 256 + result[1];
@ -52,7 +54,9 @@ float GmcDevice::getTemperature() {
string result;
if(device->serialWrite(cmd) == cmd.length()) {
device->serialRead(result, 4); // temp result has size 4
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;
@ -72,7 +76,9 @@ string GmcDevice::getVersion() {
string result;
if(device->serialWrite(cmd) == cmd.length()) {
device->serialRead(result, 14); // version result has size 14
result = device->serialRead(14); // version result has size 14
} else {
cout << "Failed to send command to device!" << endl;
}
return result;
@ -84,6 +90,15 @@ bool GmcDevice::setHeartbeatOff() {
return false;
}
string cmd = "<HEARTBEAT0>>";
string result;
if(device->serialWrite(cmd) == cmd.length()) {
return device->clearInput(100); // clear 100 chars
} else {
cout << "Failed to send command to device!" << endl;
}
return false;
}

View File

@ -60,11 +60,18 @@ SerialPort::~SerialPort() {
serialClose();
}
int SerialPort::serialRead(string buffer, int length) {
return read(fd, buffer.data(), length);
string SerialPort::serialRead(int length) {
char *buf = (char *) calloc(length + 1, sizeof(char));
read(fd, buf, length);
string result(buf);
free(buf);
return result;
}
int SerialPort::serialWrite(const string& data) {
int SerialPort::serialWrite(const string &data) {
return write(fd, data.c_str(), data.length());
}

View File

@ -30,7 +30,7 @@ public:
bool isOpen();
bool serialClose();
int serialRead(string, int);
string serialRead(int);
int serialWrite(const string&);
bool clearInput(int);
};