Class: ObjectOrientedBeagleboneBlack::UartConnection

Inherits:
Object
  • Object
show all
Includes:
UartMappings
Defined in:
lib/object_oriented_beaglebone_black/uart_connection.rb

Constant Summary collapse

PARITY =
SerialPort::NONE

Constants included from UartMappings

ObjectOrientedBeagleboneBlack::UartMappings::ID_TELETYPE_DEVICE_PATH_MAPPING

Instance Method Summary collapse

Methods included from UartMappings

#load_uarts_array, #property_hash

Constructor Details

#initialize(uart_id) ⇒ UartConnection

Returns a new instance of UartConnection.



9
10
11
12
13
14
# File 'lib/object_oriented_beaglebone_black/uart_connection.rb', line 9

def initialize(uart_id)
  @uart_id = uart_id
  @slots_file_path = File.join(File.join(OBJECT_ORIENTED_BEAGLEBONE_BLACK_CONFIG["slots_directory"], "slots"))

  activate_device_tree_overlays      
end

Instance Method Details

#activate_device_tree_overlaysObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/object_oriented_beaglebone_black/uart_connection.rb', line 16

def activate_device_tree_overlays
  # Note: Since slots file acts as an interface to activate Device Tree Overlay, simply writing to it does what needs to be done. 
  #       I'm using appending here so that testing in a local environment becomes straightfoward. 

  puts "This can take a few seconds for necessary setups..."

  slots_file = File.open(@slots_file_path, "a+")
  slots_file_content = slots_file.read

  unless slots_file_content.include? device_tree_overlay_parameter

    # Just in case where the previous read operation is not fully terminated.
    sleep 1

    slots_file.write(device_tree_overlay_parameter)

    # Just in case where it takes time to load device tree overlay.
    sleep 1

  end

  # Note: Closing this file sometimes causes an error in BeagleBone Black:
  #       Errno::EEXIST: File exists @ fptr_finalize - /sys/devices/bone_capemgr.9/slots
  begin
    slots_file.close if File.exist?(@slots_file_path) && !slots_file.closed?
  rescue SystemCallError => system_call_error_message
    puts "Error happened while closing #{@slots_file_path} with the message: #{system_call_error_message}"
  end

  puts "Setups done."

end

#read(serial_baud_rate: 9600, serial_data_bits: 8, serial_stop_bits: 1, communication_command: 's') ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/object_oriented_beaglebone_black/uart_connection.rb', line 49

def read(serial_baud_rate: 9600, serial_data_bits: 8, serial_stop_bits: 1, communication_command: 's')

  data = nil

  SerialPort.open(teletype_device_path, 
                  serial_baud_rate, 
                  serial_data_bits, 
                  serial_stop_bits, 
                  PARITY) do |serial_port|
    serial_port.sync = true

    serial_port.putc(communication_command)

    data = serial_port.gets
  end   

  data.strip

end

#write(serial_baud_rate: 9600, serial_data_bits: 8, serial_stop_bits: 1, communication_command: 's') ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/object_oriented_beaglebone_black/uart_connection.rb', line 69

def write(serial_baud_rate: 9600, serial_data_bits: 8, serial_stop_bits: 1, communication_command: 's')

  data = nil

  SerialPort.open(teletype_device_path, 
                  serial_baud_rate, 
                  serial_data_bits, 
                  serial_stop_bits, 
                  PARITY) do |serial_port|
    serial_port.sync = true

    serial_port.puts(communication_command)
  end

end