Skip to content

Commit 68ee004

Browse files
committed
Improve error logging when having the terminal used by other
application. The exception was not user friendly. Signed-off-by: Pascal Hernandez <pascal.hernandez@nordicsemi.no>
1 parent e3fcc23 commit 68ee004

2 files changed

Lines changed: 37 additions & 10 deletions

File tree

src/nrfcredstore/command_interface.py

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,20 @@
1414
import coloredlogs, logging
1515
import re
1616
from typing import List, Tuple, Optional
17+
import sys
18+
import serial
19+
20+
serial_busy_msg = (
21+
"Port is busy or disconnected. Please ensure no other program is using the serial port."
22+
)
23+
24+
timeout_msg = (
25+
"Unable to communicate with device. Please check that:\n"
26+
"- The correct serial port is selected\n"
27+
"- Your project has one of these two options enabled:\n"
28+
" * AT Host Library (CONFIG_AT_HOST_LIBRARY)\n"
29+
" * AT Shell (CONFIG_AT_SHELL)"
30+
)
1731

1832
logger = logging.getLogger(__name__)
1933

@@ -93,15 +107,23 @@ def set_shell_mode(self, shell: bool):
93107
self.shell = shell
94108

95109
def detect_shell_mode(self):
96-
"""Detect if the device is in shell mode or not."""
97-
for cmd, shell_mode in [("at AT+CGSN", True), ("AT+CGSN", False)]:
98-
for _ in range(3):
99-
self.write_raw(cmd)
100-
result, output = self.comms.expect_response("OK", "ERROR", "", suppress_errors=True, timeout=2)
101-
if result and len(re.findall("[0-9]{15}", output)) > 0:
102-
self.set_shell_mode(shell_mode)
103-
return
104-
raise TimeoutError("Failed to detect shell mode. Device does not respond to AT commands.")
110+
"""Detect if the device is in shell mode or not. """
111+
try:
112+
for cmd, shell_mode in [("at AT+CGSN", True), ("AT+CGSN", False)]:
113+
for _ in range(3):
114+
self.write_raw(cmd)
115+
result, output = self.comms.expect_response("OK", "ERROR", "", suppress_errors=True, timeout=2)
116+
if result and len(re.findall("[0-9]{15}", output)) > 0:
117+
self.set_shell_mode(shell_mode)
118+
return
119+
except serial.SerialException as e:
120+
# Port is busy or disconnected (likely due to multiple access)
121+
logger.error(serial_busy_msg)
122+
sys.exit(13) # ERR_SERIAL
123+
124+
# Timeout: no valid response received
125+
logger.error(timeout_msg)
126+
sys.exit(12) # ERR_TIMEOUT
105127

106128
def enable_error_codes(self):
107129
"""Enable error codes in the AT client"""

src/nrfcredstore/comms.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,12 @@ def expect_response(self, ok_str=None, error_str=None, store_str=None, timeout=1
323323
if error_str and error_str == line:
324324
return (False, output)
325325
if line.startswith('+CME ERROR'):
326-
code = int(line.replace('+CME ERROR: ', ''))
326+
# Parse error code if present, otherwise use -1 for unknown
327+
try:
328+
code_str = line.replace('+CME ERROR:', '').strip()
329+
code = int(code_str) if code_str else -1
330+
except ValueError:
331+
code = -1
327332
if not suppress_errors:
328333
logging.error(f'AT command error: {ERR_CODE_TO_MSG.get(code, "Unknown error")}')
329334
return (False, output)

0 commit comments

Comments
 (0)