Method: OpenC3::Interface#write

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

#write(packet) ⇒ Object

Method to send a packet on the interface.

Parameters:

  • packet (Packet)

    The Packet to send out the interface



265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/openc3/interfaces/interface.rb', line 265

def write(packet)
  raise "Interface not connected for write: #{@name}" unless connected?
  raise "Interface not writable: #{@name}" unless write_allowed?

  _write do
    @write_count += 1

    # Potentially modify packet
    @write_protocols.each do |protocol|
      packet = protocol.write_packet(packet)
      if packet == :DISCONNECT
        Logger.info("#{@name}: Protocol #{protocol.class} write_packet requested disconnect")
        disconnect()
        return
      end
      return if packet == :STOP
    end

    data = convert_packet_to_data(packet)

    # Potentially modify packet data
    @write_protocols.each do |protocol|
      data = protocol.write_data(data)
      if data == :DISCONNECT
        Logger.info("#{@name}: Protocol #{protocol.class} write_data requested disconnect")
        disconnect()
        return
      end
      return if data == :STOP
    end

    # Actually write out data if not handled by protocol
    write_interface(data)

    # Potentially block and wait for response
    @write_protocols.each do |protocol|
      packet, data = protocol.post_write_interface(packet, data)
      if packet == :DISCONNECT
        Logger.info("#{@name}: Protocol #{protocol.class} post_write_packet requested disconnect")
        disconnect()
        return
      end
      return if packet == :STOP
    end
  end

  return nil
end