Skip to content

Update __init__.py to handle short report lengths. #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Tobbinloggin
Copy link

@Tobbinloggin Tobbinloggin commented Mar 21, 2025

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.

Added a check to readone that prevents a crash if the returned report is shorter than expected
@luigifab
Copy link
Owner

luigifab commented Mar 21, 2025

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

@Tobbinloggin
Copy link
Author

Tobbinloggin commented Mar 23, 2025

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
Copy link
Owner

loool, I suppose this line: if hexa != b'' and len(hexa) >= 31:
your line is: if not hexa or len(hexa) < 31:
so maybe: if hexa and len(hexa) >= 31:

@Tobbinloggin
Copy link
Author

Tobbinloggin commented Mar 23, 2025

Okay, I'll try that instead. Interesting piece of information, I set up InfluxDB and Grafana with RadexReader and looking at the data from a zoomed out view it appears these periods of missing data are cyclic.
image

@luigifab
Copy link
Owner

luigifab commented Mar 23, 2025

Oh so nice!

There is probably something wrong with the addresses used to read data from the Radex ONE.
There is a "for", the idea is:

  • current measure is stored at address 1
  • a moment later
  • current measure is stored at address 2
  • a moment later
  • current measure is stored at address 3
  • ...

But the address is not "simple": 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))

I'm going to run it again for a few days to check that the minutes add up for my Radex RD1212.

@luigifab
Copy link
Owner

Did you find something?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants