app/models/extface/driver/datecs/fp550.rb in extface-0.4.8 vs app/models/extface/driver/datecs/fp550.rb in extface-0.4.9
- old
+ new
@@ -4,10 +4,11 @@
RESPONSE_TIMEOUT = 3 #seconds
INVALID_FRAME_RETRIES = 6 #count (bad length, bad checksum)
ACKS_MAX_WAIT = 60 #count / nothing is forever
NAKS_MAX_COUNT = 3 #count
+ BAD_SEQ_MAX_COUNT = 3
TAX_GROUPS_MAP = {
1 => "\xc0",
2 => "\xc1",
3 => "\xc2",
@@ -204,17 +205,28 @@
errors.add :base, "#{ACKS_MAX_WAIT} ACKs Received. Abort!"
end
return result
end
- def frecv(timeout) # return RespFrame or nil
- if frame_bytes = pull(timeout)
- return Frame.new(frame_bytes.b)
- else
- errors.add :base, "No data received from device"
- return nil
+ def frecv(timeout) # return Frame or nil
+ rframe = nil
+ BAD_SEQ_MAX_COUNT.times do
+ if frame_bytes = pull(timeout)
+ rframe = Frame.new(frame_bytes.b)
+ if rframe.seq.ord == sequence_number(false) #accept only current sequence number as reply
+ break
+ else
+ errors.add :base, "Sequence mismatch"
+ p "Invalid sequence (expected: #{sequence_number(false).to_s(16)}, got: #{rframe.seq.ord.to_s(16)})"
+ rframe = nil #invalidate mismatch sequence frame for the last retry
+ end
+ else
+ errors.add :base, "No data received from device"
+ break
+ end
end
+ return rframe
end
def build_packet(cmd, data = "")
"".b.tap() do |packet|
packet << STX #Preamble. 1 byte long. Value: 01H.
@@ -257,12 +269,12 @@
data << ",#{item.percent}" unless item.percent.blank?
data << ",;#{'%.2f' % item.neto}" unless item.neto.blank?
end
end
- def sequence_number
+ def sequence_number(increment = true)
@seq ||= 0x1f
- @seq += 1
+ @seq += 1 if increment
@seq = 0x1f if @seq == 0x7f
@seq
end
class Frame
\ No newline at end of file