Commit a1ad3731347ceaf04721e75edaae9feb818c3ebe
1 parent
2b90d947
implementing the RTU
Showing
3 changed files
with
104 additions
and
18 deletions
src/connectionconfig.h
| @@ -7,6 +7,7 @@ | @@ -7,6 +7,7 @@ | ||
| 7 | #pragma once | 7 | #pragma once |
| 8 | 8 | ||
| 9 | #include <string> | 9 | #include <string> |
| 10 | +#include <termios.h> | ||
| 10 | 11 | ||
| 11 | namespace osdev { | 12 | namespace osdev { |
| 12 | namespace components { | 13 | namespace components { |
| @@ -29,19 +30,6 @@ public: | @@ -29,19 +30,6 @@ public: | ||
| 29 | NONE | 30 | NONE |
| 30 | }; | 31 | }; |
| 31 | 32 | ||
| 32 | - enum class E_BAUDRATE | ||
| 33 | - { | ||
| 34 | - R1200 = 1200, | ||
| 35 | - R2400 = 2400, | ||
| 36 | - R4800 = 4800, | ||
| 37 | - R9600 = 9600, | ||
| 38 | - R19200 = 19200, | ||
| 39 | - R38400 = 38400, | ||
| 40 | - R57600 = 57600, | ||
| 41 | - R76800 = 76800, | ||
| 42 | - R115200 = 115200 | ||
| 43 | - }; | ||
| 44 | - | ||
| 45 | ConnectionConfig(); | 33 | ConnectionConfig(); |
| 46 | 34 | ||
| 47 | // Getters and Setters | 35 | // Getters and Setters |
| @@ -51,8 +39,8 @@ public: | @@ -51,8 +39,8 @@ public: | ||
| 51 | void setPortName(const std::string &port_name) { m_portName = port_name; } | 39 | void setPortName(const std::string &port_name) { m_portName = port_name; } |
| 52 | std::string getPortName() { return m_portName; } | 40 | std::string getPortName() { return m_portName; } |
| 53 | 41 | ||
| 54 | - void setBaudRate( E_BAUDRATE baud_rate ) { m_baudRate = baud_rate; } | ||
| 55 | - E_BAUDRATE getBaudRate() const { return m_baudRate; } | 42 | + void setBaudRate( speed_t baud_rate ) { m_baudRate = baud_rate; } |
| 43 | + speed_t getBaudRate() const { return m_baudRate; } | ||
| 56 | 44 | ||
| 57 | void setParity(E_PARITY parity) { m_parity = parity; } | 45 | void setParity(E_PARITY parity) { m_parity = parity; } |
| 58 | E_PARITY getParity() const { return m_parity; } | 46 | E_PARITY getParity() const { return m_parity; } |
| @@ -74,8 +62,8 @@ public: | @@ -74,8 +62,8 @@ public: | ||
| 74 | 62 | ||
| 75 | private: | 63 | private: |
| 76 | E_CONNECTIONTYPE m_conType = E_CONNECTIONTYPE::UNKNOWN; | 64 | E_CONNECTIONTYPE m_conType = E_CONNECTIONTYPE::UNKNOWN; |
| 77 | - std::string m_portName; | ||
| 78 | - E_BAUDRATE m_baudRate = E_BAUDRATE::R9600; | 65 | + std::string m_portName = "/dev/ttyUSB0"; |
| 66 | + speed_t m_baudRate = B9600; | ||
| 79 | E_PARITY m_parity = E_PARITY::NONE; | 67 | E_PARITY m_parity = E_PARITY::NONE; |
| 80 | int m_dataBits = 8; | 68 | int m_dataBits = 8; |
| 81 | int m_stopBits = 1; | 69 | int m_stopBits = 1; |
src/modbusrtu.cpp
| @@ -4,3 +4,96 @@ | @@ -4,3 +4,96 @@ | ||
| 4 | * This source code is licensed under the MIT license found in the | 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. | 5 | * LICENSE file in the root directory of this source tree. |
| 6 | */ | 6 | */ |
| 7 | + | ||
| 8 | +#include "modbusrtu.h" | ||
| 9 | + | ||
| 10 | +#include <stdio.h> | ||
| 11 | + | ||
| 12 | +// Linux headers | ||
| 13 | +#include <fcntl.h> // Contains file controls like O_RDWR | ||
| 14 | +#include <errno.h> // Error integer and strerror() function | ||
| 15 | +#include <termios.h> // Contains POSIX terminal control definitions | ||
| 16 | +#include <unistd.h> // write(), read(), close() | ||
| 17 | + | ||
| 18 | +using namespace osdev::components::modbus; | ||
| 19 | + | ||
| 20 | +ModbusRtu::ModbusRtu( const ConnectionConfig &conf ) | ||
| 21 | + : m_conConfig( conf ) | ||
| 22 | + , m_socket( -1 ) | ||
| 23 | +{ | ||
| 24 | +} | ||
| 25 | + | ||
| 26 | +bool ModbusRtu::Connect() | ||
| 27 | +{ | ||
| 28 | + m_socket = open( m_conConfig.getPortName().c_str(), O_RDWR); | ||
| 29 | + if(m_socket == -1) | ||
| 30 | + { | ||
| 31 | + return false; | ||
| 32 | + } | ||
| 33 | + | ||
| 34 | + struct termios l_tty; | ||
| 35 | + | ||
| 36 | + // Read in existing settings, and handle any error | ||
| 37 | + if( tcgetattr(m_socket, &l_tty) != 0 ) | ||
| 38 | + { | ||
| 39 | + // Replace later on with a logger line. | ||
| 40 | + return false; | ||
| 41 | + } | ||
| 42 | + | ||
| 43 | + /* Control modes */ | ||
| 44 | + l_tty.c_cflag &= ~PARENB; // Clear parity bit, disabling parity (most common) | ||
| 45 | + l_tty.c_cflag &= ~CSTOPB; // Clear stop field, only one stop bit used in communication (most common) | ||
| 46 | + l_tty.c_cflag &= ~CSIZE; // Clear all bits that set the data field | ||
| 47 | + l_tty.c_cflag |= CS8; // 8 bits per byte (most common) | ||
| 48 | + l_tty.c_cflag &= ~CRTSCTS; // Disable RTS / CTS hardware flow control (most common) | ||
| 49 | + l_tty.c_cflag |= CREAD | CLOCAL; // Turn on READ & ignore ctrl lines (CLOCAL = 1) | ||
| 50 | + | ||
| 51 | + /* local modes */ | ||
| 52 | + l_tty.c_lflag &= ~ICANON; // ..... Yes.. | ||
| 53 | + l_tty.c_lflag &= ~ECHO; // Disable echo | ||
| 54 | + l_tty.c_lflag &= ~ECHOE; // Disable Erasure | ||
| 55 | + l_tty.c_lflag &= ~ECHONL; // Diasble new-line echo | ||
| 56 | + l_tty.c_lflag &= ~ISIG; // Disable interpretation of INTS, QUIT and SUSP | ||
| 57 | + l_tty.c_lflag &= ~(IXON | IXOFF | IXANY ); // Turn off s/w flow control | ||
| 58 | + | ||
| 59 | + /* Ouput modes */ | ||
| 60 | + l_tty.c_oflag &= ~OPOST; // Prevent special interpretation of output bytes (e.g. newline chars) | ||
| 61 | + l_tty.c_oflag &= ~ONLCR; // Prevent conversion of newline to carriage return/line feed | ||
| 62 | + | ||
| 63 | + // Wait for up to 1 seconds ( 10 deciseconds), returning as soon as any data is received. | ||
| 64 | + l_tty.c_cc[VTIME] = 10; | ||
| 65 | + l_tty.c_cc[VMIN] = 0; | ||
| 66 | + | ||
| 67 | + // Set the read and write baudrate. | ||
| 68 | + cfsetispeed(&l_tty, m_conConfig.getBaudRate()); | ||
| 69 | + cfsetospeed(&l_tty, m_conConfig.getBaudRate()); | ||
| 70 | + | ||
| 71 | + // Save the tty settings. | ||
| 72 | + if( tcsetattr(m_socket, TCSANOW, &l_tty) != 0 ) | ||
| 73 | + { | ||
| 74 | + Close(); | ||
| 75 | + return false; | ||
| 76 | + } | ||
| 77 | + | ||
| 78 | + return true; | ||
| 79 | +} | ||
| 80 | + | ||
| 81 | +bool ModbusRtu::Close() | ||
| 82 | +{ | ||
| 83 | + if( close(m_socket) != 0 ) | ||
| 84 | + { | ||
| 85 | + return false; | ||
| 86 | + } | ||
| 87 | + return true; | ||
| 88 | +} | ||
| 89 | + | ||
| 90 | +int ModbusRtu::modbusSend(uint8_t *to_send, size_t length) | ||
| 91 | +{ | ||
| 92 | + return write(m_socket, to_send, length); | ||
| 93 | + | ||
| 94 | +} | ||
| 95 | + | ||
| 96 | +int ModbusRtu::modbusReceive(uint8_t *buffer) | ||
| 97 | +{ | ||
| 98 | + return read(m_socket, buffer, sizeof(*buffer)); | ||
| 99 | +} |
src/modbusrtu.h
| @@ -6,6 +6,7 @@ | @@ -6,6 +6,7 @@ | ||
| 6 | */ | 6 | */ |
| 7 | 7 | ||
| 8 | #include "modbusbase.h" | 8 | #include "modbusbase.h" |
| 9 | +#include "connectionconfig.h" | ||
| 9 | 10 | ||
| 10 | namespace osdev { | 11 | namespace osdev { |
| 11 | namespace components { | 12 | namespace components { |
| @@ -14,6 +15,9 @@ namespace modbus { | @@ -14,6 +15,9 @@ namespace modbus { | ||
| 14 | class ModbusRtu : public ModbusBase | 15 | class ModbusRtu : public ModbusBase |
| 15 | { | 16 | { |
| 16 | public: | 17 | public: |
| 18 | + ModbusRtu( const ConnectionConfig &conf ); | ||
| 19 | + virtual ~ModbusRtu() {} | ||
| 20 | + | ||
| 17 | virtual bool Connect() override; | 21 | virtual bool Connect() override; |
| 18 | virtual bool Close() override; | 22 | virtual bool Close() override; |
| 19 | 23 | ||
| @@ -33,7 +37,8 @@ public: | @@ -33,7 +37,8 @@ public: | ||
| 33 | virtual int modbusReceive(uint8_t *buffer) override; | 37 | virtual int modbusReceive(uint8_t *buffer) override; |
| 34 | 38 | ||
| 35 | private: | 39 | private: |
| 36 | - | 40 | + ConnectionConfig m_conConfig; |
| 41 | + int m_socket; | ||
| 37 | }; | 42 | }; |
| 38 | 43 | ||
| 39 | } /* End namespace modbus */ | 44 | } /* End namespace modbus */ |