Skip to content

Commit 88d12c0

Browse files
committed
chore: move serial_port getter to nordic_boards
Signed-off-by: Maximilian Deubel <[email protected]>
1 parent 3989047 commit 88d12c0

File tree

4 files changed

+27
-36
lines changed

4 files changed

+27
-36
lines changed

src/nrfcloud_utils/claim_and_provision_device.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import sys
88
import time
99
import json
10-
import serial
1110
import argparse
1211
import platform
1312
from nrfcloud_utils import (
@@ -19,10 +18,9 @@
1918
create_device_credentials
2019
)
2120
from nrfcloud_utils.cli_helpers import error_style, local_style, send_style, hivis_style, init_colorama, cli_disable_styles, is_linux, is_windows, is_macos
22-
from serial.tools import list_ports
2321
from cryptography import x509
2422
from cryptography.hazmat.primitives import serialization
25-
from nrfcloud_utils.nordic_boards import ask_for_port
23+
from nrfcloud_utils.nordic_boards import ask_for_port, get_serial_port
2624

2725
full_encoding = 'mbcs' if is_windows else 'ascii'
2826
serial_timeout = 1
@@ -100,8 +98,7 @@ def parse_args(in_args):
10098
parser.add_argument("--prov-hex", type=str, help="Filepath to nRF Provisioning sample hex file",
10199
default="")
102100
parser.add_argument("--api-key", type=str,
103-
help="API key",
104-
default=None)
101+
help="API key", required=True)
105102
parser.add_argument("--stage", type=str, help="For internal (Nordic) use only", default="")
106103
parser.add_argument("--attest", type=str,
107104
help="Attestation token base64 string (AT%%ATTESTTOKEN result)",
@@ -326,13 +323,8 @@ def main(in_args):
326323
print(local_style('Selected serial port: {}'.format(port)))
327324

328325
# try to open the serial port
329-
try:
330-
ser = serial.Serial(port, args.baud, xonxoff= args.xonxoff, rtscts=(not args.rtscts_off),
331-
dsrdtr=args.dsrdtr, timeout=serial_timeout)
332-
ser.reset_input_buffer()
333-
ser.reset_output_buffer()
334-
except serial.serialutil.SerialException:
335-
error_exit(ser, 'Port could not be opened; not a device, or open already')
326+
ser = get_serial_port(port, args.baud, xonxoff= args.xonxoff, rtscts=(not args.rtscts_off),
327+
dsrdtr=args.dsrdtr)
336328

337329
# disable modem, just so the provisioning client doesn't try to do anything...
338330
print(local_style('\nDisabling modem...'))

src/nrfcloud_utils/device_credentials_installer.py

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,13 @@
99
import os
1010
import sys
1111
import csv
12-
import serial
1312
import getpass
1413
import semver
1514
from nrfcloud_utils import create_device_credentials, ca_certs, rtt_interface
1615
from nrfcloud_utils.cli_helpers import error_style, local_style, send_style, hivis_style, init_colorama, cli_disable_styles, write_file, save_devinfo_csv, save_onboarding_csv, is_linux, is_windows, is_macos, full_encoding
1716
from nrfcloud_utils.command_interface import ATCommandInterface, ATKeygenException, TLSCredShellInterface
18-
from nrfcloud_utils.nordic_boards import ask_for_port
17+
from nrfcloud_utils.nordic_boards import ask_for_port, get_serial_port
1918

20-
from serial.tools import list_ports
2119
from cryptography import x509
2220
from cryptography.hazmat.primitives import serialization
2321

@@ -420,15 +418,8 @@ def main(in_args):
420418
else 'generic device')))
421419

422420
# try to open the serial port
423-
try:
424-
ser = serial.Serial(port, args.baud, xonxoff= args.xonxoff, rtscts=(not args.rtscts_off),
425-
dsrdtr=args.dsrdtr, timeout=serial_timeout)
426-
ser.reset_input_buffer()
427-
ser.reset_output_buffer()
428-
except serial.serialutil.SerialException:
429-
sys.stderr.write(error_style('Port could not be opened; not a device, or open already\n'))
430-
sys.exit(5)
431-
421+
ser = get_serial_port(port, args.baud, xonxoff= args.xonxoff, rtscts=(not args.rtscts_off),
422+
dsrdtr=args.dsrdtr)
432423
# for gateways, get to the AT command prompt first
433424
if is_gateway:
434425
print(local_style('Getting to prompt...'))

src/nrfcloud_utils/gather_attestation_tokens.py

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@
88
import csv
99
import time
1010
import json
11-
import serial
1211
import argparse
1312
import platform
1413
from nrfcloud_utils import modem_credentials_parser, rtt_interface
1514
from nrfcloud_utils.cli_helpers import is_linux, is_windows, is_macos
16-
from nrfcloud_utils.nordic_boards import ask_for_port
17-
from serial.tools import list_ports
15+
from nrfcloud_utils.nordic_boards import ask_for_port, get_serial_port
1816
from datetime import datetime, timezone
1917
from colorama import init, Fore, Back, Style
2018

@@ -349,15 +347,8 @@ def main(in_args):
349347
print(local_style('Selected serial port: {}'.format(port)))
350348

351349
# try to open the serial port
352-
try:
353-
ser = serial.Serial(port, args.baud, xonxoff= args.xonxoff, rtscts=(not args.rtscts_off),
354-
dsrdtr=args.dsrdtr, timeout=serial_timeout)
355-
ser.reset_output_buffer()
356-
write_line('')
357-
time.sleep(0.2)
358-
ser.reset_input_buffer()
359-
except serial.serialutil.SerialException:
360-
error_exit('Port could not be opened; not a device, or open already')
350+
ser = get_serial_port(port, args.baud, xonxoff= args.xonxoff, rtscts=(not args.rtscts_off),
351+
dsrdtr=args.dsrdtr)
361352

362353
# get attestation token
363354
attest_tok = get_attestation_token()

src/nrfcloud_utils/nordic_boards.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
from serial.tools import list_ports
2+
import serial
23
from collections import defaultdict
34
import sys
5+
import time
6+
7+
serial_timeout = 1
48

59
# Some Nordic boards have their main serial port on the second interface.
610
# Serial number prefix, display name, main_port
@@ -87,3 +91,16 @@ def ask_for_port(list_all):
8791
continue
8892
name, serial, port = ports[index]
8993
return port.device
94+
95+
def get_serial_port(port, baud, xonxoff, rtscts, dsrdtr):
96+
try:
97+
ser = serial.Serial(port, baud, xonxoff= xonxoff, rtscts=rtscts,
98+
dsrdtr=dsrdtr, timeout=serial_timeout)
99+
ser.reset_output_buffer()
100+
ser.write(b'\r\n')
101+
time.sleep(0.2)
102+
ser.reset_input_buffer()
103+
except serial.serialutil.SerialException:
104+
sys.stderr.write('Port could not be opened; not a device, or open already.\n')
105+
sys.exit(1)
106+
return ser

0 commit comments

Comments
 (0)