├── .gitignore ├── LICENSE ├── README.md ├── example └── exam.c └── src ├── IFinsCommand.h ├── ITransport.h ├── fins.cpp ├── fins.h ├── tcpFinsCommand.cpp ├── tcpFinsCommand.h ├── tcpTransport.cpp ├── tcpTransport.h ├── udpFinsCommand.cpp ├── udpFinsCommand.h ├── udpTransport.cpp └── udpTransport.h /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 nsicko42 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # libfinscpp 2 | Omron FINS Client Library 3 | ============= 4 | 5 | This is a C++ Implementation of [C# OMRON PLC TCP Interface (from github)](https://github.com/mcNets/mcOmron). 6 | -------------------------------------------------------------------------------- /example/exam.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "fins.h" 4 | 5 | using namespace OmronPlc; 6 | 7 | int main() 8 | { 9 | Fins fins(TransportType::Udp); 10 | 11 | // Set Remote Node, but it is not nessecery when connect with Hostlink 12 | // 13 | fins.SetRemote("192.168.0.1"); 14 | fins.Connect(); 15 | 16 | fins.MemoryAreaRead(MemoryArea::DM, 1000, 0, 1); 17 | 18 | fins.Close(); 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /src/IFinsCommand.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | namespace OmronPlc 8 | { 9 | enum FinsCommandField : unsigned char 10 | { 11 | ICF, 12 | RSC, 13 | GTC, 14 | DNA, 15 | DA1, 16 | DA2, 17 | SNA, 18 | SA1, 19 | SA2, 20 | SID, 21 | MC, 22 | SC, 23 | 24 | F_PARAM = 12, 25 | }; 26 | 27 | enum MemoryArea : unsigned char 28 | { 29 | CIO_Bit = 0x30, 30 | WR_Bit = 0x31, 31 | HR_Bit = 0x32, 32 | AR_Bit = 0x33, 33 | CIO_Bit_FS = 0x70, 34 | WR_Bit_FS = 0x71, 35 | HR_Bit_FS = 0x72, 36 | CIO = 0xB0, 37 | WR = 0xB1, 38 | HR = 0xB2, 39 | AR = 0xB3, 40 | CIO_FS = 0xF0, 41 | WR_FS = 0xF1, 42 | HR_FS = 0xF2, 43 | TIM = 0x09, 44 | CNT = 0x09, 45 | TIM_FS = 0x49, 46 | CNT_FS = 0x49, 47 | TIM_PV = 0x89, 48 | CNT_PV = 0x89, 49 | DM_Bit = 0x02, 50 | DM = 0x82, 51 | TK_Bit = 0x06, 52 | TK = 0x46 53 | }; 54 | 55 | enum FinsCommands 56 | { 57 | MemoryAreaRead, 58 | MemoryAreaWrite, 59 | ControllerDataRead, 60 | }; 61 | 62 | class IFinsCommand 63 | { 64 | public: 65 | uint8_t *Response; 66 | virtual ~IFinsCommand() {}; 67 | virtual bool Connect()=0; 68 | virtual void Close()=0; 69 | virtual void SetRemote(string ipaddr, uint16_t port)=0; 70 | virtual bool MemoryAreaRead(MemoryArea area, uint16_t address, uint8_t bit_position, uint16_t count) = 0; 71 | virtual bool MemoryAreaWrite(MemoryArea area, uint16_t address, uint8_t bit_position, uint16_t count, uint8_t data[]) = 0; 72 | }; 73 | 74 | } 75 | -------------------------------------------------------------------------------- /src/ITransport.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | namespace OmronPlc 8 | { 9 | class ITransport 10 | { 11 | public: 12 | bool Connected; 13 | 14 | virtual ~ITransport() {}; 15 | virtual bool Connect() = 0; 16 | virtual void Close() = 0; 17 | virtual void SetRemote(string ip, uint16_t port) = 0; 18 | virtual int Send(const uint8_t command[], int cmdLen) = 0; 19 | virtual int Receive(uint8_t response[], int respLen) = 0; 20 | }; 21 | } 22 | -------------------------------------------------------------------------------- /src/fins.cpp: -------------------------------------------------------------------------------- 1 | #include "fins.h" 2 | #include "tcpFinsCommand.h" 3 | #include "udpFinsCommand.h" 4 | #include 5 | 6 | using namespace OmronPlc; 7 | 8 | Fins::Fins(TransportType TType) 9 | { 10 | switch (TType) 11 | { 12 | case TransportType::Tcp: 13 | _finsCmd = new tcpFinsCommand(); 14 | break; 15 | case TransportType::Udp: 16 | _finsCmd = new udpFinsCommand(); 17 | break; 18 | case TransportType::Hostlink: 19 | default: 20 | throw "Transport Type Error"; 21 | } 22 | } 23 | 24 | Fins::~Fins() 25 | { 26 | } 27 | 28 | bool Fins::Connect() 29 | { 30 | _finsCmd->Connect(); 31 | return false; 32 | } 33 | 34 | void Fins::Close() 35 | { 36 | 37 | } 38 | 39 | void OmronPlc::Fins::SetRemote(string ipaddr, uint16_t port) 40 | { 41 | _finsCmd->SetRemote(ipaddr, port); 42 | } 43 | 44 | bool Fins::MemoryAreaRead(MemoryArea area, uint16_t address, uint8_t bit_position, uint16_t count) 45 | { 46 | return _finsCmd->MemoryAreaRead(area, address, bit_position, count); 47 | } 48 | 49 | bool Fins::MemoryAreaWrite(MemoryArea area, uint16_t address, uint8_t bit_position, uint16_t count, uint8_t data[]) 50 | { 51 | return _finsCmd->MemoryAreaWrite(area, address, bit_position, count, data); 52 | } 53 | 54 | bool Fins::ReadDM(uint16_t address, uint16_t & value) 55 | { 56 | if (MemoryAreaRead(DM, address, 0, 1)) return false; 57 | 58 | value = (uint16_t)(((uint)_finsCmd->Response[0] << 8) + (uint)_finsCmd->Response[1]); 59 | 60 | return true; 61 | } 62 | 63 | bool Fins::ReadDM(uint16_t address, int16_t & value) 64 | { 65 | if (MemoryAreaRead(DM, address, 0, 1)) return false; 66 | 67 | value = (int16_t)(((int)_finsCmd->Response[0] << 8) + (int)_finsCmd->Response[1]); 68 | 69 | return true; 70 | } 71 | 72 | bool Fins::ReadDM(uint16_t address, uint16_t data[], uint16_t count) 73 | { 74 | if (MemoryAreaRead(DM, address, 0, 1)) return false; 75 | 76 | for (int x = 0; x < count; ++x) 77 | { 78 | data[x] = (uint16_t)(((uint)_finsCmd->Response[x * 2] << 8) + ((uint)_finsCmd->Response[x * 2 + 1])); 79 | } 80 | 81 | return true; 82 | } 83 | 84 | bool Fins::WriteDM(uint16_t address, const uint16_t value) 85 | { 86 | uint8_t data[2]; 87 | data[0] = (uint8_t)((value >> 8) & 0xFF); 88 | data[1] = (uint8_t)(value & 0xFF); 89 | 90 | return MemoryAreaWrite(DM, address, 0, 1, data); 91 | } 92 | 93 | bool Fins::WriteDM(uint16_t address, const int16_t value) 94 | { 95 | uint8_t data[2]; 96 | data[0] = (uint8_t)((value >> 8) & 0xFF); 97 | data[1] = (uint8_t)(value & 0xFF); 98 | 99 | return MemoryAreaWrite(DM, address, 0, 1, data); 100 | } 101 | 102 | bool Fins::WriteDM(uint16_t address, uint16_t data[], uint16_t count) 103 | { 104 | uint8_t byteData[count * sizeof(uint16_t)]; 105 | 106 | for (int x = 0; x < count; ++x) 107 | { 108 | byteData[x * 2] = (uint8_t)(data[x] / 256); 109 | byteData[x * 2 + 1] = (uint8_t)(data[x] % 256); 110 | } 111 | 112 | return MemoryAreaWrite(DM, address, 0, count, byteData); 113 | } 114 | 115 | bool Fins::ClearDM(uint16_t address, uint16_t count) 116 | { 117 | // zeroed array (each DM requieres 2 bytes) 118 | // 119 | vector data; 120 | data.reserve(count * 2); 121 | data.clear(); 122 | 123 | // fins command 124 | // 125 | return MemoryAreaWrite(DM, address, 0, count, &data[0]); 126 | } 127 | 128 | bool Fins::ReadCIOBit(uint16_t address, uint8_t bit_position, bool & value) 129 | { 130 | // FINS command 131 | // 132 | if (!MemoryAreaRead(MemoryArea::CIO_Bit, address, bit_position, 1)) return false; 133 | 134 | // value 135 | // 136 | //value = BTool.BytesToUInt16(_finsCmd.Response[0], _finsCmd.Response[1]); 137 | value = (bool)_finsCmd->Response[0]; 138 | 139 | return true; 140 | } 141 | 142 | bool OmronPlc::Fins::WriteCIOBit(uint16_t address, uint8_t bit_position, const bool value) 143 | { 144 | // get the array 145 | // 146 | uint8_t data[1]; 147 | data[0] = (uint8_t)value; 148 | 149 | // fins command 150 | // 151 | return MemoryAreaWrite(MemoryArea::CIO_Bit, address, bit_position, 1, data); 152 | } 153 | -------------------------------------------------------------------------------- /src/fins.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | 5 | #include "IFinsCommand.h" 6 | #include "tcpTransport.h" 7 | 8 | #define DEFAULT_PORT 9600 9 | 10 | namespace OmronPlc 11 | { 12 | enum TransportType 13 | { 14 | Tcp, 15 | Udp, 16 | Hostlink 17 | }; 18 | 19 | 20 | class Fins 21 | { 22 | private: 23 | IFinsCommand * _finsCmd; 24 | 25 | public: 26 | 27 | Fins(TransportType TType = TransportType::Tcp); 28 | ~Fins(); 29 | 30 | bool Connect(); 31 | void Close(); 32 | void SetRemote(string ipaddr, uint16_t port=DEFAULT_PORT); 33 | 34 | bool MemoryAreaRead(MemoryArea area, uint16_t address, uint8_t bit_position, uint16_t count); 35 | bool MemoryAreaWrite(MemoryArea area, uint16_t address, uint8_t bit_position, uint16_t count, uint8_t data[]); 36 | 37 | bool ReadDM(uint16_t address, uint16_t &value); 38 | bool ReadDM(uint16_t address, int16_t &value); 39 | bool ReadDM(uint16_t address, uint16_t data[], uint16_t count); 40 | bool WriteDM(uint16_t address, const uint16_t value); 41 | bool WriteDM(uint16_t address, const int16_t value); 42 | bool WriteDM(uint16_t address, uint16_t data[], uint16_t count); 43 | bool ClearDM(uint16_t address, uint16_t count); 44 | 45 | bool ReadCIOBit(uint16_t address, uint8_t bit_position, bool &value); 46 | bool WriteCIOBit(uint16_t address, uint8_t bit_position, const bool value); 47 | }; 48 | } 49 | -------------------------------------------------------------------------------- /src/tcpFinsCommand.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "tcpFinsCommand.h" 5 | 6 | namespace OmronPlc 7 | { 8 | tcpFinsCommand::tcpFinsCommand(uint8_t ServiceID) 9 | { 10 | // transport layer 11 | // 12 | transport = new tcpTransport(); 13 | 14 | Response = &respFinsData[0]; 15 | 16 | cmdFins.reserve(22); 17 | cmdFins.empty(); 18 | 19 | // Default fins command fields 20 | // 21 | //---- COMMAND HEADER ------------------------------------------------------- 22 | cmdFins[ICF] = 0x80; // 00 ICF Information control field 23 | cmdFins[RSC] = 0x00; // 01 RSC Reserved 24 | cmdFins[GTC] = 0x02; // 02 GTC Gateway count 25 | cmdFins[DNA] = 0x00; // 03 DNA Destination network address (0=local network) 26 | cmdFins[DA1] = 0x00; // 04 DA1 Destination node number 27 | cmdFins[DA2] = 0x00; // 05 DA2 Destination unit address 28 | cmdFins[SNA] = 0x00; // 06 SNA Source network address (0=local network) 29 | cmdFins[SA1] = 0x00; // 07 SA1 Source node number 30 | cmdFins[SA2] = 0x00; // 08 SA2 Source unit address 31 | cmdFins[SID] = ServiceID; // 09 SID Service ID 32 | //---- COMMAND -------------------------------------------------------------- 33 | cmdFins[MC] = 0x00; // 10 MC Main command 34 | cmdFins[SC] = 0x00; // 11 SC Subcommand 35 | //---- PARAMS --------------------------------------------------------------- 36 | cmdFins[12] = 0x00; // 12 reserved area for additional params 37 | cmdFins[13] = 0x00; // depending on fins command 38 | cmdFins[14] = 0x00; 39 | cmdFins[15] = 0x00; 40 | cmdFins[16] = 0x00; 41 | cmdFins[17] = 0x00; 42 | cmdFins[18] = 0x00; 43 | cmdFins[19] = 0x00; 44 | cmdFins[20] = 0x00; 45 | cmdFins[21] = 0x00; 46 | 47 | cmdFS.reserve(16); 48 | cmdFS.clear(); 49 | cmdFS[0] = 0x46; // 'F' 50 | cmdFS[1] = 0x49; // 'I' 51 | cmdFS[2] = 0x4E; // 'N' 52 | cmdFS[3] = 0x53; // 'S' 53 | cmdFS[11] = 0x02; // Command FS Sending=2 / Receiving=3 54 | 55 | respFS.reserve(16); 56 | respFS.clear(); 57 | 58 | respFins.reserve(2048); 59 | respFinsData.reserve(2048); 60 | 61 | finsCommandLen = 0; 62 | finsResponseLen = 0; 63 | } 64 | 65 | tcpFinsCommand::~tcpFinsCommand() 66 | { 67 | delete transport; 68 | } 69 | 70 | bool tcpFinsCommand::Connect() 71 | { 72 | try 73 | { 74 | transport->Connect(); 75 | 76 | return NodeAddressDataSend(); 77 | } 78 | catch (const char * msg) 79 | { 80 | return false; 81 | } 82 | } 83 | 84 | void tcpFinsCommand::Close() 85 | { 86 | transport->Close(); 87 | } 88 | 89 | void tcpFinsCommand::SetRemote(string ipaddr, uint16_t port) 90 | { 91 | transport->SetRemote(ipaddr, port); 92 | } 93 | 94 | bool tcpFinsCommand::MemoryAreaRead(MemoryArea area, uint16_t address, uint8_t bit_position, uint16_t count) 95 | { 96 | cmdFins[MC] = 0x01; 97 | cmdFins[SC] = 0x01; 98 | 99 | cmdFins[F_PARAM] = area; 100 | cmdFins[F_PARAM + 1] = (uint8_t)((address >> 8) & 0xFF); 101 | cmdFins[F_PARAM + 2] = (uint8_t)(address & 0xFF); 102 | cmdFins[F_PARAM + 3] = (uint8_t)(bit_position); 103 | cmdFins[F_PARAM + 4] = (uint8_t)((count >> 8) & 0xFF); 104 | cmdFins[F_PARAM + 5] = (uint8_t)(count & 0xFF); 105 | 106 | finsCommandLen = 18; 107 | 108 | return FrameSend(); 109 | } 110 | 111 | bool tcpFinsCommand::MemoryAreaWrite(MemoryArea area, uint16_t address, uint8_t bit_position, uint16_t count, uint8_t data[]) 112 | { 113 | // command & subcomand 114 | // 115 | cmdFins[MC] = 0x01; 116 | cmdFins[SC] = 0x02; 117 | 118 | // memory area 119 | // 120 | cmdFins[F_PARAM] = area; 121 | 122 | // address 123 | // 124 | cmdFins[F_PARAM + 1] = (uint8_t)((address >> 8) & 0xFF); 125 | cmdFins[F_PARAM + 2] = (uint8_t)(address & 0xFF); 126 | 127 | // bit position 128 | // 129 | cmdFins[F_PARAM + 3] = bit_position; 130 | 131 | // count items 132 | // 133 | cmdFins[F_PARAM + 4] = (uint8_t)((count >> 8) & 0xFF); 134 | cmdFins[F_PARAM + 5] = (uint8_t)(count & 0xFF); 135 | 136 | // set command lenght (12 + additional params) 137 | // 138 | finsCommandLen = 18; 139 | 140 | // send the message 141 | // 142 | vector w_data(data, data + count * 2); 143 | return FrameSend(w_data); 144 | } 145 | 146 | bool tcpFinsCommand::NodeAddressDataSend() 147 | { 148 | /* NODE ADDRESS DATA SEND buffer */ 149 | uint8_t cmdNADS[] = 150 | { 151 | 0x46, 0x49, 0x4E, 0x53, // 'F' 'I' 'N' 'S' 152 | 0x00, 0x00, 0x00, 0x0C, // 12 Bytes expected 153 | 0x00, 0x00, 0x00, 0x00, // NADS Command (0 Client to server, 1 server to client) 154 | 0x00, 0x00, 0x00, 0x00, // Error code (Not used) 155 | 0x00, 0x00, 0x00, 0x00 // Client node address, 0 = auto assigned 156 | }; 157 | 158 | 159 | // send NADS command 160 | // 161 | transport->Send(cmdNADS, sizeof(cmdNADS)); 162 | 163 | // wait for a plc response 164 | // 165 | uint8_t respNADS[24] = { 0 }; 166 | transport->Receive(respNADS, sizeof(respNADS)); 167 | 168 | 169 | // checks response error 170 | // 171 | if (respNADS[15] != 0) 172 | { 173 | lastError = "NASD command error: "; 174 | lastError += respNADS[15]; 175 | 176 | // no more actions 177 | // 178 | Close(); 179 | return false; 180 | } 181 | 182 | 183 | // checking header error 184 | // 185 | if (respNADS[8] != 0 || respNADS[9] != 0 || respNADS[10] != 0 || respNADS[11] != 1) 186 | { 187 | lastError = "Error sending NADS command. "; 188 | lastError += respNADS[8]; 189 | lastError += " "; 190 | lastError += respNADS[9]; 191 | lastError += " "; 192 | lastError += respNADS[10]; 193 | lastError += " "; 194 | lastError += respNADS[11]; 195 | 196 | // no more actions 197 | // 198 | Close(); 199 | 200 | return false; 201 | } 202 | 203 | 204 | // save the client & server node in the FINS command for all next conversations 205 | // 206 | cmdFins[DA1] = respNADS[23]; 207 | cmdFins[SA1] = respNADS[19]; 208 | 209 | return true; 210 | } 211 | 212 | bool tcpFinsCommand::FrameSend(const vector &data) 213 | { 214 | static pthread_mutex_t cs_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP; 215 | 216 | respFS.clear(); 217 | 218 | int fsLen = finsCommandLen + 8; 219 | fsLen += data.size(); 220 | 221 | cmdFS[6] = (uint8_t)((fsLen >> 8) & 0xFF); 222 | cmdFS[7] = (uint8_t)(fsLen & 0xFF); 223 | 224 | /* Enter the critical section -- other threads are locked out */ 225 | pthread_mutex_lock(&cs_mutex); 226 | 227 | // send frame header 228 | // 229 | transport->Send(&cmdFS[0], cmdFS.size()); 230 | 231 | // send FINS command 232 | // 233 | transport->Send(&cmdFins[0], finsCommandLen); 234 | 235 | // send additional data 236 | // 237 | if (data.size() > 0) 238 | { 239 | transport->Send(&data[0], data.size()); 240 | } 241 | 242 | // frame response 243 | // 244 | transport->Receive(&respFS[0], respFS.size()); 245 | 246 | // check frame error [8]+[9]+[10]+[11] 247 | // 248 | string FSR_ERR = ""; 249 | FSR_ERR += respFS[8]; 250 | FSR_ERR += respFS[9]; 251 | FSR_ERR += respFS[10]; 252 | FSR_ERR += respFS[11]; 253 | if (FSR_ERR.compare("0002") == 0) 254 | { 255 | lastError = "FRAME SEND error: " + FSR_ERR; 256 | return false; 257 | } 258 | 259 | // checks response error 260 | // 261 | if (respFS[15] != 0) 262 | { 263 | lastError = "Error receving FS command: " + respFS[15]; 264 | return false; 265 | } 266 | 267 | // calculate the expedted response lenght 268 | // 269 | // 16 bits word ([6] + [7]) 270 | // substract the additional 8 bytes 271 | // 272 | finsResponseLen = (respFS[6] << 8) + respFS[7]; 273 | finsResponseLen -= 8; 274 | 275 | 276 | // fins command response 277 | // 278 | transport->Receive(&respFins[0], 14); 279 | 280 | 281 | if (finsResponseLen > 14) 282 | { 283 | // fins command response data 284 | // 285 | transport->Receive(&respFinsData[0], finsResponseLen - 14); 286 | } 287 | 288 | /*Leave the critical section -- other threads can now pthread_mutex_lock() */ 289 | pthread_mutex_unlock(&cs_mutex); 290 | 291 | if (respFins[12] != 0 || respFins[13] != 0) 292 | { 293 | lastError += "Response Code error: (Code: "; 294 | lastError += respFins[12]; 295 | lastError += "Subcode: "; 296 | lastError += respFins[13]; 297 | lastError += ")"; 298 | 299 | return false; 300 | } 301 | 302 | return true; 303 | } 304 | } 305 | -------------------------------------------------------------------------------- /src/tcpFinsCommand.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | #include "IFinsCommand.h" 7 | #include "tcpTransport.h" 8 | 9 | using namespace std; 10 | 11 | namespace OmronPlc 12 | { 13 | class tcpFinsCommand : public IFinsCommand 14 | { 15 | private: 16 | int finsCommandLen = 0; 17 | int finsResponseLen = 0; 18 | vector cmdFS; 19 | vector respFS; 20 | vector respFinsData; 21 | vector cmdFins; 22 | vector respFins; 23 | tcpTransport * transport; 24 | string lastError; 25 | 26 | public: 27 | tcpFinsCommand(uint8_t ServiceID = 0x01); 28 | ~tcpFinsCommand(); 29 | virtual bool Connect(); 30 | virtual void Close(); 31 | virtual void SetRemote(string ipaddr, uint16_t port); 32 | virtual bool MemoryAreaRead(MemoryArea area, uint16_t address, uint8_t bit_position, uint16_t count); 33 | virtual bool MemoryAreaWrite(MemoryArea area, uint16_t address, uint8_t bit_position, uint16_t count, uint8_t data[]); 34 | 35 | private: 36 | bool NodeAddressDataSend(); 37 | bool FrameSend(const vector &data = vector()); 38 | }; 39 | } 40 | -------------------------------------------------------------------------------- /src/tcpTransport.cpp: -------------------------------------------------------------------------------- 1 | #include "tcpTransport.h" 2 | #include 3 | 4 | namespace OmronPlc 5 | { 6 | tcpTransport::tcpTransport() 7 | { 8 | } 9 | 10 | tcpTransport::~tcpTransport() 11 | { 12 | } 13 | 14 | void tcpTransport::SetRemote(string ip, uint16_t port) 15 | { 16 | _ip = ip; 17 | _port = port; 18 | } 19 | 20 | bool tcpTransport::Connect() 21 | { 22 | _socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 23 | if (_socket < 0) 24 | { 25 | return true; 26 | } 27 | 28 | _serveraddr.sin_family = AF_INET; 29 | _serveraddr.sin_addr.s_addr = inet_addr(_ip.c_str()); 30 | _serveraddr.sin_port = htons(_port); 31 | 32 | if (connect(_socket, (struct sockaddr *)&_serveraddr, sizeof(_serveraddr)) < 0) 33 | { 34 | return true; 35 | } 36 | 37 | Connected = true; 38 | 39 | return false; 40 | } 41 | 42 | void tcpTransport::Close() 43 | { 44 | if (Connected) 45 | { 46 | close(_socket); 47 | _socket = 0; 48 | Connected = false; 49 | } 50 | } 51 | 52 | int tcpTransport::Send(const uint8_t command[], int cmdLen) 53 | { 54 | if (!Connected) 55 | { 56 | throw "Sockect is not connected."; 57 | } 58 | 59 | // int byteSent = send(_socket, command, cmdLen, MSG_DONTWAIT); 60 | int bytesSent = send(_socket, command, cmdLen, 0); 61 | 62 | if (bytesSent != cmdLen) 63 | { 64 | string msg = "Sending error. (Expected bytes:"; 65 | msg += to_string(cmdLen); 66 | msg += " Sent: "; 67 | msg += to_string(bytesSent); 68 | msg += ")"; 69 | 70 | throw msg.c_str(); 71 | } 72 | return bytesSent; 73 | } 74 | 75 | int tcpTransport::Receive(uint8_t response[], int respLen) 76 | { 77 | if (!Connected) 78 | { 79 | throw "Socket is not connected."; 80 | } 81 | 82 | // receives the response, this is a synchronous method and can hang the process 83 | // 84 | int bytesRecv = recv(_socket, response, respLen, 0); 85 | 86 | // check the number of bytes received 87 | // 88 | if (bytesRecv != respLen) 89 | { 90 | string msg = "Receiving error. (Expected:"; 91 | msg += to_string(respLen); 92 | msg += " Received: "; 93 | msg += to_string(bytesRecv); 94 | msg += ")"; 95 | 96 | throw msg.c_str(); 97 | } 98 | 99 | return bytesRecv; 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /src/tcpTransport.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include "ITransport.h" 9 | 10 | using namespace std; 11 | 12 | namespace OmronPlc 13 | { 14 | class tcpTransport : ITransport 15 | { 16 | private: 17 | struct sockaddr_in _serveraddr; 18 | int _socket; 19 | uint16_t _port; 20 | string _ip; 21 | 22 | public: 23 | tcpTransport(); 24 | ~tcpTransport(); 25 | virtual void SetRemote(string ip, uint16_t port); 26 | virtual bool Connect(); 27 | virtual void Close(); 28 | virtual int Send(const uint8_t command[], int cmdLen); 29 | virtual int Receive(uint8_t response[], int respLen); 30 | }; 31 | } 32 | -------------------------------------------------------------------------------- /src/udpFinsCommand.cpp: -------------------------------------------------------------------------------- 1 | #include "udpFinsCommand.h" 2 | 3 | using namespace OmronPlc; 4 | 5 | udpFinsCommand::udpFinsCommand(uint8_t ServiceID) 6 | { 7 | transport = new udpTransport(); 8 | 9 | cmdFins[SID] = ServiceID; 10 | Response = &respFinsData[0]; 11 | } 12 | 13 | udpFinsCommand::~udpFinsCommand() 14 | { 15 | delete transport; 16 | } 17 | 18 | bool udpFinsCommand::Connect() 19 | { 20 | try 21 | { 22 | return transport->Connect(); 23 | } 24 | catch (const char * msg) 25 | { 26 | return false; 27 | } 28 | return false; 29 | } 30 | 31 | void udpFinsCommand::Close() 32 | { 33 | transport->Close(); 34 | } 35 | 36 | void udpFinsCommand::SetRemote(string ipaddr, uint16_t port) 37 | { 38 | transport->SetRemote(ipaddr, port); 39 | 40 | in_addr_t addr = inet_addr(ipaddr.c_str()); 41 | cmdFins[DA1] = (uint8_t)ntohl(addr); 42 | cmdFins[SA1] = 0x2; // client ip address last number ex) 192.168.1.2 43 | cmdFins[SA2] = 1; 44 | } 45 | 46 | 47 | bool udpFinsCommand::MemoryAreaRead(MemoryArea area, uint16_t address, uint8_t bit_position, uint16_t count) 48 | { 49 | try 50 | { 51 | // command & subcomand 52 | // 53 | cmdFins[MC] = 0x01; 54 | cmdFins[SC] = 0x01; 55 | 56 | // memory area 57 | // 58 | cmdFins[F_PARAM] = (uint8_t)area; 59 | 60 | // address 61 | // 62 | cmdFins[F_PARAM + 1] = (uint8_t)((address >> 8) & 0xFF); 63 | cmdFins[F_PARAM + 2] = (uint8_t)(address & 0xFF); 64 | 65 | // no bit position 66 | // 67 | cmdFins[F_PARAM + 3] = bit_position; 68 | 69 | // count items 70 | // 71 | cmdFins[F_PARAM + 4] = (uint8_t)((count >> 8) & 0xFF); 72 | cmdFins[F_PARAM + 5] = (uint8_t)(count & 0xFF); 73 | 74 | // set command lenght (12 + additional params) 75 | // 76 | finsCommandLen = 18; 77 | 78 | // send the message 79 | // 80 | return FrameSend(); 81 | } 82 | catch (const char * msg) 83 | { 84 | lastError = msg; 85 | return false; 86 | } 87 | } 88 | 89 | bool udpFinsCommand::MemoryAreaWrite(MemoryArea area, uint16_t address, uint8_t bit_position, uint16_t count, uint8_t data[]) 90 | { 91 | try 92 | { 93 | // command & subcomand 94 | // 95 | cmdFins[MC] = 0x01; 96 | cmdFins[SC] = 0x02; 97 | 98 | // memory area 99 | // 100 | cmdFins[F_PARAM] = (uint8_t)area; 101 | 102 | // address 103 | // 104 | cmdFins[F_PARAM + 1] = (uint8_t)((address >> 8) & 0xFF); 105 | cmdFins[F_PARAM + 2] = (uint8_t)(address & 0xFF); 106 | 107 | // bit position 108 | // 109 | cmdFins[F_PARAM + 3] = bit_position; 110 | 111 | // count items 112 | // 113 | cmdFins[F_PARAM + 4] = (uint8_t)((count >> 8) & 0xFF); 114 | cmdFins[F_PARAM + 5] = (uint8_t)(count & 0xFF); 115 | 116 | // set command lenght (12 + additional params) 117 | // 118 | finsCommandLen = 18; 119 | 120 | // send the message 121 | // 122 | vector w_data(data, data + count * 2); 123 | return FrameSend(w_data); 124 | } 125 | catch (const char * msg) 126 | { 127 | lastError = msg; 128 | return false; 129 | } 130 | } 131 | 132 | bool udpFinsCommand::FrameSend(const vector &data) 133 | { 134 | int fsLen = finsCommandLen; 135 | fsLen += data.size(); 136 | 137 | // send additional data 138 | // 139 | if (data.size() > 0) 140 | { 141 | cmdFins.insert(cmdFins.end(), data.begin(), data.end()); 142 | finsResponseLen = 14; 143 | } 144 | else 145 | { 146 | int FS_LEN = ((int)cmdFins[16] << 8) + cmdFins[17]; 147 | finsResponseLen = (14 + FS_LEN * 2); 148 | } 149 | // send FINS command 150 | // 151 | transport->Send(&cmdFins[0], fsLen); 152 | 153 | transport->Receive(&respFins[0], finsResponseLen); 154 | 155 | // check response code 156 | // 157 | if (respFins[12] != 0 || respFins[13] != 0) 158 | { 159 | lastError += "Response Code error: (Code: "; 160 | lastError += to_string(respFins[12]); 161 | lastError += "Subcode: " + to_string(respFins[13]) + ")"; 162 | return false; 163 | } 164 | 165 | for (int idx = 0; idx < finsResponseLen - 14; ++idx) 166 | { 167 | respFinsData[2 * idx] = respFins[14 + 2 * idx]; 168 | respFinsData[2 * idx + 1] = respFins[15 + 2 * idx]; 169 | } 170 | 171 | return true; 172 | } 173 | -------------------------------------------------------------------------------- /src/udpFinsCommand.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | #include "IFinsCommand.h" 7 | #include "udpTransport.h" 8 | 9 | using namespace std; 10 | 11 | namespace OmronPlc 12 | { 13 | class udpFinsCommand : public IFinsCommand 14 | { 15 | private: 16 | int finsCommandLen = 0; 17 | int finsResponseLen = 0; 18 | vector respFins; 19 | vector respFinsData; 20 | vector cmdFins; 21 | udpTransport * transport; 22 | string lastError; 23 | 24 | public: 25 | udpFinsCommand(uint8_t ServiceID = 0x02); 26 | ~udpFinsCommand(); 27 | virtual bool Connect(); 28 | virtual void Close(); 29 | virtual void SetRemote(string ipaddr, uint16_t port); 30 | virtual bool MemoryAreaRead(MemoryArea area, uint16_t address, uint8_t bit_position, uint16_t count); 31 | virtual bool MemoryAreaWrite(MemoryArea area, uint16_t address, uint8_t bit_position, uint16_t count, uint8_t data[]); 32 | 33 | private: 34 | bool FrameSend(const vector &data = vector()); 35 | }; 36 | } 37 | -------------------------------------------------------------------------------- /src/udpTransport.cpp: -------------------------------------------------------------------------------- 1 | #include "udpTransport.h" 2 | #include 3 | 4 | OmronPlc::udpTransport::udpTransport() 5 | { 6 | } 7 | 8 | OmronPlc::udpTransport::~udpTransport() 9 | { 10 | } 11 | 12 | void OmronPlc::udpTransport::SetRemote(string ip, uint16_t port) 13 | { 14 | _ip = ip; 15 | _port = port; 16 | } 17 | 18 | bool OmronPlc::udpTransport::Connect() 19 | { 20 | _socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); 21 | if (_socket < 0) 22 | { 23 | return true; 24 | } 25 | //setsockopt(_socket, SOL_SOCKET, SO_RCVTIMEO, &ReceiveTimeout, sizeof(ReceiveTimeout)); 26 | 27 | _serveraddr.sin_family = AF_INET; 28 | _serveraddr.sin_addr.s_addr = inet_addr(_ip.c_str()); 29 | _serveraddr.sin_port = htons(_port); 30 | 31 | if (connect(_socket, (struct sockaddr *)&_serveraddr, sizeof(_serveraddr)) < 0) 32 | { 33 | return true; 34 | } 35 | 36 | Connected = true; 37 | 38 | return false; 39 | } 40 | 41 | void OmronPlc::udpTransport::Close() 42 | { 43 | if (Connected) 44 | { 45 | close(_socket); 46 | _socket = 0; 47 | Connected = false; 48 | } 49 | } 50 | 51 | int OmronPlc::udpTransport::Send(const uint8_t command[], int cmdLen) 52 | { 53 | if (!Connected) 54 | { 55 | throw "Socket is not connected."; 56 | } 57 | 58 | // sends the command 59 | // 60 | int bytesSent = send(_socket, command, cmdLen, 0); 61 | 62 | // it checks the number of bytes sent 63 | // 64 | if (bytesSent != cmdLen) 65 | { 66 | string msg = "Sending error. (Expected bytes:"; 67 | msg += to_string(cmdLen); 68 | msg += " Sent: "; 69 | msg += to_string(bytesSent); 70 | msg += ")"; 71 | 72 | throw msg.c_str(); 73 | } 74 | 75 | return bytesSent; 76 | } 77 | 78 | int OmronPlc::udpTransport::Receive(uint8_t response[], int respLen) 79 | { 80 | if (!Connected) 81 | { 82 | throw "Socket is not connected."; 83 | } 84 | 85 | // receives the response, this is a synchronous method and can hang the process 86 | // 87 | int bytesRecv = recv(_socket, response, respLen, 0); 88 | 89 | // check the number of bytes received 90 | // 91 | if (bytesRecv != respLen) 92 | { 93 | string msg = "Receiving error. (Expected:"; 94 | msg += to_string(respLen); 95 | msg += " Received: "; 96 | msg += to_string(bytesRecv); 97 | msg += ")"; 98 | 99 | throw msg.c_str(); 100 | } 101 | 102 | return bytesRecv; 103 | } 104 | -------------------------------------------------------------------------------- /src/udpTransport.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include "ITransport.h" 9 | 10 | namespace OmronPlc 11 | { 12 | class udpTransport : ITransport 13 | { 14 | private: 15 | struct sockaddr_in _serveraddr; 16 | int _socket; 17 | uint16_t _port; 18 | string _ip; 19 | 20 | public: 21 | udpTransport(); 22 | ~udpTransport(); 23 | virtual void SetRemote(string ip, uint16_t port); 24 | virtual bool Connect(); 25 | virtual void Close(); 26 | virtual int Send(const uint8_t command[], int cmdLen); 27 | virtual int Receive(uint8_t response[], int respLen); 28 | }; 29 | } 30 | --------------------------------------------------------------------------------