Skip to content

azelcs/opcua-client-ruby

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

opcua-client-ruby

Incomplete OPC-UA client library for Ruby. Wraps open62541: https://open62541.org.

Installation

Add it to your Gemfile:

gem 'opcua_client'

Basic usage

Use start helper to automatically close connections:

require 'opcua_client'

OPCUAClient.start("opc.tcp://127.0.0.1:4840") do |client|
  # write to ns=2;s=1
  client.write_int16(2, "1", 888)
  puts client.read_int16(2, "1")
end

Or handle connections manually:

require 'opcua_client'

client = OPCUAClient::Client.new
begin
  client.connect("opc.tcp://127.0.0.1:4840")
  # write to ns=2;s=1
  client.write_int16(2, "1", 888)
  puts client.read_int16(2, "1")

  client.multi_write_int16(2, (1..10).map{|x| "action_#{x}"}, (1..10).map{|x| x * 10}) # 10x writes
  client.multi_write_int32(2, (1..10).map{|x| "amount_#{x}"}, (1..10).map{|x| x * 10 + 1}) # 10x writes
ensure
  client.disconnect
end

Available methods - connection:

  • client.connect(String url) - raises OPCUAClient::Error if unsuccessful
  • client.disconnect => Fixnum - returns status

Available methods - reads and writes:

All methods raise OPCUAClient::Error if unsuccessful.

  • client.read_int16(Fixnum ns, String name) => Fixnum
  • client.read_uint16(Fixnum ns, String name) => Fixnum
  • client.read_int32(Fixnum ns, String name) => Fixnum
  • client.read_uint32(Fixnum ns, String name) => Fixnum
  • client.read_float(Fixnum ns, String name) => Float
  • client.read_boolean(Fixnum ns, String name) => true/false
  • client.write_int16(Fixnum ns, String name, Fixnum value)
  • client.write_uint16(Fixnum ns, String name, Fixnum value)
  • client.write_int32(Fixnum ns, String name, Fixnum value)
  • client.write_uint32(Fixnum ns, String name, Fixnum value)
  • client.write_float(Fixnum ns, String name, Float value)
  • client.write_boolean(Fixnum ns, String name, bool value)
  • client.multi_write_int16(Fixnum ns, Array[String] names, Array[Fixnum] values)
  • client.multi_write_int32(Fixnum ns, Array[String] names, Array[Fixnum] values)
  • client.multi_write_float(Fixnum ns, Array[String] names, Array[Float] values)
  • client.multi_write_boolean(Fixnum ns, Array[String] names, Array[bool] values)

Available methods - misc:

  • client.state => Fixnum - client internal state
  • client.human_state => String - human readable client internal state
  • OPCUAClient::Client.human_status_code(Fixnum status) => String - returns human status for status

Subscriptions and monitoring

cli = OPCUAClient::Client.new

cli.after_session_created do |cli|
  subscription_id = cli.create_subscription
  ns_index = 1
  node_name = "the.answer"
  cli.add_monitored_item(subscription_id, ns_index, node_name)
end

cli.after_data_changed do |subscription_id, monitor_id, server_time, source_time, new_value|
  puts("data changed: " + [subscription_id, monitor_id, server_time, source_time, new_value].inspect)
end

cli.connect("opc.tcp://127.0.0.1:4840")

loop do
  cli.connect("opc.tcp://127.0.0.1:4840") # no-op if connected
  cli.run_mon_cycle
  sleep(0.2)
end

Available methods:

  • client.create_subscription => Fixnum - nil if error
  • client.add_monitored_item(Fixnum subscription, Fixnum ns, String name) => Fixnum - nil if error
  • client.run_mon_cycle - returns status
  • client.run_mon_cycle! - raises OPCUAClient::Error if unsuccessful

Available callbacks:

  • after_session_created
  • after_data_changed

Contribute

Set up

bundle

Try out changes

$ rake compile
$ bin/console
pry> client = OPCUAClient::Client.new
pry> client.connect("opc.tcp://127.0.0.1:4840")

About

Incomplete OPC-UA client library for Ruby

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C 99.9%
  • Ruby 0.1%