Method: OpenC3::SimulatedTargetInterface#read

Defined in:
lib/openc3/interfaces/simulated_target_interface.rb

#readPacket

Returns a simulated target packet from the simulator

Returns:

  • (Packet)

    Returns a simulated target packet from the simulator



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/openc3/interfaces/simulated_target_interface.rb', line 66

def read
  packet = nil
  if @connected
    packet = first_pending_packet()
    if packet
      # Support read_packet (but not read data) in protocols
      # Generic protocol use is not supported
      @read_protocols.each do |protocol|
        packet = protocol.read_packet(packet)
        if packet == :DISCONNECT
          Logger.info("#{@name}: Protocol #{protocol.class} read_packet requested disconnect")
          return nil
        end
        break if packet == :STOP
      end
      return packet unless packet == :STOP
    end

    while true
      # Calculate time to sleep to make ticks the right distance apart
      now = Time.now.sys
      delta = @next_tick_time - now
      if delta > 0.0
        sleep(delta) # Sleep between packets
        return nil unless @connected
      elsif delta < -1.0
        # Fell way behind - jump next tick time
        @next_tick_time = Time.now.sys
      end

      @pending_packets = @sim_target.read(@count_100hz, @next_tick_time)
      @next_tick_time += @sim_target.tick_period_seconds
      @count_100hz += @sim_target.tick_increment

      packet = first_pending_packet()
      if packet
        # Support read_packet (but not read data) in protocols
        # Generic protocol use is not supported
        @read_protocols.each do |protocol|
          packet = protocol.read_packet(packet)
          if packet == :DISCONNECT
            Logger.info("#{@name}: Protocol #{protocol.class} read_packet requested disconnect")
            return nil
          end
          break if packet == :STOP
        end
        next if packet == :STOP

        return packet
      end
    end
  else
    raise "Interface not connected"
  end
  return packet
end