<commit_before>require 'serialport' # http://rubydoc.info/gems/serialport/1.1.0/frames 

class SerialPortConnectionError < RuntimeError; end

class SerialPortProfile

  def initialize(device, serial_port_connection_class=SerialPort)
    raise ArgumentError.new('device must be non-nil') if device.nil?

    @device = device
    @connection_class = serial_port_connection_class
    @connection = nil
  end

  def connect
    establish_connection if @connection.nil?
  end

  def disconnect
    @connection.close unless @connection.nil? || @connection.closed?
  end

  def send_data_package(package)
    # No intelligence here, just send the package
    package.each do |byte|
      @connection.putc byte
    end
  end

  def receive_data_package
    # No intelligence here, just read the max of 64KB + 2 length bytes
    # As long as there's SOMETHING on the wire, there will be no EOFError
    max_length = (1024 * 64) + 2
    @connection.sysread(max_length)
  end

  def settings
    {
      'baud' => 57600,
      'data_bits' => 8,
      'stop_bits' => 1,
      'parity' => ::SerialPort::NONE,
      'flow_control' => ::SerialPort::HARD,
      'read_timeout' => 5000
    }

  end


  private
  def establish_connection
    @connection = @connection_class.new(
      @device,
      settings['baud'],
      settings['data_bits'],
      settings['stop_bits'],
      settings['parity']
    )

    unless @connection.nil?
      set_post_connection_controls
    else
      raise SerialPortConnectionError, "Could not establish a SerialPortProfile connection to #{@device}"
    end
  end

  def set_post_connection_controls
    @connection.flow_control = settings['flow_control']
    @connection.read_timeout = settings['read_timeout']
  end
end
<commit_msg>add max_length as parameter<commit_after>require 'serialport' # http://rubydoc.info/gems/serialport/1.1.0/frames 

class SerialPortConnectionError < RuntimeError; end

class SerialPortProfile

  def initialize(device, serial_port_connection_class=SerialPort)
    raise ArgumentError.new('device must be non-nil') if device.nil?

    @device = device
    @connection_class = serial_port_connection_class
    @connection = nil
  end

  def connect
    establish_connection if @connection.nil?
  end

  def disconnect
    @connection.close unless @connection.nil? || @connection.closed?
  end

  def send_data_package(package)
    # No intelligence here, just send the package
    package.each do |byte|
      @connection.putc byte
    end
  end

  def receive_data_package(max_length=(1024*64)+2)
    # No intelligence here, just read the max length
    # (default is 64KB plus 2 length bytes, but that is leaking abstractions)
    # As long as there's SOMETHING on the wire, there will be no EOFError
    @connection.sysread(max_length)
  end

  def settings
    {
      'baud' => 57600,
      'data_bits' => 8,
      'stop_bits' => 1,
      'parity' => ::SerialPort::NONE,
      'flow_control' => ::SerialPort::HARD,
      'read_timeout' => 5000
    }

  end


  private
  def establish_connection
    @connection = @connection_class.new(
      @device,
      settings['baud'],
      settings['data_bits'],
      settings['stop_bits'],
      settings['parity']
    )

    unless @connection.nil?
      set_post_connection_controls
    else
      raise SerialPortConnectionError, "Could not establish a SerialPortProfile connection to #{@device}"
    end
  end

  def set_post_connection_controls
    @connection.flow_control = settings['flow_control']
    @connection.read_timeout = settings['read_timeout']
  end
end

{'fraction_non_alphanumeric': 0.09269112293475217, 'fraction_numerical': 0.013441612993559227, 'mean_word_length': 3.372093023255814, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4810539', 'n_tokens_mistral': 1124, 'n_tokens_neox': 1050, 'n_words': 281}