rllib  1
rldf1.h
Go to the documentation of this file.
1 /***************************************************************************
2  rldf1.cpp - description
3  -------------------
4 
5 This Class implements basic functions of DF1 Full Duplex protocall as
6 described in 1770-6.5.16 Publication of AB.
7 
8 This Class implements:
9  - Data Link Layer ( Message Frames )
10  - Application Layer ( Message Packets )
11  - Some Basic Communication Commands for reading and writting PLC files.
12  It is easy to add more Communication Commands.
13 
14 Some basic knowledge about the related PLCs is required.
15 The class supports only serial communication through RS232 port.
16 
17 DANGER: DON'T connect to a PLC unless you are certain it is safe to do so!!!
18 You are ABSOLUTELY RESPONSIBLE if you affect the operation of a running PLC.
19 
20 Evangelos Arkalis
21 mail:arkalis.e@gmail.com
22 ***************************************************************************/
23 
24 /***************************************************************************
25  * *
26  * This library is free software; you can redistribute it and/or modify *
27  * it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as *
28  * published by the Free Software Foundation *
29  * *
30  ***************************************************************************/
31 
32 #ifndef _RL_DF1_H_
33 #define _RL_DF1_H_
34 
35 #include "rlserial.h"
36 class df1Buffer;
37 
38 class rlDF1
39 {
40 public:
41 
47  rlDF1(unsigned char src=0, int timeout=1000);
48  virtual ~rlDF1();
49  void registerSerial(rlSerial *serial);
50 
51  enum DF1ret {
56  retERROR = -2,
59  };
60 
61 
67  enum CPUMODE {
68  PROGRAM = 0,
70  RUN,
72  };
73  int cmdSetCPUMode(unsigned char destination, unsigned char mode);
84  int cmdDiagnosticStatus( unsigned char destination, unsigned char *data);
110  int cmdLogicalRead( unsigned char destination, unsigned char nsize, unsigned char filetype, unsigned char filenum, unsigned char adr, unsigned char sadr, unsigned char *buffer);
136  int cmdLogicalWrite( unsigned char destination, unsigned char nsize, unsigned char filetype, unsigned char filenum, unsigned char adr, unsigned char sadr, unsigned char *buffer);
137 private:
152  int sendCommand( unsigned char destination, unsigned char cmd, unsigned char sts, unsigned char *cdata, unsigned char len);
153  int receiveAnswer( unsigned char &destination, unsigned char &cmd, unsigned char &sts, unsigned char *cdata, unsigned char &len);
158  enum DF1BytePos {
166  };
168  SOH = 0x01,
169  STX = 0x02,
170  ETX = 0x03,
171  EOT = 0x04,
172  ENQ = 0x05,
173  ACK = 0x06,
174  DLE = 0x10,
175  NAC = 0x15
176  };
177  enum DF1Flags {
181  };
182  int writeBuffer( unsigned char *buffer, int len );
183  int writeBuffer( df1Buffer& buffer );
184  int getSymbol(unsigned char *c);
185  int get(unsigned char *c);
186  int sendENQ();
187  int sendACK();
188  int sendNAC();
189 
190 
192  bool active;
193  unsigned short tns;
194 
197  unsigned char source;
198  int timeout;
199 
200 public:
204  int requests() {return nRequests;};
205  int responses() {return nResponses;};
206  void clearStats() { nRequests=0; nResponses=0;};
207 };
208 
209 
210 #endif // _RL_DF1_H_
211 
212 /*
213 // test program for rlDF1
214 #include <rldf1.h>
215 
216 // Remote ID
217 const unsigned char id=1;
218 
219 rlSerial ser;
220 rlDF1 myplc;
221 
222 
223 int main(int argc, char *argv[])
224 {
225  int ret;
226  if ( ser.openDevice( "/dev/ttyS0", B19200, 0, 0 )!= 0 ) exit(-1);
227  printf("\nDevice OK");
228  myplc.registerSerial( &ser ); /// Register Serial Port
229 
230 
231  printf("\n1.TEST CONNECTIVITY");
232  ret = myplc.cmdSetCPUMode( id, rlDF1::NOCHANGE ); /// For Connectivity Test
233  if (ret<0) {
234  printf("\nDevice Error %d\n",ret);
235  exit(-1);
236  }
237  rlsleep(3000);
238 
239 
240  printf("\n2.SET PLC IN PROGRAM MODE");
241  ret = myplc.cmdSetCPUMode( id, rlDF1::PROGRAM ); /// PROGRAM MODE
242  if (ret<0) {
243  printf("\nDevice Error %d\n",ret);
244  exit(-1);
245  }
246  rlsleep(3000);
247 
248 
249  printf("\n3.SET PLC IN RUN MODE");
250  ret = myplc.cmdSetCPUMode( id, rlDF1::RUN ); /// RUN MODE
251  if (ret<0) {
252  printf("\nDevice Error %d\n",ret);
253  exit(-1);
254  }
255  rlsleep(3000);
256 
257 
258  printf("\n4.GET PLC STATUS BYTES (Read Manual for details)");
259  unsigned char rbytes[256];
260  ret = myplc.cmdDiagnosticStatus( id, rbytes );
261  if (ret<0) {
262  printf("\nDevice Error %d\n",ret);
263  exit(-1);
264  }
265  for(int i=0;i<ret;i++) printf("\n%d -> \t%d \t%02X", i, rbytes[i], rbytes[i]);
266  printf("\n");
267  rlsleep(3000);
268 
269 
270  printf("\n5.LOGICAL READ - Read 2 bytes from Inputs - I1:0");
271  ret = myplc.cmdLogicalRead( id, 2, 0x8C, 1, 0, 0, rbytes ); /// 0x8C:Read Inputs by Slot: 2 bytes from Data File I1, starting at Address 0 [ I1:0 ]
272  if (ret<0) {
273  printf("\nDevice Error %d\n",ret);
274  exit(-1);
275  }
276  for(int i=0;i<ret;i++) printf("\n%d -> \t%d \t%02X", i, rbytes[i], rbytes[i]);
277  printf("\n");
278  rlsleep(3000);
279 
280 
281  printf("\n6.LOGICAL WRITE - Write 2 bytes to Outputs - Q0:0 > CYCLE FROM 0.0 to 0.7, RESET 0.8-0.15\n");
282  for(int i=0;i<8;i++) {
283  printf("SET Q0:0.%d",i);
284  rbytes[0]=0x01<<i;
285  rbytes[1]=0;
286  ret = myplc.cmdLogicalWrite( id, 2, 0x8B, 0, 0, 0, rbytes ); /// 0x8B:Write Outputs by Slot: 2 bytes to Data File Q0, starting at Address 0 [ Q0:0 ]
287  if (ret<0) {
288  printf("\nDevice Error %d\n",ret);
289  exit(-1);
290  }
291  printf("\n");
292  rlsleep(1000);
293  }
294  printf("\n7.LOGICAL WRITE - Write 2 bytes to Outputs - Q0:0 > RESET 0.0-0.15");
295  rbytes[0]=0; /// Reset Outputs 0-15
296  rbytes[1]=0;
297  ret = myplc.cmdLogicalWrite( id, 2, 0x8B, 0, 0, 0, rbytes ); /// 0x8B:Write Outputs by Slot: 2 bytes to Data File Q0, starting at Address 0 [ Q0:0 ]
298  if (ret<0) {
299  printf("\nDevice Error %d\n",ret);
300  exit(-1);
301  }
302  printf("\nDuring execution of this program, we had %d Requests and %d Responses to/from PLC\n", myplc.requests(), myplc.responses());
303 
304 
305  ser.closeDevice();
306 }
307 */
int requests()
Definition: rldf1.h:204
int nRequests
Definition: rldf1.h:195
Definition: rldf1.h:38
int timeout
Definition: rldf1.h:198
int sendENQ()
Definition: rldf1.cpp:211
int writeBuffer(unsigned char *buffer, int len)
Definition: rldf1.cpp:165
int responses()
Definition: rldf1.h:205
unsigned short tns
Definition: rldf1.h:193
void registerSerial(rlSerial *serial)
Definition: rldf1.cpp:160
virtual ~rlDF1()
Definition: rldf1.cpp:156
rlDF1(unsigned char src=0, int timeout=1000)
Definition: rldf1.cpp:145
int sendACK()
Definition: rldf1.cpp:220
int nResponses
Definition: rldf1.h:196
bool active
Definition: rldf1.h:192
int sendCommand(unsigned char destination, unsigned char cmd, unsigned char sts, unsigned char *cdata, unsigned char len)
Definition: rldf1.cpp:319
unsigned char source
Definition: rldf1.h:197
int cmdLogicalWrite(unsigned char destination, unsigned char nsize, unsigned char filetype, unsigned char filenum, unsigned char adr, unsigned char sadr, unsigned char *buffer)
Definition: rldf1.cpp:294
DF1ret
Definition: rldf1.h:51
int cmdSetCPUMode(unsigned char destination, unsigned char mode)
Definition: rldf1.cpp:240
rlSerial * tty
Definition: rldf1.h:191
int getSymbol(unsigned char *c)
Definition: rldf1.cpp:184
int sendNAC()
Definition: rldf1.cpp:229
CPUMODE
Definition: rldf1.h:67
int cmdDiagnosticStatus(unsigned char destination, unsigned char *data)
Definition: rldf1.cpp:257
DF1Flags
Definition: rldf1.h:177
int cmdLogicalRead(unsigned char destination, unsigned char nsize, unsigned char filetype, unsigned char filenum, unsigned char adr, unsigned char sadr, unsigned char *buffer)
Definition: rldf1.cpp:273
DF1BytePos
Definition: rldf1.h:158
Page 2-6 of Ref Manual.
Definition: rldf1.h:168
void clearStats()
Definition: rldf1.h:206
DF1ControlChars
Definition: rldf1.h:167
int receiveAnswer(unsigned char &destination, unsigned char &cmd, unsigned char &sts, unsigned char *cdata, unsigned char &len)
Definition: rldf1.cpp:386