Skip to content

Instantly share code, notes, and snippets.

@tallakt
Created September 14, 2012 11:56
Show Gist options
  • Save tallakt/3721522 to your computer and use it in GitHub Desktop.
Save tallakt/3721522 to your computer and use it in GitHub Desktop.
Nodave FFI integration test
require 'ffi'
require 'socket'
require_relative 'nodave_constants'
module Nodave
include NodaveConstants
extend FFI::Library
ffi_lib 'libnodave'
class DaveOsSerialInterface < FFI::Struct
layout :rfd, :int,
:wfd, :int
end
callback :init_adapter_func, [:pointer], :int
callback :connect_plc_func, [:pointer], :int
callback :disconnect_plc_func, [:pointer], :int
callback :disconnect_adapter_func, [:pointer], :int
callback :exchange_func, [:pointer, :pointer], :int
callback :send_message_func, [:pointer, :pointer], :int
callback :get_response_func, [:pointer], :int
callback :list_reachable_partners_func, [:pointer, :string], :int
callback :read_func, [:pointer, :buffer_in, :int], :int
callback :write_func, [:pointer, :buffer_out, :int], :int
class DaveInterface < FFI::Struct
layout :timeout, :int, # long on avr
:fd, :pointer, # DaveOsSerialType
:localMPI, :int,
:users, :int,
:name, :string,
:protocol, :int,
:speed, :int,
:ackPos, :int,
:nextConnection, :int,
:initAdapter, :init_adapter_func,
:connectPLCFunc, :connect_plc_func,
:disconnectPLCFunc, :disconnect_plc_func,
:exchangeFunc, :exchange_func,
:sendMessageFunc, :send_message_func,
:getResponseFunc, :get_response_func,
:listReachablePartnersFunc, :list_reachable_partners_func,
:realName, [:uint8, 20],
:readFunc, :read_func,
:writeFunc, :write_func,
:seqNumber, :int
end
# EXPORTSPEC daveInterface * DECL2 daveNewInterface(_daveOSserialType nfd, char * nname, int localMPI, int protocol, int speed);
attach_function :daveNewInterface, [DaveOsSerialInterface.by_value, :string, :int, :int, :int], :pointer
attach_function :daveNewConnection, [ :pointer, :int, :int, :int ], :pointer
# EXPORTSPEC int DECL2 daveDisconnectPLC(daveConnection * dc);
attach_function :daveConnectPLC, [:pointer], :int
attach_function :daveSetTimeout, [:pointer, :int], :void
attach_function :daveSetDebug, [:int], :void
attach_function :daveGetDebug, [], :int
# EXPORTSPEC int DECL2 daveReadBytes(daveConnection * dc, int area, int DB, int start, int len, void * buffer);
attach_function :daveReadBytes, [:pointer, :int, :int, :int, :int, :pointer], :int
attach_function :daveGetU32, [:pointer], :uint32
end
puts 'get debug'
p Nodave.daveGetDebug
Nodave.daveSetDebug(Nodave::DAVE_DEBUG_ALL)
p Nodave.daveGetDebug
TCPSocket.open('192.168.10.106', 102) do |socket|
#TCPSocket.open('localhost', 8808) do |socket|
file_descriptors = Nodave::DaveOsSerialInterface.new
file_descriptors[:rfd] = file_descriptors[:wfd] = socket.fileno
di = Nodave.daveNewInterface(file_descriptors, 'IF1', 0, Nodave::DAVE_PROTO_ISOTCP, Nodave::DAVE_SPEED187K)
Nodave.daveSetTimeout di, 5000000
dc = Nodave.daveNewConnection di, 2, 0, 2
if Nodave.daveConnectPLC(dc).zero?
puts 'Connected'
if Nodave.daveReadBytes(dc, Nodave::DAVE_FLAGS, 0, 0, 16, nil).zero?
puts 'Read'
puts Nodave.daveGetU32 dc
else
puts 'read failed'
end
else
puts 'Could not connect to PLC'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment