Merge ~jocave/checkbox-support:nmea-test-script into checkbox-support:master

Proposed by Jonathan Cave
Status: Merged
Approved by: Jonathan Cave
Approved revision: ce7ad645e2155ef99d4bf4426f7a26d953e158ec
Merged at revision: 3b5a7544a0c66db88b45d34df6e93e2dad5c989a
Proposed branch: ~jocave/checkbox-support:nmea-test-script
Merge into: checkbox-support:master
Diff against target: 89 lines (+72/-0)
2 files modified
checkbox_support/scripts/nmea_test.py (+70/-0)
setup.py (+2/-0)
Reviewer Review Type Date Requested Status
Sylvain Pineau (community) Approve
Jonathan Cave (community) Needs Resubmitting
Review via email: mp+335460@code.launchpad.net

Description of the change

Add a script that can be used to test there is valid NMEA data being received on a serial port. The script allows configuration of serial port parameters.

The pynmea2 library is used to perform NMEA parsing. This is added as a python dependency in order to use a newer version of the library from pypi with checksum support.

To post a comment you must log in.
Revision history for this message
Sylvain Pineau (sylvain-pineau) wrote :

Unfortunately 'pynmea2 >= 1.8.0' in setup.py will break ppa builds as lp builders are not allowed to fetch dependencies from pypi.

Moreover if the archive version is too old I'd drop this requirement from setup.py and only add it to the checkbox-support part branch [1] under a new python-packages section.

python-packages:
  - pynmea2>=1.8.0

[1] https://git.launchpad.net/~checkbox-dev/checkbox/+git/checkbox-dev-parts/tree/snapcraft.yaml

review: Needs Fixing
Revision history for this message
Jonathan Cave (jocave) wrote :

Removed the dependency from the setup.py and squashed. Will follow up with remote part update.

review: Needs Resubmitting
Revision history for this message
Sylvain Pineau (sylvain-pineau) wrote :

+1

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/checkbox_support/scripts/nmea_test.py b/checkbox_support/scripts/nmea_test.py
2new file mode 100644
3index 0000000..1165721
4--- /dev/null
5+++ b/checkbox_support/scripts/nmea_test.py
6@@ -0,0 +1,70 @@
7+#!/usr/bin/env python3
8+# Copyright 2017 Canonical Ltd.
9+# All rights reserved.
10+#
11+# Written by:
12+# Jonathan Cave <jonathan.cave@canonical.com>
13+
14+import argparse
15+import os
16+from stat import S_ISCHR
17+
18+import pynmea2
19+import serial
20+
21+
22+def main():
23+ parser = argparse.ArgumentParser()
24+ parser.add_argument('device', help='Serial port device e.g. /dev/ttyS1')
25+ parser.add_argument('--baudrate', default=9600, type=int)
26+ parser.add_argument('--bytesize', choices=[5, 6, 7, 8], type=int,
27+ help='set bytesize, one of {5, 6, 7, 8}, default: 8',
28+ default=8)
29+ parser.add_argument('--parity', choices=['N', 'E', 'O', 'S', 'M'],
30+ type=lambda c: c.upper(),
31+ help='set parity, one of {N E O S M}, default: N',
32+ default='N')
33+ parser.add_argument('--stopbits', choices=[1, 2], type=int,
34+ help='set stopbits, one of {1, 2}, default: 1',
35+ default=1)
36+ args = parser.parse_args()
37+ print('Device name: {}'.format(args.device))
38+ mode = os.stat(args.device).st_mode
39+ if not S_ISCHR(mode):
40+ raise SystemExit('Expected a character device file')
41+ with serial.Serial(args.device,
42+ baudrate=args.baudrate,
43+ bytesize=args.bytesize,
44+ parity=args.parity,
45+ stopbits=args.stopbits,
46+ timeout=1) as f_dev:
47+ attempts = 30
48+ print('Attempting to parse {} sentences:'.format(attempts))
49+ bad_msg_count = 0
50+ for _ in range(attempts):
51+ try:
52+ str_from_serial = f_dev.readline().decode('UTF-8')
53+ except UnicodeDecodeError:
54+ print(' Bad sentence: couldn\'t decode line')
55+ bad_msg_count += 1
56+ continue
57+ try:
58+ msg = pynmea2.parse(str_from_serial, check=True)
59+ except pynmea2.ChecksumError:
60+ print(' Bad sentence: checksum error')
61+ bad_msg_count += 1
62+ continue
63+ except pynmea2.ParseError:
64+ print(' Bad sentence: parse error')
65+ bad_msg_count += 1
66+ continue
67+ print(' Got sentence type: {}'.format(msg.sentence_type))
68+ print('Total bad sentences: {}'.format(bad_msg_count))
69+ if bad_msg_count > 3:
70+ raise SystemExit('Too many bad NMEA sentences')
71+ else:
72+ print('Acceptable number of bad NMEA sentences')
73+
74+
75+if __name__ == '__main__':
76+ main()
77diff --git a/setup.py b/setup.py
78index 8300d78..88e247e 100755
79--- a/setup.py
80+++ b/setup.py
81@@ -86,6 +86,8 @@ setup(
82 "checkbox_support.scripts.usb_read_write:run_read_write_test"),
83 ("checkbox-support-snap_configuration="
84 "checkbox_support.scripts.snap_configuration:main"),
85+ ("checkbox-support-nmea_test="
86+ "checkbox_support.scripts.nmea_test:main"),
87 ],
88 },
89 )

Subscribers

People subscribed via source and target branches