-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathLibFINS.h
204 lines (175 loc) · 8.17 KB
/
LibFINS.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#ifndef _FINS_TCP_H_
#define _FINS_TCP_H_
#include <Arduino.h>
// #include <SPI.h>
// #include <EthernetENC.h>
#ifdef ESP32
#include <WiFi.h>
#else
#include <ESP8266WiFi.h>
#endif
#include "FINS_constants.h"
//#include <vector>
//#include <Vector.h>
using namespace std;
const char dia_de_la_semana[7][10] = {"Domingo", "Lunes", "Martes", "Miercoles", "Jueves", "Viernes", "Sabado"};
class FINS_TCP
{
private:
// struct sockaddr_in _serveraddr;
uint16_t _port;
const char *_ip;
WiFiClient cliente;
int finsCommandLen = 0;
int finsResponseLen = 0;
bool conectado = false;
uint8_t cmdFS[16];
uint8_t respFS[16];
uint8_t respFinsData[2048];
uint8_t cmdFins[22];
uint8_t respFins[2048];
uint8_t *Response;
String lastError;
uint8_t DA1__, SA1__;
FinsErrors FrameSend(const uint8_t data[] = new uint8_t[0], int words = 0);
void tcpFinsCommand(uint8_t ServiceID = 0x01);
bool NodeAddressDataSend();
bool Connect();
void Close();
int Send(const uint8_t command[], int cmdLen);
int Receive(uint8_t response[], int respLen);
void commandTypePrint(CommandType comand_type);
public:
FINS_TCP();
// FINS_TCP(WiFiClient &cliente_);
// ~tcpTransport();
bool init(const char *omron_plc_ip, uint16_t fins_port = 9600);
/* Read 1 word from an specific memory area and address and return its value in one 16 bits unsigned word */
uint16_t readWord(MemoryArea area, uint16_t address);
/* Read Any Memory Area(words) and Write to ans_items 16 bits array and returns the error code of the operation
Params...
Memory Area = memory area to read (DM,H,CIO..)
Address= Memory area to start read words
No_words = number of words to read
Ans_items = uint16_t array to save the data readed
*/
FinsErrors MemoryAreaRead(MemoryArea area, uint16_t address, uint16_t no_words, uint16_t ans_items[]);
/* Write to Any Memory Area from uint16_t WORD and returns the error code of the operation
Params...
Memory Area = memory area to read (DM,H,CIO..)
Address= Memory area to start writing words
No_words = number of words to write
Data = unsigned 16 bits word
*/
FinsErrors MemoryAreaWrite(MemoryArea area, uint16_t start_address, uint16_t data);
/* Write to Any Memory Area from uint8_t array and returns the error code of the operation
Params...
Memory Area = memory area to read (DM,H,CIO..)
Address= Memory area to start writing words
No_words = number of words to write
Data[] = array where is the uint8_t information(2 are requeired from 1 word(16 bits))
*/
FinsErrors MemoryAreaWrite(MemoryArea area, uint16_t start_address, uint16_t number_of_words, uint8_t data[]);
/* Write to Any Memory Area from uint16_t array and returns the error code of the operation
Params...
Memory Area = memory area to read (DM,H,CIO..)
Address= Memory area to start writing words
No_words = number of words to write
Data[] = array where is the uint8_t information
*/
FinsErrors MemoryAreaWrite(MemoryArea area, uint16_t start_address, uint16_t number_of_words, uint16_t data[]);
/* Write to Any Memory Area from signed int value and returns the error code of the operation
>>>>>> (this operation uses just 1 word to store the int value) <<<<<<
Params...
Memory Area = memory area to read (DM,H,CIO..)
Address= Memory area to start writing words
No_words = number of words to write
Int = int signed value of 16 bits
*/
FinsErrors MemoryAreaWrite(MemoryArea area, uint16_t start_address, int int_value);
/* Write to Any Memory Area from signed float value and returns the error code of the operation
>>>>>> (this operation uses 2 consecutive words to store the float value) <<<<<<
Params...
Memory Area = memory area to read (DM,H,CIO..)
Address= Memory area to start writing words
No_words = number of words to write
float_value = float value of 32 bits
*/
FinsErrors MemoryAreaWrite(MemoryArea area, uint16_t start_address, float float_value);
/* Write to Any Memory Area from signed double value and returns the error code of the operation
>>>>>>> (this operation uses 4 consecutive words to store the double value) <<<<<<
Memory Area = memory area to read (DM,H,CIO..)
Address= Memory area to start writing words
No_words = number of words to write
float_value = float value of 64 bits
*/
FinsErrors MemoryAreaWrite(MemoryArea area, uint16_t start_address, double double_value);
/* Write to Any Memory Area from signed double value and returns the error code of the operation
>>>>>>> (this operation uses 4 consecutive words to store the double value) <<<<<<
Memory Area = memory area to read (DM,H,CIO..)
Address= Memory area to start writing words
No_words = number of words to write
float_value = float value of 64 bits
*/
FinsErrors MemoryAreaWrite(MemoryArea area, uint16_t start_address, String text_message);
/* Prints the time in the PLC */
FinsErrors ClockRead();
/* Read the specific value of one bit in one word and returns it value 0 or 1
Params...
Memory Area = memory area where is the bit to read(DM,H,CIO..)
Address= Memory address where is the bit
Bit_position = number of bit of the word(0-15)
*/
uint8_t readBit(MemoryArea area, uint16_t address, uint8_t bit_position);
/* Write the specific value to one bit in one word and returns the error code of the operation
Params...
Memory Area = memory area where is the bit to write(DM,H,CIO..)
Address= Memory address where is the bit to write
Bit_position = number of bit of the word(0-15)
Value = 0 or 1 to write
>>Note. if the bit is used for a program, it will maybe not change, to ensure the change use forcedBit()<<
*/
FinsErrors writeBit(MemoryArea area, uint16_t address, uint8_t bit_position, uint8_t value);
/* Try to Toggle the value of one bit in one word and returns the error code of the operation
Params...
Memory Area = memory area where is the bit to write(DM,H,CIO..)
Address= Memory address where is the bit to toggle
BIt_position = number of bit of the word(0-15)
Value = 0 or 1 to write
>>Note. if the bit is used for a program, it will maybe not change, to ensure the change use toggleBitForced()<<
*/
FinsErrors toggleBit(MemoryArea area, uint16_t address, uint8_t bit_position);
/* Force to Toggle the value of one bit in one word and returns the error code of the operation
Params...
Memory Area = memory area where is the bit to write(DM,H,CIO..)
Address= Memory address where is the bit to toggle
BIt_position = number of bit of the word(0-15)
Value = 0 or 1 to write
Note. if the bit is used for a program, it will maybe not change, to ensure the change use toggleBitForced
*/
FinsErrors toggleBitForced(MemoryArea area, uint16_t address, uint8_t bit_position);
/* Forced set or reset one bit from 1 memory address
Params...
Memory Area = memory area where is the bit to write(DM,H,CIO..)
Address= Memory address where is the bit to toggle
Bit_position = number of bit of the word(0-15)
Force_mode = Forced_Reset to put the bit in 0 logic, Forced_Set to put the bit un 1 logic
*/
FinsErrors forcedBit(MemoryArea area, uint16_t address, uint8_t bit_position, Force_Mode set_reset);
/*
Cancel All forced Signals in PLC
Note. Sometimes the canceled signals doest returns to it original state
*/
FinsErrors cancelAllForced();
/*
Change the PLC status to another state
PLC_MODE = MONITOR; RUN; STOP or PROGRAM
*/
FinsErrors changePLCMode(PLC_MODE modo);
/*Read the PLC status and returns the PLC mode PLC_MODE = MONITOR; RUN; STOP or PROGRAM
*/
PLC_MODE PLCStatusRead();
bool StopCPU(uint16_t ProgNum1, uint16_t ProgNum2);
bool ClockWrite(uint16_t Year, uint16_t Month, uint16_t Date, uint16_t Hour, uint16_t Minute, uint16_t Second, uint16_t Day);
};
#endif