Update __init__.py to handle short report lengths. - #18
Conversation
Added a check to readone that prevents a crash if the returned report is shorter than expected
can you try this version? diff --git a/src/radexreader/__init__.py b/src/radexreader/__init__.py
index 4238f6c238..6a42905cb2 100644
--- a/src/radexreader/__init__.py+++ b/src/radexreader/__init__.py@@ -278,30 +278,32 @@
self.hid_set_report((0x7b, 0xff, 0x20, 0, 0x06, 0, self.keyA, self.keyB, 0, 0, self.keyC, self.keyD, 0, 0x08, 0x0c, 0, 0xf3, 0xf7))
hexa = self.hid_get_report()
+ if hexa != b'' and len(hexa) >= 31:+ # measure = 0.15 µSv/h = 15 / 0.15 µSv accumulated = 15 / 15 CPM = 15+ measure = (hexa[20] + hexa[21] * 256 + hexa[22] * 256 * 256) / 100+ measure_acc = (hexa[24] + hexa[25] * 256 + hexa[26] * 256 * 256) / 100+ measure_cpm = hexa[28] + hexa[29] * 256 + hexa[30] * 256 * 256+ # uncertainty of the result+ percent = 15 + 6 / measure+ measure_min = measure * (1 - percent / 100)+ measure_max = measure * (1 + percent / 100)+ if measure_min < 0:+ measure_min = 0+ if percent > 99.9:+ percent = 99.9+ # most recent measure+ timestamp = int(time.time())+ return { timestamp: {+ 'pct': percent,+ 'min': measure_min,+ 'val': measure,+ 'max': measure_max,+ 'acc': measure_acc,+ 'cpm': measure_cpm,+ 'time': timestamp+ } }- # measure = 0.15 µSv/h = 15 / 0.15 µSv accumulated = 15 / 15 CPM = 15- measure = (hexa[20] + hexa[21] * 256 + hexa[22] * 256 * 256) / 100- measure_acc = (hexa[24] + hexa[25] * 256 + hexa[26] * 256 * 256) / 100- measure_cpm = hexa[28] + hexa[29] * 256 + hexa[30] * 256 * 256- # uncertainty of the result- percent = 15 + 6 / measure- measure_min = measure * (1 - percent / 100)- measure_max = measure * (1 + percent / 100)- if measure_min < 0:- measure_min = 0- if percent > 99.9:- percent = 99.9-- timestamp = int(time.time())- return { timestamp: {- 'pct': percent,- 'min': measure_min,- 'val': measure,- 'max': measure_max,- 'acc': measure_acc,- 'cpm': measure_cpm,- 'time': timestamp- } }+ return {}
def erase(self):
if self.com == 'RD1212v2':
it's similar between RD1212 and One, and without print |
It appears that applying that patch caused everything that's returned to be empty. When I reverted to the old code it started returning data again. I was testing why that was, but I didn't figure it out. |
luigifab
commented
Mar 23, 2025
loool, I suppose this line: |
Oh so nice! There is probably something wrong with the addresses used to read data from the Radex ONE.
But the address is not "simple": |
There was a problem hiding this comment.
Pull Request Overview
This PR adds a safeguard in readOne to prevent crashes when the HID report is shorter than expected.
- Introduces a length check for the received report and early return on incomplete data
- Emits a warning and returns an empty dict when the report is too short
Comments suppressed due to low confidence (1)
src/radexreader/init.py:282
- Add unit tests covering the new branch where
hexaisNoneor shorter than expected to validate this behavior and prevent regressions.
# Check if the returned report has the expected length
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
It look like that there are negative values with current code: with ChatGPT I tried some stupid things, and there is still something wrong, because his helper made never breaks: importcsv# the correct initial valueskeyA=0x04-0x04# => 0x00keyB=0x00keyC=0x5a+0x04# => 0x5ekeyD=0x00seen=set()
rows= []
iteration=1whileiteration<=10000:
t= (keyA, keyB, keyC, keyD)
iftinseen:
breakseen.add(t)
rows.append([iteration, f"0x{keyA:02x}", f"0x{keyB:02x}", f"0x{keyC:02x}", f"0x{keyD:02x}"])
iteration+=1keyA+=0x04keyC-=0x04ifkeyA>0xff:
keyA-=0xfekeyB+=0x01ifkeyB>0xff:
keyB=0x00keyC-=0x01elifkeyC<0x00:
keyC+=0xffkeyD-=0x01ifkeyD<0x00:
keyD=0xffwithopen("radex_keys_cycle.csv", "w", newline="") ascsvfile:
writer=csv.writer(csvfile, delimiter="\t")
writer.writerow(["iteration", "keyA", "keyB", "keyC", "keyD"])
writer.writerows(rows) |
It could be nice to know which address crash in your beautiful graph. I suppose that the beginning of the iteration is correct, we need to identify working addresses, and the first one that crash ( |

I added a check to readone that prevents a crash if the returned report is shorter than expected.
I encountered an issue with my code crashing due to some of the reports being shorter than expected, this stop-gap addition prevents the code from crashing out.