aboutsummaryrefslogtreecommitdiff
path: root/SerialPort/SerialPort.cpp
diff options
context:
space:
mode:
authorJán Sučan <sucanjan@fit.cvut.cz>2017-05-10 15:13:29 +0200
committerJán Sučan <sucanjan@fit.cvut.cz>2017-05-10 15:13:29 +0200
commit02e24f0f533fe904c3a5275c4060c10c38d7c17a (patch)
tree19d05c60e3d6a6782c4712de960a8f6705054063 /SerialPort/SerialPort.cpp
Uvodny commit, subory su rovnake ako na CD prilozenom k vytlacenemu texu bakalarskej prace, naviac je pridany len subor LICENCIA
Diffstat (limited to 'SerialPort/SerialPort.cpp')
-rwxr-xr-xSerialPort/SerialPort.cpp157
1 files changed, 157 insertions, 0 deletions
diff --git a/SerialPort/SerialPort.cpp b/SerialPort/SerialPort.cpp
new file mode 100755
index 0000000..93e9d20
--- /dev/null
+++ b/SerialPort/SerialPort.cpp
@@ -0,0 +1,157 @@
+#include "SerialPort.hpp"
+#include "Logger.hpp"
+#include "ExitException.hpp"
+#include "ExitCodes.hpp"
+#include <cstring>
+#include <sstream>
+
+using std::ostringstream;
+
+#define READ_DEFAULT_TIMEOUT 3000 // ms
+
+void
+CSerialPort::writeWord(uint16_t w)
+{
+ uint8_t b;
+
+ b = (w & 0xff);
+ this->write(&b, 1, 1);
+
+ b = (w & 0xff00) >> 8;
+ this->write(&b, 1, 1);
+}
+
+uint16_t
+CSerialPort::readWord()
+{
+ uint8_t b;
+ uint16_t w;
+
+ w = 0;
+
+ this->read(&b, 1);
+ w |= ((uint16_t) b);
+
+ this->read(&b, 1);
+ w |= ((uint16_t) b) << 8;
+
+ return w;
+}
+
+uint32_t
+CSerialPort::readDoubleWord()
+{
+ uint8_t b;
+ uint32_t dw;
+
+ dw = 0;
+
+ this->read(&b, 1);
+ dw |= ((uint32_t) b);
+
+ this->read(&b, 1);
+ dw |= ((uint32_t) b) << 8;
+
+ this->read(&b, 1);
+ dw |= ((uint32_t) b) << 16;
+
+ this->read(&b, 1);
+ dw |= ((uint32_t) b) << 24;
+
+ return dw;
+}
+
+void
+CSerialPort::setReadTimeout(int timetout)
+{
+ mReadTimeoutMs = timetout;
+}
+
+void
+CSerialPort::setDefaultTimeout()
+{
+ mReadTimeoutMs = READ_DEFAULT_TIMEOUT;
+}
+
+void
+CSerialPort::write(uint8_t *data, int data_length, int padd_to)
+{
+ // Write data
+ for (int i = 0; i < data_length; )
+ i += writeSingle(data, data_length);
+ // Write pad
+ // TODO: odkial brat hodnotu pad byte??
+ uint8_t p[512];
+ memset(p, 0xFF, 512);
+
+ for (padd_to -= data_length; padd_to > 0; ) {
+ int w = 512;
+ if (padd_to < 512)
+ w = padd_to;
+ padd_to -= writeSingle(p, w);
+ }
+}
+
+void
+CSerialPort::read(uint8_t *data, int data_length)
+{
+ for (int i = 0; i < data_length; ) {
+ int old_i = i;
+ i += readSingle(data + i, data_length - i);
+ // TODO: pre Win32, pretoze tam nedetekujeme timeout a moze vracat 0 bajtov precitanych
+ if (i == old_i) {
+ CLogger::error("Timeout occured while reading data from serial port", EXIT_SERIAL_PORT);
+ }
+ }
+}
+
+void
+CSerialPort::sendSafeByte(uint8_t b)
+{
+ uint8_t r;
+
+ this->write(&b, 1, 1);
+ this->read(&r, 1);
+ if (r != b) {
+ ostringstream os;
+ os << "Bad echo when sending byte safely, expected " << CLogger::decToHex(b);
+ os << " received " << CLogger::decToHex(r);
+ CLogger::error(os.str(), EXIT_SERIAL_PORT);
+ }
+ this->write(&b, 1, 1);
+ this->read(&r, 1);
+ if (r != 0x00) {
+ ostringstream os;
+ os << "Cannot send byte safely, expected 0x00 received " << CLogger::decToHex(r);
+ CLogger::error(os.str(), EXIT_SERIAL_PORT);
+ }
+}
+
+void
+CSerialPort::sendSafeWord(uint16_t w)
+{
+ this->writeWord(w);
+ uint16_t r = this->readWord();
+ if (r != w) {
+ ostringstream os;
+ os << "Bad echo when sending word safely, expected " << CLogger::decToHex(w);
+ os << " received " << CLogger::decToHex(r);
+ CLogger::error(os.str(), EXIT_SERIAL_PORT);
+ }
+ this->writeWord(w);
+ r = this->readWord();
+ if (r != 0x00) {
+ ostringstream os;
+ os << "Cannot send word safely, expected 0x0000 received " << CLogger::decToHex(r);
+ CLogger::error(os.str(), EXIT_SERIAL_PORT);
+ }
+}
+
+void
+CSerialPort::sendSafeDoubleWord(uint32_t w)
+{
+ // Send low word
+ sendSafeWord(w & 0x0000FFFF);
+ // Send high word
+ sendSafeWord((w & 0xFFFF0000) >> 16);
+}