diff --git a/src/connectionconfig.h b/src/connectionconfig.h index 0badc21..e6bc562 100644 --- a/src/connectionconfig.h +++ b/src/connectionconfig.h @@ -7,6 +7,7 @@ #pragma once #include +#include namespace osdev { namespace components { @@ -29,19 +30,6 @@ public: NONE }; - enum class E_BAUDRATE - { - R1200 = 1200, - R2400 = 2400, - R4800 = 4800, - R9600 = 9600, - R19200 = 19200, - R38400 = 38400, - R57600 = 57600, - R76800 = 76800, - R115200 = 115200 - }; - ConnectionConfig(); // Getters and Setters @@ -51,8 +39,8 @@ public: void setPortName(const std::string &port_name) { m_portName = port_name; } std::string getPortName() { return m_portName; } - void setBaudRate( E_BAUDRATE baud_rate ) { m_baudRate = baud_rate; } - E_BAUDRATE getBaudRate() const { return m_baudRate; } + void setBaudRate( speed_t baud_rate ) { m_baudRate = baud_rate; } + speed_t getBaudRate() const { return m_baudRate; } void setParity(E_PARITY parity) { m_parity = parity; } E_PARITY getParity() const { return m_parity; } @@ -74,8 +62,8 @@ public: private: E_CONNECTIONTYPE m_conType = E_CONNECTIONTYPE::UNKNOWN; - std::string m_portName; - E_BAUDRATE m_baudRate = E_BAUDRATE::R9600; + std::string m_portName = "/dev/ttyUSB0"; + speed_t m_baudRate = B9600; E_PARITY m_parity = E_PARITY::NONE; int m_dataBits = 8; int m_stopBits = 1; diff --git a/src/modbusrtu.cpp b/src/modbusrtu.cpp index 46376c0..bcbfd53 100644 --- a/src/modbusrtu.cpp +++ b/src/modbusrtu.cpp @@ -4,3 +4,96 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ + +#include "modbusrtu.h" + +#include + +// Linux headers +#include // Contains file controls like O_RDWR +#include // Error integer and strerror() function +#include // Contains POSIX terminal control definitions +#include // write(), read(), close() + +using namespace osdev::components::modbus; + +ModbusRtu::ModbusRtu( const ConnectionConfig &conf ) + : m_conConfig( conf ) + , m_socket( -1 ) +{ +} + +bool ModbusRtu::Connect() +{ + m_socket = open( m_conConfig.getPortName().c_str(), O_RDWR); + if(m_socket == -1) + { + return false; + } + + struct termios l_tty; + + // Read in existing settings, and handle any error + if( tcgetattr(m_socket, &l_tty) != 0 ) + { + // Replace later on with a logger line. + return false; + } + + /* Control modes */ + l_tty.c_cflag &= ~PARENB; // Clear parity bit, disabling parity (most common) + l_tty.c_cflag &= ~CSTOPB; // Clear stop field, only one stop bit used in communication (most common) + l_tty.c_cflag &= ~CSIZE; // Clear all bits that set the data field + l_tty.c_cflag |= CS8; // 8 bits per byte (most common) + l_tty.c_cflag &= ~CRTSCTS; // Disable RTS / CTS hardware flow control (most common) + l_tty.c_cflag |= CREAD | CLOCAL; // Turn on READ & ignore ctrl lines (CLOCAL = 1) + + /* local modes */ + l_tty.c_lflag &= ~ICANON; // ..... Yes.. + l_tty.c_lflag &= ~ECHO; // Disable echo + l_tty.c_lflag &= ~ECHOE; // Disable Erasure + l_tty.c_lflag &= ~ECHONL; // Diasble new-line echo + l_tty.c_lflag &= ~ISIG; // Disable interpretation of INTS, QUIT and SUSP + l_tty.c_lflag &= ~(IXON | IXOFF | IXANY ); // Turn off s/w flow control + + /* Ouput modes */ + l_tty.c_oflag &= ~OPOST; // Prevent special interpretation of output bytes (e.g. newline chars) + l_tty.c_oflag &= ~ONLCR; // Prevent conversion of newline to carriage return/line feed + + // Wait for up to 1 seconds ( 10 deciseconds), returning as soon as any data is received. + l_tty.c_cc[VTIME] = 10; + l_tty.c_cc[VMIN] = 0; + + // Set the read and write baudrate. + cfsetispeed(&l_tty, m_conConfig.getBaudRate()); + cfsetospeed(&l_tty, m_conConfig.getBaudRate()); + + // Save the tty settings. + if( tcsetattr(m_socket, TCSANOW, &l_tty) != 0 ) + { + Close(); + return false; + } + + return true; +} + +bool ModbusRtu::Close() +{ + if( close(m_socket) != 0 ) + { + return false; + } + return true; +} + +int ModbusRtu::modbusSend(uint8_t *to_send, size_t length) +{ + return write(m_socket, to_send, length); + +} + +int ModbusRtu::modbusReceive(uint8_t *buffer) +{ + return read(m_socket, buffer, sizeof(*buffer)); +} diff --git a/src/modbusrtu.h b/src/modbusrtu.h index 1cd4609..e7b4263 100644 --- a/src/modbusrtu.h +++ b/src/modbusrtu.h @@ -6,6 +6,7 @@ */ #include "modbusbase.h" +#include "connectionconfig.h" namespace osdev { namespace components { @@ -14,6 +15,9 @@ namespace modbus { class ModbusRtu : public ModbusBase { public: + ModbusRtu( const ConnectionConfig &conf ); + virtual ~ModbusRtu() {} + virtual bool Connect() override; virtual bool Close() override; @@ -33,7 +37,8 @@ public: virtual int modbusReceive(uint8_t *buffer) override; private: - + ConnectionConfig m_conConfig; + int m_socket; }; } /* End namespace modbus */