Skip to content

Fixed issue where metadata would come with garbage#50

Open
Tjoms99 wants to merge 7 commits intoIRNAS:masterfrom
Tjoms99:metadata_fix
Open

Fixed issue where metadata would come with garbage#50
Tjoms99 wants to merge 7 commits intoIRNAS:masterfrom
Tjoms99:metadata_fix

Conversation

@Tjoms99
Copy link
Copy Markdown

@Tjoms99 Tjoms99 commented Dec 17, 2024

Investigated the PR/issue discussed here #49.

Turns out not properly exiting a program running the PPK2 influences the next PPK2 session. Setting up the metadata the first time will throw an exception due to the garbage data (from the previous session) that comes a long with it. Physically turning the PPK2 on and off using the switch resolves this issue, but that’s inconvenient to do every time; So I’ve added a software fix here :)

To reproduce the issue and showcase the fix

  1. Connect the PPK2 via USB

  2. Open a terminal and run the example

python3 examples/example_measure_mw.py
  1. Terminate the program (2x)
ctrl + c
  1. Run the example again and you'll probably see this in the terminal
Attempt 1/2 - Failed to get valid PPK2 metadata: Could not retrieve valid metadata from the device.
Attempt 2/2 - Got metadata from PPK2
Initialized Power Profiler

This is where the UnicodeDecodeError would previously be triggered

…2 session were not terminated properly due to garbage data coming along with the metadata. We now expect them and try to get metadata one more time, but this time without garbage from previous session
…ld also just work after connecting to a PPK2 via USB
@Tjoms99 Tjoms99 mentioned this pull request Dec 17, 2024
@RmThrt
Copy link
Copy Markdown

RmThrt commented Dec 27, 2024

Thanks for the fix, it helped to figure out the way my test work! Good fix for me :)

@MrKevinWeiss
Copy link
Copy Markdown
Collaborator

Also tested and it works for me!

@MrKevinWeiss
Copy link
Copy Markdown
Collaborator

This is on 4.2.0 and 4.2.1 fw.

@mstixenberger
Copy link
Copy Markdown

works fine, thank you!

@bobatsar
Copy link
Copy Markdown
Contributor

bobatsar commented May 5, 2025

Works also on my side. @wlgrd is it possible to merge it into master?

@tphillips-ict
Copy link
Copy Markdown

+1 Can we merge this? Running into the same issue.

Is this project still actively maintained?

Copy link
Copy Markdown
Collaborator

@MrKevinWeiss MrKevinWeiss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few small things, but at least now we can move!

Comment thread src/ppk2_api/ppk2_api.py Outdated
Comment thread src/ppk2_api/ppk2_api.py Outdated
Tjoms99 added 5 commits April 14, 2026 15:22
…r treating the symptom instead of the root cause
…) messes up with higher level application loggers. The application using the ppk2 lib should decide what to do with the exceptions and thereby log what they need.
@Tjoms99
Copy link
Copy Markdown
Author

Tjoms99 commented Apr 14, 2026

Did another pass on this locally and verified it against a connected PPK2.

A couple of extra things came up while testing/integrating this in other code using the API:

  • get_modifiers() is now tightened up so it either succeeds or raises, instead of returning a mixed True / None
  • added return type hints in ppk2_api.py for the main API methods so the return contracts are a bit clearer
  • fixed the compatibility issue in src/power_profiler.py where get_samples() was still treated like a flat list instead of (samples, raw_digital)

I also removed the library-side logger usage that was ending up affecting my main application logger outside of ppk2. Hard failures now just raise instead of log-and-continue.

Verified by running:
python3 examples/example_measure_mw.py

This still works on my side with a connected PPK2 (/dev/ttyACM0) and streams measurements as expected. How does it look on your end?

@MrKevinWeiss
Copy link
Copy Markdown
Collaborator

Just got back from vacation, I will try to find some time either today or tomorrow for testing and reviewing!

@MrKevinWeiss
Copy link
Copy Markdown
Collaborator

oh 3 weeks old... I guess that slipped through the cracks 🫤

Copy link
Copy Markdown
Collaborator

@MrKevinWeiss MrKevinWeiss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested with python3.13 but needed a change...

manual testing results with "fresh" ppk
(.venv) weiss@lobaro-laptop-kw:~/repos/ppk2-api-python$ python examples/example_measure_mw.py
Press Enter to exit...
PPK2 devices found: [('/dev/ttyACM0', 'D06D19AF')]
[('/dev/ttyACM0', 'D06D19AF')]
Connecting to /dev/ttyACM0
Initialized Power Profiler
139.58 mW, Avg: 139.58 mW
27.09 mW, Avg: 83.34 mW
26.72 mW, Avg: 64.46 mW
26.66 mW, Avg: 55.01 mW
26.45 mW, Avg: 49.30 mW
26.29 mW, Avg: 45.47 mW
26.24 mW, Avg: 42.72 mW
26.17 mW, Avg: 40.65 mW
26.04 mW, Avg: 39.03 mW
26.02 mW, Avg: 37.73 mW
25.99 mW, Avg: 36.66 mW
25.95 mW, Avg: 35.77 mW
25.9 mW, Avg: 35.01 mW
25.83 mW, Avg: 34.35 mW
26.51 mW, Avg: 33.83 mW
26.94 mW, Avg: 33.40 mW
26.71 mW, Avg: 33.01 mW
27.01 mW, Avg: 32.67 mW
27.01 mW, Avg: 32.37 mW
26.99 mW, Avg: 32.11 mW
27.43 mW, Avg: 31.88 mW
27.78 mW, Avg: 31.70 mW
30.21 mW, Avg: 31.63 mW

28.85 mW, Avg: 31.52 mW
(.venv) weiss@lobaro-laptop-kw:~/repos/ppk2-api-python$ python examples/example_measure_mw.py
Press Enter to exit...
PPK2 devices found: [('/dev/ttyACM0', 'D06D19AF')]
[('/dev/ttyACM0', 'D06D19AF')]
Connecting to /dev/ttyACM0
Initialized Power Profiler
155.98 mW, Avg: 155.98 mW
26.94 mW, Avg: 91.46 mW
26.68 mW, Avg: 69.87 mW
26.61 mW, Avg: 59.05 mW
26.38 mW, Avg: 52.52 mW
26.22 mW, Avg: 48.13 mW
26.16 mW, Avg: 45.00 mW
26.09 mW, Avg: 42.63 mW
26.09 mW, Avg: 40.79 mW
25.98 mW, Avg: 39.31 mW
25.96 mW, Avg: 38.10 mW
25.92 mW, Avg: 37.08 mW
25.89 mW, Avg: 36.22 mW
25.84 mW, Avg: 35.48 mW
25.84 mW, Avg: 34.84 mW
25.82 mW, Avg: 34.27 mW
26.67 mW, Avg: 33.83 mW
26.96 mW, Avg: 33.45 mW
26.99 mW, Avg: 33.11 mW
27.0 mW, Avg: 32.80 mW
26.99 mW, Avg: 32.52 mW
27.01 mW, Avg: 32.27 mW
28.76 mW, Avg: 32.12 mW
27.48 mW, Avg: 31.93 mW
^C30.57 mW, Avg: 31.87 mW
Traceback (most recent call last):
  File "/home/weiss/repos/ppk2-api-python/examples/example_measure_mw.py", line 107, in <module>
    main()
    ~~~~^^
  File "/home/weiss/repos/ppk2-api-python/examples/example_measure_mw.py", line 102, in main
    input("Press Enter to exit...\n")
    ~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^
KeyboardInterrupt

(.venv) weiss@lobaro-laptop-kw:~/repos/ppk2-api-python$ python examples/example_measure_mw.py
Press Enter to exit...
PPK2 devices found: [('/dev/ttyACM0', 'D06D19AF')]
[('/dev/ttyACM0', 'D06D19AF')]
Connecting to /dev/ttyACM0
Initialized Power Profiler
131.61 mW, Avg: 131.61 mW
27.03 mW, Avg: 79.32 mW
26.74 mW, Avg: 61.79 mW
26.67 mW, Avg: 53.01 mW
26.45 mW, Avg: 47.70 mW
26.33 mW, Avg: 44.14 mW
26.21 mW, Avg: 41.58 mW
26.11 mW, Avg: 39.64 mW
26.07 mW, Avg: 38.14 mW
26.05 mW, Avg: 36.93 mW
26.0 mW, Avg: 35.93 mW
25.95 mW, Avg: 35.10 mW
25.9 mW, Avg: 34.39 mW
25.84 mW, Avg: 33.78 mW
26.46 mW, Avg: 33.29 mW
26.74 mW, Avg: 32.88 mW
26.8 mW, Avg: 32.53 mW
27.01 mW, Avg: 32.22 mW
26.99 mW, Avg: 31.95 mW
27.01 mW, Avg: 31.70 mW
27.43 mW, Avg: 31.50 mW
27.94 mW, Avg: 31.33 mW
29.77 mW, Avg: 31.27 mW

28.88 mW, Avg: 31.17 mW

import time
import threading

from src.ppk2_api.ppk2_api import PPK2_API
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
from src.ppk2_api.ppk2_api import PPK2_API
from ppk2_api.ppk2_api import PPK2_API

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works with python3.10 (which is the oldest still supported) and I am a big fan of the type hints.

@MrKevinWeiss
Copy link
Copy Markdown
Collaborator

If you add the small change and squash then I would be happy to merge.

@MrKevinWeiss
Copy link
Copy Markdown
Collaborator

Hmmm testing on a firmware updated ppk I get the first few readings as 0...

Details
$ python examples/example_measure_mw.py
Press Enter to exit...
PPK2 devices found: [('/dev/ttyACM0', 'D06D19AF')]
[('/dev/ttyACM0', 'D06D19AF')]
Connecting to /dev/ttyACM0
Initialized Power Profiler
-0.0 mW, Avg: 0.00 mW
-0.0 mW, Avg: 0.00 mW
-0.0 mW, Avg: 0.00 mW
-0.0 mW, Avg: 0.00 mW
-0.0 mW, Avg: 0.00 mW
-0.0 mW, Avg: 0.00 mW
-0.0 mW, Avg: 0.00 mW
-0.0 mW, Avg: 0.00 mW
-0.0 mW, Avg: 0.00 mW
-0.0 mW, Avg: 0.00 mW
-0.0 mW, Avg: 0.00 mW
25.57 mW, Avg: 2.13 mW
26.42 mW, Avg: 4.00 mW
26.38 mW, Avg: 5.60 mW
26.33 mW, Avg: 6.98 mW
26.35 mW, Avg: 8.19 mW
26.33 mW, Avg: 9.26 mW
26.31 mW, Avg: 10.21 mW
26.42 mW, Avg: 11.06 mW
27.45 mW, Avg: 11.88 mW
27.43 mW, Avg: 12.62 mW
27.43 mW, Avg: 13.29 mW
27.43 mW, Avg: 13.91 mW
27.42 mW, Avg: 14.47 mW
27.43 mW, Avg: 14.99 mW
27.4 mW, Avg: 15.47 mW
29.27 mW, Avg: 15.98 mW
31.07 mW, Avg: 16.52 mW
31.01 mW, Avg: 17.02 mW
30.99 mW, Avg: 17.48 mW
30.97 mW, Avg: 17.92 mW
30.96 mW, Avg: 18.32 mW
30.93 mW, Avg: 18.71 mW
30.95 mW, Avg: 19.07 mW
32.2 mW, Avg: 19.44 mW
32.28 mW, Avg: 19.80 mW
32.28 mW, Avg: 20.14 mW
32.26 mW, Avg: 20.45 mW
32.22 mW, Avg: 20.76 mW
32.25 mW, Avg: 21.04 mW
32.24 mW, Avg: 21.32 mW
30.86 mW, Avg: 21.54 mW
28.22 mW, Avg: 21.70 mW
28.21 mW, Avg: 21.85 mW
28.22 mW, Avg: 21.99 mW
28.22 mW, Avg: 22.12 mW
28.22 mW, Avg: 22.25 mW
28.21 mW, Avg: 22.38 mW
28.19 mW, Avg: 22.50 mW
25.5 mW, Avg: 22.56 mW

24.68 mW, Avg: 22.60 mW
(.venv) weiss@lobaro-laptop-kw:~/repos/ppk2-api-python$ python examples/example_measure_mw.py
Press Enter to exit...
PPK2 devices found: [('/dev/ttyACM0', 'D06D19AF')]
[('/dev/ttyACM0', 'D06D19AF')]
Connecting to /dev/ttyACM0
Initialized Power Profiler
-0.0 mW, Avg: 0.00 mW
-0.0 mW, Avg: 0.00 mW
-0.0 mW, Avg: 0.00 mW
-0.0 mW, Avg: 0.00 mW
-0.0 mW, Avg: 0.00 mW
-0.0 mW, Avg: 0.00 mW
-0.0 mW, Avg: 0.00 mW
-0.0 mW, Avg: 0.00 mW
-0.0 mW, Avg: 0.00 mW
-0.0 mW, Avg: 0.00 mW
-0.0 mW, Avg: 0.00 mW
25.2 mW, Avg: 2.10 mW
26.08 mW, Avg: 3.94 mW
26.05 mW, Avg: 5.52 mW
26.03 mW, Avg: 6.89 mW
26.03 mW, Avg: 8.09 mW
26.04 mW, Avg: 9.14 mW
26.03 mW, Avg: 10.08 mW
26.07 mW, Avg: 10.92 mW
27.22 mW, Avg: 11.74 mW
27.19 mW, Avg: 12.47 mW
27.2 mW, Avg: 13.14 mW
27.2 mW, Avg: 13.75 mW
27.19 mW, Avg: 14.31 mW
27.17 mW, Avg: 14.83 mW
27.19 mW, Avg: 15.30 mW
28.98 mW, Avg: 15.81 mW
30.83 mW, Avg: 16.35 mW
30.77 mW, Avg: 16.84 mW
30.74 mW, Avg: 17.31 mW
30.72 mW, Avg: 17.74 mW
30.69 mW, Avg: 18.14 mW
30.7 mW, Avg: 18.52 mW
30.66 mW, Avg: 18.88 mW
31.91 mW, Avg: 19.25 mW
32.03 mW, Avg: 19.61 mW
32.03 mW, Avg: 19.94 mW
32.01 mW, Avg: 20.26 mW
31.97 mW, Avg: 20.56 mW
32.0 mW, Avg: 20.85 mW
31.99 mW, Avg: 21.12 mW
30.57 mW, Avg: 21.34 mW
28.0 mW, Avg: 21.50 mW
27.95 mW, Avg: 21.65 mW
^CTraceback (most recent call last):
  File "/home/weiss/repos/ppk2-api-python/examples/example_measure_mw.py", line 107, in <module>
    main()
  File "/home/weiss/repos/ppk2-api-python/examples/example_measure_mw.py", line 102, in main
    input("Press Enter to exit...\n")
KeyboardInterrupt

(.venv) weiss@lobaro-laptop-kw:~/repos/ppk2-api-python$ python examples/example_measure_mw.py
Press Enter to exit...
PPK2 devices found: [('/dev/ttyACM0', 'D06D19AF')]
[('/dev/ttyACM0', 'D06D19AF')]
Connecting to /dev/ttyACM0
Initialized Power Profiler
-0.0 mW, Avg: 0.00 mW
-0.0 mW, Avg: 0.00 mW
-0.0 mW, Avg: 0.00 mW
-0.0 mW, Avg: 0.00 mW
-0.0 mW, Avg: 0.00 mW
-0.0 mW, Avg: 0.00 mW
-0.0 mW, Avg: 0.00 mW
-0.0 mW, Avg: 0.00 mW
-0.0 mW, Avg: 0.00 mW
-0.0 mW, Avg: 0.00 mW
-0.0 mW, Avg: 0.00 mW
25.59 mW, Avg: 2.13 mW
26.1 mW, Avg: 3.98 mW
26.1 mW, Avg: 5.56 mW
26.06 mW, Avg: 6.92 mW
26.04 mW, Avg: 8.12 mW
26.02 mW, Avg: 9.17 mW
25.98 mW, Avg: 10.10 mW
26.11 mW, Avg: 10.95 mW
27.19 mW, Avg: 11.76 mW
27.23 mW, Avg: 12.50 mW
27.19 mW, Avg: 13.16 mW
27.19 mW, Avg: 13.77 mW
27.19 mW, Avg: 14.33 mW
27.22 mW, Avg: 14.85 mW
27.22 mW, Avg: 15.32 mW
29.09 mW, Avg: 15.83 mW
30.82 mW, Avg: 16.37 mW
30.74 mW, Avg: 16.86 mW
30.76 mW, Avg: 17.33 mW
30.77 mW, Avg: 17.76 mW
30.74 mW, Avg: 18.17 mW

30.71 mW, Avg: 18.55 mW

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.

6 participants