44_S7_S5Client.pm 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
  1. # $Id: 44_S7_S5Client.pm 12776 2016-12-14 18:09:08Z charlie71born $
  2. ##############################################
  3. use strict;
  4. use warnings;
  5. require Exporter;
  6. use Config;
  7. use AutoLoader;
  8. require "44_S7_Client.pm";
  9. #if ( OS_Linux() ) {
  10. use Device::SerialPort;
  11. #}
  12. #else {
  13. # use Win32::SerialPort;
  14. #}
  15. package S5Client;
  16. #use S7ClientBase;
  17. our @ISA = qw(S7ClientBase); # inherits from Person
  18. #---------------------- constants for communication
  19. use constant DLE => 0x10;
  20. use constant ETX => 0x03;
  21. use constant STX => 0x02;
  22. use constant SYN => 0x16;
  23. use constant NAK => 0x15;
  24. use constant EOT => 0x04; # for S5
  25. use constant ACK => 0x06; # for S5
  26. use constant daveS5BlockType_DB => 0x01;
  27. use constant maxSysinfoLen => 87;
  28. use constant daveMaxRawLen => 2048;
  29. use constant MaxPduSize =>
  30. 240;
  31. sub new {
  32. my $class = shift;
  33. my $self = $class->SUPER::new();
  34. $self->{S5PAEAddress} = 0;
  35. $self->{S5PAAAddress} = 0;
  36. $self->{S5flagsAddress} = 0;
  37. $self->{S5timerAddress} = 0;
  38. $self->{S5counterAddress} = 0;
  39. $self->{__davet1006} = [ &DLE, &ACK ];
  40. $self->{__daveT161003} = [ 0x16, &DLE, &ETX ];
  41. $self->{__davet121003} = [ 0x12, &DLE, &ETX ];
  42. $self->{PDULength} = &MaxPduSize;
  43. #my @__davet1006 = ( &DLE, &ACK );
  44. #my @__daveT161003 = ( 0x16, &DLE, &ETX );
  45. #my @{$self->{__davet121003}} = ( 0x12, &DLE, &ETX );
  46. return bless $self, $class;
  47. }
  48. # ----------- compare arrays
  49. sub compare {
  50. my ( $self, $a_ref, $b_ref ) = @_;
  51. my @a = @{$a_ref}; # dereferencing and copying each array
  52. my @b = @{$b_ref};
  53. if ( @a != @b ) {
  54. return 0;
  55. }
  56. else {
  57. foreach ( my $i = 0 ; $i < @a ; $i++ ) {
  58. # Ideally, check for undef/value comparison here as well
  59. if ( $a[$i] != $b[$i] )
  60. { # use "ne" if elements are strings, not numbers
  61. # Or you can use generic sub comparing 2 values
  62. return 0;
  63. }
  64. }
  65. return 1;
  66. }
  67. }
  68. #
  69. # ----------- This writes a single chracter to the serial interface
  70. #
  71. sub S5SendSingle($$) {
  72. my ( $self, $c ) = @_;
  73. my $buffer = pack( 'C*', $c );
  74. my $tbuffer = join( ", ", unpack( "H2 " x length($buffer), $buffer ) );
  75. main::Log3( undef, 5, "S5Client S5SendSingle <-- " . $tbuffer );
  76. $self->{serial}->write($buffer);
  77. }
  78. #---------------------------------------------------reqest transaction with PLC
  79. sub S5ReqTrans($$) {
  80. my ( $self, $trN ) = @_;
  81. my $buffer;
  82. my $count;
  83. my $tbuffer;
  84. $self->S5SendSingle(&STX); #start trasmission
  85. #expected S5 awnswer DLE,ACK
  86. ( $count, $buffer ) = $self->{serial}->read(2);
  87. my @cbuffer = unpack( "C" x $count, $buffer );
  88. if ( $main::attr{global}{verbose} >= 5 ) {
  89. $tbuffer = join( ", ", unpack( "H2 " x $count, $buffer ) );
  90. main::Log3( undef, 5, "S5Client S5ReqTrans $tbuffer -->" );
  91. }
  92. if ( $self->compare( \@cbuffer, \@{ $self->{__davet1006} } ) == 0 ) {
  93. main::Log3( undef, 3, "S5Client S5ReqTrans: no DLE,ACK before send" );
  94. return -1;
  95. }
  96. $self->S5SendSingle($trN);
  97. ( $count, $buffer ) = $self->{serial}->read(1);
  98. if ( $main::attr{global}{verbose} >= 5 ) {
  99. $tbuffer = join( ", ", unpack( "H2 " x $count, $buffer ) );
  100. main::Log3( undef, 5, "S5Client S5ReqTrans $tbuffer -->" );
  101. }
  102. if ( $count != 1 ) {
  103. #error awnser is too short
  104. return -1;
  105. }
  106. @cbuffer = unpack( "C" x $count, $buffer );
  107. if ( $cbuffer[0] ne &STX ) {
  108. main::Log3( undef, 3, "S5Client S5ReqTrans: no STX before send" );
  109. return -2;
  110. }
  111. $self->S5SendDLEACK();
  112. ( $count, $buffer ) = $self->{serial}->read(3);
  113. if ( $main::attr{global}{verbose} >= 5 ) {
  114. $tbuffer = join( ", ", unpack( "H2 " x $count, $buffer ) );
  115. main::Log3( undef, 5, "S5Client S5ReqTrans $tbuffer -->" );
  116. }
  117. @cbuffer = unpack( "C" x $count, $buffer );
  118. if ( $self->compare( \@cbuffer, \@{ $self->{__daveT161003} } ) == 0 ) {
  119. main::Log3( undef, 3, "S5Client S5ReqTrans: no accept0 from plc" );
  120. return -3;
  121. }
  122. $self->S5SendDLEACK();
  123. return 0;
  124. }
  125. sub S5SendDLEACK($) {
  126. my ($self) = @_;
  127. my $buffer = pack( 'C2', @{ $self->{__davet1006} } );
  128. if ( $main::attr{global}{verbose} >= 5 ) {
  129. my $tbuffer = join( ", ", unpack( "H2 " x 2, $buffer ) );
  130. main::Log3( undef, 5, "S5Client S5SendDLEACK <-- $tbuffer" );
  131. }
  132. return $self->{serial}->write($buffer);
  133. }
  134. #---------------------------------------------- S5 Exchange data
  135. sub S5ExchangeAS511($$$$$) {
  136. my ( $self, $b, $len, $maxlen, $trN ) = @_;
  137. my ( $res, $i, $b1, $count );
  138. my @cbuffer;
  139. my $msgIn = "";
  140. my $tbuffer;
  141. $res = $self->S5ReqTrans($trN);
  142. if ( $res < 0 ) {
  143. main::Log3( undef, 3,
  144. "S5Client S5ExchangeAS511: Error in Exchange.ReqTrans request" );
  145. return ( $res - 10, "" );
  146. }
  147. if ( $trN == 8 ) { #Block write functions have advanced syntax
  148. #LOG1("trN 8\n");
  149. $self->S5SendWithDLEDup( $b, 4 );
  150. #LOG1("trN 8 done\n");
  151. }
  152. else {
  153. #LOG3("trN %d len %d\n",trN,len);
  154. $self->S5SendWithDLEDup( $b, $len );
  155. #LOG2("trN %d done\n",trN);
  156. }
  157. ( $count, $b1 ) = $self->{serial}->read(2);
  158. # if ( $main::attr{global}{verbose} >= 5 ) {
  159. $tbuffer = join( ", ", unpack( "H2 " x $count, $b1 ) );
  160. main::Log3( undef, 5, "S5Client S5ExchangeAS511 $tbuffer -->" );
  161. # }
  162. @cbuffer = unpack( "C" x $count, $b1 );
  163. if ( $self->compare( \@cbuffer, \@{ $self->{__davet1006} } ) == 0 ) {
  164. main::Log3( undef, 3,
  165. "S5Client S5ExchangeAS511: no DLE,ACK in Exchange request" );
  166. return ( -1, "" );
  167. }
  168. if ( ( $trN != 3 ) && ( $trN != 7 ) && ( $trN != 9 ) ) {
  169. #write bytes, compress & delblk
  170. if ( !$self->S5ReadSingle() eq &STX ) {
  171. main::Log3( undef, 3,
  172. "S5Client S5ExchangeAS511: no STX in Exchange request" );
  173. return ( -2, "" );
  174. }
  175. $self->S5SendDLEACK();
  176. $res = 0;
  177. @cbuffer = ();
  178. my $buffer = "";
  179. do {
  180. ( $i, $b1 ) = $self->{serial}->read(1);
  181. $res += $i;
  182. push( @cbuffer, unpack( "C" x $i, $b1 ) ) if ( $i > 0 );
  183. } while (
  184. ( $i > 0 )
  185. && ( ( $cbuffer[ $res - 2 ] != &DLE )
  186. || ( $cbuffer[ $res - 1 ] != &ETX ) )
  187. );
  188. if ( $main::attr{global}{verbose} >= 5 ) {
  189. $tbuffer =
  190. join( ", ", unpack( "H2 " x @cbuffer, pack( "C*", @cbuffer ) ) );
  191. main::Log3( undef, 5, "S5Client S5ExchangeAS511 $tbuffer -->" );
  192. }
  193. #LOG3( "%s *** got %d bytes.\n", dc->iface->name, res );
  194. if ( $res < 0 ) {
  195. main::Log3( undef, 3,
  196. "S5Client S5ExchangeAS511: Error in Exchange.ReadChars request"
  197. );
  198. return ( $res - 20, "" );
  199. }
  200. if ( ( $cbuffer[ $res - 2 ] != &DLE )
  201. || ( $cbuffer[ $res - 1 ] != &ETX ) )
  202. {
  203. main::Log3( undef, 3,
  204. "S5Client S5ExchangeAS511: No DLE,ETX in Exchange data." );
  205. return ( -4, "" );
  206. }
  207. ( $res, $msgIn ) = $self->S5DLEDeDup( \@cbuffer );
  208. if ( $res < 0 ) {
  209. main::Log3( undef, 3,
  210. "S5Client S5ExchangeAS511: Error in Exchange rawdata." );
  211. return ( -3, "" );
  212. }
  213. $self->S5SendDLEACK();
  214. }
  215. if ( $trN == 8 ) { # Write requests have more differences from others
  216. @cbuffer = unpack( "C" x length($msgIn), $msgIn );
  217. if ( $cbuffer[0] != 9 ) { #todo fix
  218. main::Log3( undef, 3,
  219. "S5Client S5ExchangeAS511 No 0x09 in special Exchange request."
  220. );
  221. return ( -5, "" );
  222. }
  223. $self->S5SendSingle(&STX);
  224. ( $count, $b1 ) = $self->{serial}->read(2);
  225. if ( $main::attr{global}{verbose} >= 5 ) {
  226. $tbuffer = $tbuffer = join( ", ", unpack( "H2 " x $count, $b1 ) );
  227. main::Log3( undef, 5, "S5Client S5ExchangeAS511 $tbuffer -->" );
  228. }
  229. @cbuffer = unpack( "C" x $count, $b1 );
  230. if ( $self->compare( \@cbuffer, \@{ $self->{__davet1006} } ) == 0 ) {
  231. main::Log3( undef, 3,
  232. "S5Client S5ExchangeAS511 no DLE,ACK in special Exchange request"
  233. );
  234. return ( -6, "" );
  235. }
  236. my $b2 = substr( $b, 4 );
  237. $self->S5SendWithDLEDup( $b2, $len ); # todo need testing !!!
  238. #$self->S5SendWithDLEDup(dc->iface,b+4,len); #
  239. ( $count, $b1 ) = $self->{serial}->read(2);
  240. if ( $main::attr{global}{verbose} >= 5 ) {
  241. $tbuffer = join( ", ", unpack( "H2 " x $count, $b1 ) );
  242. main::Log3( undef, 5, "S5Client S5ExchangeAS511 $tbuffer -->" );
  243. }
  244. @cbuffer = unpack( "C" x $count, $b1 );
  245. if ( $self->compare( \@cbuffer, \@{ $self->{__davet1006} } ) == 0 ) {
  246. main::Log3( undef, 3,
  247. "S5Client S5ExchangeAS511 no DLE,ACK after transfer in Exchange."
  248. );
  249. return ( -7, "" );
  250. }
  251. }
  252. if ( $trN == 7 ) {
  253. }
  254. $res = $self->S5EndTrans();
  255. if ( $res < 0 ) {
  256. main::Log3( undef, 3,
  257. "S5Client S5ExchangeAS511 Error in Exchange.EndTrans request." );
  258. return ( $res - 30, "" );
  259. }
  260. return ( 0, $msgIn );
  261. }
  262. #
  263. # Sends a sequence of characters after doubling DLEs and adding DLE,EOT.
  264. #
  265. sub S5SendWithDLEDup($$$) {
  266. my ( $self, $b, $size ) = @_;
  267. # uc target[&daveMaxRawLen];
  268. my @target;
  269. my $res;
  270. my $i; #preload
  271. my @cbuffer = unpack( "C" x $size, $b );
  272. #LOG1("SendWithDLEDup: \n");
  273. #_daveDump("I send",b,size);
  274. for ( $i = 0 ; $i < $size ; $i++ ) {
  275. push( @target, $cbuffer[$i] );
  276. if ( $cbuffer[$i] == &DLE ) {
  277. push( @target, &DLE );
  278. }
  279. }
  280. push( @target, &DLE );
  281. push( @target, &EOT );
  282. #LOGx_daveDump("I send", target, targetSize);
  283. my $buffer = pack( 'C*', @target );
  284. $res = $self->{serial}->write($buffer);
  285. if ( $main::attr{global}{verbose} >= 5 ) {
  286. my $tbuffer = join( ", ", unpack( "H2 " x length($buffer), $buffer ) );
  287. main::Log3( undef, 5, "S5Client S5SendWithDLEDup <-- $tbuffer" );
  288. }
  289. #if(daveDebug & daveDebugExchange)
  290. #LOG2("send: res:%d\n",res);
  291. return 0;
  292. }
  293. #
  294. # Remove the DLE doubling:
  295. #
  296. sub S5DLEDeDup($$) {
  297. my ( $self, $b ) = @_;
  298. my @rawBuf = @{$b};
  299. my @msg = ();
  300. my $j = 0;
  301. my $k;
  302. for ( $k = 0 ; $k < @rawBuf - 2 ; $k++ ) {
  303. push( @msg, $rawBuf[$k] );
  304. if ( DLE == $rawBuf[$k] ) {
  305. if ( DLE != $rawBuf[ $k + 1 ] ) {
  306. return ( -1, "" ); #Bad doubling found
  307. }
  308. $k++;
  309. }
  310. }
  311. push( @msg, $rawBuf[$k] );
  312. $k++;
  313. push( @msg, $rawBuf[$k] );
  314. $b = pack( 'C*', @msg );
  315. return ( 0, $b );
  316. }
  317. #
  318. # Executes part of the dialog required to terminate transaction:
  319. #
  320. sub S5EndTrans($) {
  321. my ($self) = @_;
  322. #LOG2("%s daveEndTrans\n", dc->iface->name);
  323. if ( $self->S5ReadSingle() ne &STX ) {
  324. #LOG2("%s daveEndTrans *** no STX at eot sequense.\n", dc->iface->name);
  325. #return -1;
  326. }
  327. $self->S5SendDLEACK();
  328. my ( $res, $b1 ) = $self->{serial}->read(3);
  329. if ( $main::attr{global}{verbose} >= 5 ) {
  330. my $tbuffer = join( ", ", unpack( "H2 " x $res, $b1 ) );
  331. main::Log3( undef, 5, "S5Client S5EndTrans $tbuffer -->" );
  332. }
  333. #_daveDump("3got",b1, res);
  334. my @cbuffer = unpack( "C" x $res, $b1 );
  335. if ( $self->compare( \@cbuffer, \@{ $self->{__davet121003} } ) == 0 ) {
  336. main::Log3( undef, 3,
  337. "S5Client S5EndTransno accept of eot/ETX from plc." );
  338. return -2;
  339. }
  340. $self->S5SendDLEACK();
  341. return 0;
  342. }
  343. #
  344. # This reads a single chracter from the serial interface:
  345. sub S5ReadSingle ($) {
  346. my ($self) = @_;
  347. my ( $res, $i );
  348. ( $i, $res ) = $self->{serial}->read(1);
  349. if ( $main::attr{global}{verbose} >= 5 ) {
  350. my $tbuffer = join( ", ", unpack( "H2 " x $i, $res ) );
  351. main::Log3( undef, 5, "S5Client S5ReadSingle $tbuffer -->" );
  352. }
  353. #if ((daveDebug & daveDebugSpecialChars)!=0)
  354. # LOG3("readSingle %d chars. 1st %02X\n",i,res);
  355. if ( $i == 1 ) {
  356. return $res;
  357. }
  358. return 0;
  359. }
  360. #--------------------------------------------------------------------------------
  361. # Connect to S5 CPU
  362. #
  363. sub S5ConnectPLCAS511($$) {
  364. my ( $self, $portName ) = @_;
  365. my $b1 = "";
  366. my $ttyPort;
  367. #if ( OS_Linux() ) {
  368. $self->{serial} = new Device::SerialPort($portName);
  369. #}
  370. #else {
  371. # $ttyPort = new Win32::SerialPort( $portName );
  372. #}
  373. main::Log3( undef, 3, "Can't open serial port $portName" )
  374. unless ( $self->{serial} );
  375. die unless ( $self->{serial} );
  376. $self->{serial}->baudrate(9600);
  377. $self->{serial}->databits(8);
  378. $self->{serial}->parity('even');
  379. $self->{serial}->stopbits(1);
  380. $self->{serial}->read_const_time(500); # 500 milliseconds = 0.5 seconds
  381. $self->{serial}->read_char_time(10); # avg time between read char
  382. #$ttyPort->handshake('none');
  383. #$ttyPort->stty_icrnl(1);
  384. #$ttyPort->stty_ocrnl(1);
  385. #$ttyPort->stty_onlcr(1);
  386. #$ttyPort->stty_opost(1)
  387. $self->{serial}->write_settings();
  388. $b1 = pack( "C*", 0, 0 );
  389. my ( $res, $msgIn ) =
  390. $self->S5ExchangeAS511( $b1, 2, &maxSysinfoLen, 0x18 );
  391. if ( $res < 0 ) {
  392. main::Log3( undef, 3,
  393. "S5Client S5ConnectPLCAS511 ImageAddr.Exchange sequence" );
  394. return $res - 10;
  395. }
  396. if ( length($msgIn) < 47 ) {
  397. main::Log3( undef, 3,
  398. "S5Client S5ConnectPLCAS511 Too few chars in ImageAddr data" );
  399. return -2;
  400. }
  401. #_daveDump("connect:",dc->msgIn, 47);
  402. my @cbuffer = unpack( "C" x length($msgIn), $msgIn );
  403. $self->{S5PAEAddress} =
  404. $self->WordAt( \@cbuffer, 5 ); # start of inputs;
  405. $self->{S5PAAAddress} = $self->WordAt( \@cbuffer, 7 ); # start of outputs
  406. $self->{S5flagsAddress} =
  407. $self->WordAt( \@cbuffer, 9 ); # start of flag (marker) memory;
  408. $self->{S5timerAddress} =
  409. $self->WordAt( \@cbuffer, 11 ); #start of timer memory;
  410. $self->{S5counterAddress} =
  411. $self->WordAt( \@cbuffer, 13 ); #start of counter memory
  412. main::Log3( undef, 3,
  413. "S5Client ->S5ConnectPLCAS511 start of inputs in memory "
  414. . $self->{S5PAEAddress} );
  415. main::Log3( undef, 3,
  416. "S5Client ->S5ConnectPLCAS511 start of outputs in memory "
  417. . $self->{S5PAAAddress} );
  418. main::Log3( undef, 3,
  419. "S5Client ->S5ConnectPLCAS511 start of flags in memory "
  420. . $self->{S5flagsAddress} );
  421. main::Log3( undef, 3,
  422. "S5Client ->S5ConnectPLCAS511 start of timers in memory "
  423. . $self->{S5timerAddress} );
  424. main::Log3( undef, 3,
  425. "S5Client ->S5ConnectPLCAS511 start of counters in memory "
  426. . $self->{S5counterAddress} );
  427. return 0;
  428. }
  429. #
  430. # Reads <count> bytes from area <BlockN> with offset <offset>,
  431. # that can be readed with daveGetInteger etc. You can read bytes from
  432. # PBs & FBs too, but use daveReadBlock for this:
  433. #
  434. sub S5ReadS5Bytes($$$$$) {
  435. my ( $self, $area, $BlockN, $offset, $count ) = @_;
  436. my ( $res, $dataend, $datastart, $b1, $msgIn );
  437. if ( $area == &S7ClientBase::S7AreaDB ) { #DB
  438. ( $res, $datastart ) = $self->S5ReadS5BlockAddress( $area, $BlockN );
  439. if ( $res < 0 ) {
  440. main::Log3( undef, 3,
  441. "S5Client S5ReadS5Bytes Error in ReadS5Bytes.BlockAddr request"
  442. );
  443. return ( $res - 50, "" );
  444. }
  445. }
  446. elsif ( $area == &S7ClientBase::S7AreaPE ) { #inputs
  447. $datastart =
  448. $self->{S5PAEAddress}; #need to get this information from a property
  449. }
  450. elsif ( $area == &S7ClientBase::S7AreaPA ) { #outputs
  451. $datastart =
  452. $self->{S5PAAAddress}; #need to get this information from a property
  453. }
  454. elsif ( $area == &S7ClientBase::S7AreaMK ) { #flags
  455. $datastart =
  456. $self->{S5flagsAddress}; #need to get this information from a property
  457. }
  458. elsif ( $area == &S7ClientBase::S7AreaTM ) { #timers
  459. $datastart =
  460. $self->{S5timerAddress}; #need to get this information from a property
  461. }
  462. elsif ( $area == &S7ClientBase::S7AreaCT ) { #counters
  463. $datastart = $self
  464. ->{S5counterAddress}; #need to get this information from a property
  465. }
  466. else {
  467. main::Log3( undef, 3,
  468. "S5Client S5ReadS5Bytes Unknown area in ReadS5Bytes request" );
  469. return ( -1, "" );
  470. }
  471. if ( $count > &daveMaxRawLen ) {
  472. main::Log3( undef, 3,
  473. "S5Client S5ReadS5Bytes: Requested data is out-of-range" );
  474. return ( -1, "" );
  475. }
  476. $datastart += $offset;
  477. $dataend = $datastart + $count - 1;
  478. $b1 = pack( "C*",
  479. $datastart / 256,
  480. $datastart % 256,
  481. $dataend / 256,
  482. $dataend % 256 );
  483. ( $res, $msgIn ) = $self->S5ExchangeAS511( $b1, 4, 2 * $count + 7, 0x04 );
  484. if ( $res < 0 ) {
  485. main::Log3( undef, 3,
  486. "S5Client S5ReadS5Bytes Error in ReadS5Bytes.Exchange sequence" );
  487. return ( $res - 10, "" );
  488. }
  489. #if (dc->AnswLen<count+7) { #todo implement this check
  490. # LOG3("%s *** Too few chars (%d) in ReadS5Bytes data.\n", dc->iface->name,dc->AnswLen);
  491. #return (-5,"");
  492. #}
  493. my @cbuffer = unpack( "C" x length($msgIn), $msgIn );
  494. if ( ( $cbuffer[0] != 0 )
  495. || ( $cbuffer[1] != 0 )
  496. || ( $cbuffer[2] != 0 )
  497. || ( $cbuffer[3] != 0 )
  498. || ( $cbuffer[4] != 0 ) )
  499. {
  500. main::Log3( undef, 3,
  501. "S5Client S5ReadS5Bytes Wrong ReadS5Bytes data signature" );
  502. return ( -6, "" );
  503. }
  504. $msgIn = substr( $msgIn, 5, -2 );
  505. return ( 0, $msgIn );
  506. }
  507. #
  508. # Requests physical addresses and lengths of blocks in PLC memory and writes
  509. # them to ai structure:
  510. #
  511. sub S5ReadS5BlockAddress($$$) {
  512. my ( $self, $area, $BlockN ) = @_;
  513. my ( $res, $msgIn, $dbaddr, $dblen, $ai );
  514. my $b1 = pack( "C*", &daveS5BlockType_DB, $BlockN )
  515. ; #note we only support DB, no PB,FB,SB
  516. ( $res, $msgIn ) = $self->S5ExchangeAS511( $b1, 2, 24, 0x1A );
  517. if ( $res < 0 ) {
  518. main::Log3( undef, 3,
  519. "S5Client >S5ReadS5BlockAddress Error in BlockAddr.Exchange sequense"
  520. );
  521. return ( $res - 10, 0, 0 );
  522. }
  523. if ( length($msgIn) < 15 ) {
  524. main::Log3( undef, 3,
  525. "S5Client S5ReadS5BlockAddress Too few chars in BlockAddr data." );
  526. return ( -2, 0, 0 );
  527. }
  528. my @cbuffer = unpack( "C" x length($msgIn), $msgIn );
  529. if ( ( $cbuffer[0] != 0 )
  530. || ( $cbuffer[3] != 0x70 )
  531. || ( $cbuffer[4] != 0x70 )
  532. || ( $cbuffer[5] != 0x40 + &daveS5BlockType_DB )
  533. || ( $cbuffer[6] != $BlockN ) )
  534. {
  535. main::Log3( undef, 3,
  536. "S5Client S5ReadS5BlockAddress Wrong BlockAddr data signature." );
  537. return ( -3, 0, 0 );
  538. }
  539. $dbaddr = $cbuffer[1];
  540. $dbaddr =
  541. $dbaddr * 256 +
  542. $cbuffer[2]; #Let make shift operations to compiler's optimizer
  543. $dblen = $cbuffer[11];
  544. $dblen =
  545. ( $dblen * 256 + $cbuffer[12] - 5 ) *
  546. 2; #PLC returns dblen in words including
  547. #5 word header (but returnes the
  548. #start address after the header) so
  549. #dblen is length of block body
  550. return ( 0, $dbaddr, $dblen );
  551. }
  552. #
  553. # Writes <count> bytes from area <BlockN> with offset <offset> from buf.
  554. # You can't write data to the program blocks because you can't syncronize
  555. # with PLC cycle. For this purposes use daveWriteBlock:
  556. #
  557. sub S5WriteS5Bytes($$$$$$) {
  558. my ( $self, $area, $BlockN, $offset, $count, $buf ) = @_;
  559. my ( $res, $datastart, $dblen, $b1, $msgIn );
  560. if ( $area == &S7ClientBase::S7AreaDB ) { #DB
  561. ( $res, $datastart, $dblen ) =
  562. $self->S5ReadS5BlockAddress( $area, $BlockN );
  563. if ( $res < 0 ) {
  564. main::Log3( undef, 3,
  565. "S5Client S5WriteS5Bytes Error in ReadS5Bytes.BlockAddr request."
  566. );
  567. return $res - 50;
  568. }
  569. }
  570. elsif ( $area == &S7ClientBase::S7AreaPE ) { #inputs
  571. $datastart =
  572. $self->{S5PAEAddress}; #need to get this information from a property
  573. $dblen = 128;
  574. }
  575. elsif ( $area == &S7ClientBase::S7AreaPA ) { #outputs
  576. $datastart =
  577. $self->{S5PAAAddress}; #need to get this information from a property
  578. $dblen = 128;
  579. }
  580. elsif ( $area == &S7ClientBase::S7AreaMK ) { #flags
  581. $datastart =
  582. $self->{S5flagsAddress}; #need to get this information from a property
  583. #$dblen = 128; # S5-90U
  584. $dblen = 256; # S5-95U
  585. }
  586. elsif ( $area == &S7ClientBase::S7AreaTM ) { #timers
  587. $datastart =
  588. $self->{S5timerAddress}; #need to get this information from a property
  589. #$dblen = 32 *2; # S5-90U
  590. $dblen = 128 *2; # S5-95U
  591. }
  592. elsif ( $area == &S7ClientBase::S7AreaCT ) { #counters
  593. $datastart = $self
  594. ->{S5counterAddress}; #need to get this information from a property
  595. #$dblen = 32 *2; # S5-90U
  596. $dblen = 128 * 2; # S5-95U
  597. }
  598. else {
  599. main::Log3( undef, 3,
  600. "S5Client S5WriteS5Bytes Unknown area in WriteS5Bytes request." );
  601. return -1;
  602. }
  603. if ( ( $count > &daveMaxRawLen ) || ( $offset + $count > $dblen ) ) {
  604. main::Log3( undef, 3,
  605. "S5Client S5WriteS5Bytes Requested data is out-of-range." );
  606. return -1;
  607. }
  608. #LOG2("area start is %04x, ",datastart);
  609. $datastart += $offset;
  610. #LOG2("data start is %04x\n",datastart);
  611. $b1 = pack( "C*", $datastart / 256, $datastart % 256 );
  612. $b1 = $b1 . $buf;
  613. ( $res, $msgIn ) = $self->S5ExchangeAS511( $b1, 2 + $count, 0, 0x03 );
  614. if ( $res < 0 ) {
  615. main::Log3( undef, 3,
  616. "S5Client S5WriteS5Bytes Error in WriteS5Bytes.Exchange sequense."
  617. );
  618. return $res - 10;
  619. }
  620. return 0;
  621. }
  622. 1;
  623. =pod
  624. =item summary low level interface to S5
  625. =item summary_DE low level interface to S5
  626. =begin html
  627. <p><a name="S7_S5Client"></a></p>
  628. <h3>S7_S5Client</h3>
  629. <ul>
  630. <ul>low level interface to S5</ul>
  631. </ul>
  632. =end html
  633. =begin html_DE
  634. <p><a name="S7_S5Client"></a></p>
  635. <h3>S7_S5Client</h3>
  636. <ul>
  637. <ul>low level interface to S5</ul>
  638. </ul>
  639. =end html_DE
  640. =cut