Class: Simatic::Sessions::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/simatic/sessions/session.rb

Overview

Parent of all communication sessions

Direct Known Subclasses

ExchangeSession, SetupSession

Constant Summary collapse

@@pdu_num =
0
@@max_pdu_length =
0x3c0

Instance Method Summary collapse

Instance Method Details

#make_request(payload) ⇒ Object



25
26
27
# File 'lib/simatic/sessions/session.rb', line 25

def make_request payload
  [0x03, 0x00, payload.length + 4].pack('CCn') + payload
end

#packet_numberObject



20
21
22
23
# File 'lib/simatic/sessions/session.rb', line 20

def packet_number
  @@pdu_num = 0 if @@pdu_num >= 0xffff
  @@pdu_num += 1
end

#parse_response(raw_data) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/simatic/sessions/session.rb', line 29

def parse_response raw_data
  # print "raw "; raw_data.bytes.each{|byte| printf "%02X ", byte}; puts ''

  @real_length = raw_data.length
  raise "empty response" if @real_length < 2

  @protocol_version = raw_data[0,2].unpack('n').first
  raise "unknown response 0x#{@protocol_version.to_s(16)}" if @protocol_version != 0x0300
  
  @lenght = raw_data[2,2].unpack('n').first
  raise "too short response" if @real_length < 4
  raise "broken response length #{@real_length}, must be #{@lenght}" if @real_length != @lenght
end