Module: AgriController::Bcc

Defined in:
lib/agri-controller/bcc.rb

Overview

module Bcc is simple bcc check SUM

example

require “rubygems” require “agri-controller” include AgriController

Bcc::bcc(str) # =>Bcc_String Bcc::bcc(“%01#RCSX0000”) # => “1d”

Bcc::bcc?(“%01#RCSX00001dr”) # => true Bcc::bcc?(“%01#RCSX0000FFr”) # => false Bcc::bcc?(“%01#RCSX0000”,“1dr”) # => true Bcc::bcc?(“%01#RCSX0000”,“1d”) # => true Bcc::bcc?(“error_str”,“error”) # => nil

Class Method Summary collapse

Class Method Details

._xor(x, y) ⇒ Object

_xor(6,2) # =>4



22
23
24
# File 'lib/agri-controller/bcc.rb', line 22

def _xor(x,y)
  x.ord ^ y.ord
end

.bcc(str) ⇒ Object

xor check SUM



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/agri-controller/bcc.rb', line 27

def bcc(str)
  x=str[0]
  (str.size-1).times do |i|
    x=x.ord ^ str[i+1].ord
  end
  #p x
  res=x.to_s(16).upcase
  res="0"+res if res.size==1
  p res
  return res
end

.bcc?(str, bcc_char = nil) ⇒ Boolean

Bcc::bcc?

example

require “agri-controller” include AgriController

   str1="%01#RCSX0000"+"1d\r"
bad_bcc="%01#RCSX0000"+"00\r"
   str2="%01#RCSX0000"
Bcc::bcc?(str1)       # =>true
Bcc::bcc?(bad_bcc)    # =>false
Bcc::bcc?(str2,"1D")  # =>true
Bcc::bcc?(str2,"1d\r")# =>true

Returns:

  • (Boolean)


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/agri-controller/bcc.rb', line 54

def bcc?(str,bcc_char=nil)
  #on Error returns nil...
  begin
    str2=str.chomp.upcase
    unless bcc_char && str2.size>2
      bcc_char=str2[-2..-1]
      str2=str2[0..-3]
    else
      bcc_char=bcc_char.chomp.upcase
    end
      return bcc_char==bcc(str2)
  rescue
    return nil
  end
end