Class: Snap7::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/ffi-snap7/client/client.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



19
20
21
22
23
24
# File 'lib/ffi-snap7/client/client.rb', line 19

def initialize
  @cli       = Snap7.cli_create
  @connected = false

  ObjectSpace.define_finalizer self, self.class.finalizer(@cli)
end

Class Method Details

.finalizer(ptr) ⇒ Proc



10
11
12
13
14
15
16
# File 'lib/ffi-snap7/client/client.rb', line 10

def self.finalizer(ptr)
  proc do
    ptrptr = FFI::MemoryPointer.new :pointer
    ptrptr.write_pointer ptr
    Snap7.cli_destroy ptrptr
  end
end

Instance Method Details

#connect(address, rack, slot) ⇒ Object



33
34
35
36
# File 'lib/ffi-snap7/client/client.rb', line 33

def connect(address, rack, slot)
  check_rc Snap7.cli_connect_to(@cli, address, rack, slot)
  @connected = true
end

#connected?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/ffi-snap7/client/client.rb', line 45

def connected?
  @connected
end

#db_read(db_number, start, size) ⇒ Object



63
64
65
66
67
# File 'lib/ffi-snap7/client/client.rb', line 63

def db_read(db_number, start, size)
  p_usr_data = FFI::MemoryPointer.new :uchar, size
  check_rc Snap7.cli_db_read(@cli, db_number, start, size, p_usr_data)
  p_usr_data.get_array_of_uint8 0, size
end

#db_write(db_number, start, size, data) ⇒ Object



70
71
72
73
74
# File 'lib/ffi-snap7/client/client.rb', line 70

def db_write(db_number, start, size, data)
  p_usr_data = FFI::MemoryPointer.new :uchar, size
  p_usr_data.put_array_of_uint8 0, data
  check_rc Snap7.cli_db_write(@cli, db_number, start, size, p_usr_data)
end

#disconnectObject



39
40
41
42
# File 'lib/ffi-snap7/client/client.rb', line 39

def disconnect
  Snap7.cli_disconnect @cli
  @connected = false
end

#plc_statusObject



55
56
57
58
59
60
# File 'lib/ffi-snap7/client/client.rb', line 55

def plc_status
  p_status = FFI::MemoryPointer.new :uchar, 1
  check_rc Snap7.cli_get_plc_status(@cli, p_status)
  result = p_status.get_array_of_uint8 0, 1
  { 0 => 'unknown', 4 => 'stopped', 8 => 'running'}[result.first]
end

#remote_portInteger

Returns:

  • (Integer)


86
87
88
89
90
# File 'lib/ffi-snap7/client/client.rb', line 86

def remote_port
  p_port = FFI::MemoryPointer.new(:uint16)
  check_rc Snap7.cli_get_param(@cli, P_u16_RemotePort, p_port)
  p_port.read_uint16
end

#remote_port=(port) ⇒ Object

Parameters:

  • port (Integer)

    remote port to connect to



78
79
80
81
82
# File 'lib/ffi-snap7/client/client.rb', line 78

def remote_port=(port)
  p_value = FFI::MemoryPointer.new(:uint16)
  p_value.write_uint16 port
  check_rc Snap7.cli_set_param(@cli, P_u16_RemotePort, p_value)
end

#set_session_password(password) ⇒ Object



50
51
52
# File 'lib/ffi-snap7/client/client.rb', line 50

def set_session_password(password)
  check_rc Snap7.cli_set_session_password(@cli, password)
end

#to_ptrFFI::MemoryPointer

Returns pointer to the native object.

Returns:

  • (FFI::MemoryPointer)

    pointer to the native object



28
29
30
# File 'lib/ffi-snap7/client/client.rb', line 28

def to_ptr
  @cli
end