Class: HexData

Inherits:
Object
  • Object
show all
Defined in:
lib/hexdata.rb

Overview

Note:

The file hexdata.rb is available under the GNU GPL v3.

Examples:

require "hexdata"
hex = HexData.new [":04001000746573742C", ":0D00000048656C6C6F20576F726C642100B6", ":10004000404142434445464748494A4B4C4D4E4F38", ":00000001FF"] # Open to read
hex.interpret # Interpret data
p hex.ascii # return string result of the interpret
p hex.checksum # return checksum from the first data
p hex.checksum 1 # return checksum from the second data

hex = HexData.new # Open to write/read
hex.push "test" # Write "test"
hex.push_end # Write End of File
hex.interpret # Interpret data
p hex.ascii 0 # return string result of the interpret of the first data 

Author:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sets = []) ⇒ HexData

Initalies the cash register hexdata

Parameters:

  • sets (Array) (defaults to: [])

    The (Intel) hex data to be read or edited. Default: []



57
58
59
60
61
# File 'lib/hexdata.rb', line 57

def initialize sets=[]
  @sets = sets
  @push_address = 16
  @enddef = ":00000001FF"
end

Instance Attribute Details

#enddefString

The “end of file” mark

Returns:

  • (String)

    the current value of enddef



51
52
53
# File 'lib/hexdata.rb', line 51

def enddef
  @enddef
end

#push_addressInteger

The address of the next record in the (Intel) hex file

Returns:

  • (Integer)

    the current value of push_address



51
52
53
# File 'lib/hexdata.rb', line 51

def push_address
  @push_address
end

Instance Method Details

#ascii(index = nil) ⇒ Object

Reads the result, the ASCII / ANSI text, the evaluation (interpretation).

Parameters:

  • index (Integer) (defaults to: nil)

    The index of the record to be read. If all is to be read out, nil can be transferred. Default: nil

Returns:

  • The result of all records, if no index was specified, otherwise only the result of the record from which the index was specified.



167
168
169
# File 'lib/hexdata.rb', line 167

def ascii index=nil
  return (index ? @ascii[index] : @ascii.join(""))
end

#checksum?(index = 0) ⇒ Integer

Reads the checksum of a record.

Parameters:

  • index (Integer) (defaults to: 0)

    The index of the record from which the checksum is to be read. Default: 0

Returns:

  • (Integer)

    The read checksum



160
161
162
# File 'lib/hexdata.rb', line 160

def checksum? index=0
  return @sums[index]
end

#clear!NilClass

Resets the hex data. All saved hex data will be deleted. Then HexData is ready to write.

Returns:

  • (NilClass)

    nil



136
137
138
139
140
141
# File 'lib/hexdata.rb', line 136

def clear!
  @sets = nil.to_a
  @sums = nil.to_a
  @push_address = 16
  nil
end

#dataArray

Note:

No copy of the array is created. If you edit the arrays, this affects the HexData class. If you want a copy of the array, you can, for example, use the .clone method.

Returns the records

Returns:

  • (Array)

    The records returned as an array



66
67
68
# File 'lib/hexdata.rb', line 66

def data
  @sets
end

#data_end?Integer

Returns the index of the first record that detected an “End of File” markup.

Returns:

  • (Integer)

    The index of the first element where an “End of file”-mark was detected.



151
152
153
154
155
# File 'lib/hexdata.rb', line 151

def data_end?
  for i in 0...@sets.length
    return i if @sets[i] == @enddef
  end
end

#delete_data!(index) ⇒ NilClass

The string to be written.

Parameters:

  • index (Integer)

    The index of the record to be deleted.

Returns:

  • (NilClass)

    nil



129
130
131
132
# File 'lib/hexdata.rb', line 129

def delete_data! index
  @sets.delete_at index
  nil
end

#interpretNilClass

Evaluate the (Intel) Hex data (interpretation) and make it available for reading.

Returns:

  • (NilClass)

    nil



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
# File 'lib/hexdata.rb', line 72

def interpret
  @ascii = nil.to_a
  @sums = nil.to_a
  @sets.each { |data|
    if data[0] != ":"
      raise "Invalid data! Data: #{data}"
    end
  
    len = data[1 .. 2].to_i 16
    address = data[3 .. 6]
    type = data[7 .. 8]
    field = data[9 .. len * 2 + 8]
    sum = data[-2 .. -1].to_i 16
    str = ""

    csum = len + address.to_i(16) + type.to_i(16)
    #p field
    for i in (0..field.length-1).step(2)
      v = field[i .. i + 1].to_i 16
      csum += v
      str += v.chr
    end
  
    csum &= 255
    bsum = ""
    csum.to_s(2).each_char { |c| bsum += (c=="0"?"1":"0") }
    while bsum.length < 8
      bsum = "1" + bsum
    end

    if bsum.to_i(2) + 1 != sum
      raise "Invalid checksum! Data: #{data}"
    end
    @ascii << str if type == "00"
    @sums << sum
  }
  nil
end

#max_index?Integer

Returns the number of records.

Returns:

  • (Integer)

    The number of records.



145
146
147
# File 'lib/hexdata.rb', line 145

def max_index?
  @sets.length
end

#push(str) ⇒ NilClass

Write a user-entered text to the records as (Intel) Hex.

Parameters:

  • str (String)

    The text to be written to the records.

Returns:

  • (NilClass)

    nil



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/hexdata.rb', line 181

def push str

  if @push_address > 65535
    raise "To many data!"
  end

  sum = str.length + @push_address
  res = "#{str.length.to_s 16}"
  while res.length < 2
    res = "0" + res
  end
  
  res = ":" + res
  
  tmp = @push_address.to_s 16
  while tmp.length < 4
    tmp = "0" + tmp
  end
  res += tmp + "00"
  
  str.each_char { |c|
    sum += c.ord
    tmp = c.ord.to_s 16
    while tmp.length < 2
      tmp = "0" + tmp
    end
    res += tmp
  }
  
  sum &= 255
  bsum = ""
  sum.to_s(2).each_char { |c| bsum += (c=="0"?"1":"0") }
  while bsum.length < 8
    bsum = "1" + bsum
  end

  res += bsum.to_i(2).+(1).to_s 16
  @sets << res
  @push_address += 1
end

#push_endNilClass

Writes the “End of file” mark. This is appended to the end of the records.

Returns:

  • (NilClass)

    nil



173
174
175
176
# File 'lib/hexdata.rb', line 173

def push_end
  @sets << @enddef
  nil
end

#push_user(str) ⇒ NilClass

Writes a string passed by the user to the data.

Parameters:

  • str (String)

    The user data to be written as a record.

Returns:

  • (NilClass)

    nil



121
122
123
124
# File 'lib/hexdata.rb', line 121

def push_user str
  @sets << str
  nil
end

#reset_address!NilClass

Resets the write address to the default value (16).

Returns:

  • (NilClass)

    nil



113
114
115
116
# File 'lib/hexdata.rb', line 113

def reset_address!
  @push_address = 16
  nil
end