Class: MeasReceiver::CommProtocol
- Inherits:
-
Object
- Object
- MeasReceiver::CommProtocol
- Defined in:
- lib/meas_receiver/comm_protocol.rb
Constant Summary collapse
- @@host =
'localhost'
- @@port =
'12345'
Class Method Summary collapse
-
.byte_array_to_i(ba) ⇒ Object
Convert string/byte array to number.
- .create_and_send_command(command_array, response_size, hostname = @@host, port = @@port) ⇒ Object
-
.create_send_command(command_array, response_size, hostname = @@host, port = @@port) ⇒ Object
Create String command to IoServer and send it.
- .host ⇒ Object
- .host=(_host) ⇒ Object
-
.i_to_byte_array(number, zero_fill = 0) ⇒ Object
Convert number to string/byte.
- .port ⇒ Object
- .port=(_port) ⇒ Object
-
.prepare_command_string(command_array, response_size) ⇒ Object
Create command string to IoServer command_array can be Array, String or even Fixnum.
-
.send_command(command_byte_array, hostname = @@host, port = @@port) ⇒ Object
Send command to IoServer and send it.
Instance Method Summary collapse
- #g ⇒ Object
- #get ⇒ Object
-
#initialize(_command, _response_size) ⇒ CommProtocol
constructor
A new instance of CommProtocol.
Constructor Details
#initialize(_command, _response_size) ⇒ CommProtocol
Returns a new instance of CommProtocol.
6 7 8 9 10 |
# File 'lib/meas_receiver/comm_protocol.rb', line 6 def initialize(_command, _response_size) @command = _command @response_size = _response_size @command_string = self.class.prepare_command_string(_command, _response_size) end |
Class Method Details
.byte_array_to_i(ba) ⇒ Object
Convert string/byte array to number
85 86 87 88 89 90 91 92 |
# File 'lib/meas_receiver/comm_protocol.rb', line 85 def self.byte_array_to_i(ba) number = 0 ba.each_byte do |b| number *= 256 number += b end number end |
.create_and_send_command(command_array, response_size, hostname = @@host, port = @@port) ⇒ Object
49 50 51 |
# File 'lib/meas_receiver/comm_protocol.rb', line 49 def self.create_and_send_command(command_array, response_size, hostname = @@host, port = @@port) create_send_command(command_array, response_size, hostname, port) end |
.create_send_command(command_array, response_size, hostname = @@host, port = @@port) ⇒ Object
Create String command to IoServer and send it
43 44 45 46 47 |
# File 'lib/meas_receiver/comm_protocol.rb', line 43 def self.create_send_command(command_array, response_size, hostname = @@host, port = @@port) str = prepare_command_string(command_array, response_size) puts "command_string2 #{str.inspect}" return send_command(str, hostname, port) end |
.host ⇒ Object
37 38 39 40 |
# File 'lib/meas_receiver/comm_protocol.rb', line 37 def self.host return nil unless defined? @@host return @@host end |
.host=(_host) ⇒ Object
28 29 30 |
# File 'lib/meas_receiver/comm_protocol.rb', line 28 def self.host=(_host) @@host = _host end |
.i_to_byte_array(number, zero_fill = 0) ⇒ Object
Convert number to string/byte
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/meas_receiver/comm_protocol.rb', line 95 def self.i_to_byte_array(number, zero_fill = 0) ba = '' n = number.to_i while n > 256 r = n % 256 ba += r.chr n = (n - r) / 256 end ba += n.chr # fill with zeroes if ba.size < zero_fill fill_count = zero_fill - ba.size ba += 0.chr * fill_count end # reverse magic ba.reverse end |
.port ⇒ Object
32 33 34 35 |
# File 'lib/meas_receiver/comm_protocol.rb', line 32 def self.port return nil unless defined? @@port return @@port end |
.port=(_port) ⇒ Object
24 25 26 |
# File 'lib/meas_receiver/comm_protocol.rb', line 24 def self.port=(_port) @@port = _port end |
.prepare_command_string(command_array, response_size) ⇒ Object
Create command string to IoServer command_array can be Array, String or even Fixnum
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/meas_receiver/comm_protocol.rb', line 64 def self.prepare_command_string(command_array, response_size) if command_array.kind_of?(Array) command_byte_array = command_array elsif command_array.kind_of?(String) command_byte_array = command_array.split(//) elsif command_array.kind_of?(Fixnum) command_byte_array = [command_array.chr] else raise ArgumentError end command_byte_array.size.chr + response_size.chr + command_byte_array.collect { |c| if c.kind_of? Fixnum c.chr else c.to_s end }.join('') end |
.send_command(command_byte_array, hostname = @@host, port = @@port) ⇒ Object
Send command to IoServer and send it
54 55 56 57 58 59 60 |
# File 'lib/meas_receiver/comm_protocol.rb', line 54 def self.send_command(command_byte_array, hostname = @@host, port = @@port) s = TCPSocket.open(hostname, port) s.puts(command_byte_array) data = s.gets s.close return data end |
Instance Method Details
#g ⇒ Object
12 13 14 |
# File 'lib/meas_receiver/comm_protocol.rb', line 12 def g get end |
#get ⇒ Object
16 17 18 19 |
# File 'lib/meas_receiver/comm_protocol.rb', line 16 def get d = self.class.send_command(@command_string) return self.class.byte_array_to_i(d) end |