Hi I am trying to write a script that uses Segger's J-Link tool. When the J-Link tool is run it presents a prompt to the user, then commands can issued to it to perform tasks. Here is what I get if I run it from the command prompt:
C:\Program Files (x86)\SEGGER\JLink>jlink
SEGGER J-Link Commander V6.56b (Compiled Dec 6 2019 15:00:15)
DLL version V6.56b, compiled Dec 6 2019 14:58:46
Connecting to J-Link via USB...O.K.
Firmware: Energy Micro EFM32 compiled Apr 20 2016 12:17:23
Hardware version: V1.00
S/N: 440082037
License(s): RDI
VTref=3.297V
Type "connect" to establish a target connection, '?' for help
J-Link>
When I run the following code:
import subprocess
process = subprocess.Popen('C:\\Program Files (x86)\\SEGGER\JLink\\jlink.exe -nonsense', stdout=subprocess.PIPE, universal_newlines=True)
print(process.stdout.read())
I get the following output
SEGGER J-Link Commander V6.56b (Compiled Dec 6 2019 15:00:15)
DLL version V6.56b, compiled Dec 6 2019 14:58:46
Unknown command line option -nonsense.
But if I remove the -nonsense option the process hangs. This make sense as with a nonsense option J-Link exits with an error, in essence closing stdout.
What I want to do in my script is:
- Start the J-Link program
- Get the initial output from the program (so I can parse it and determine if the script should continue)
- Issue commands to J-Link program
- Get any responses, parse them and use this in my script
- When the script is finished issue qc command which causes J-Link to close.
What am I doing wrong with my subprocess calls that make the process hang, or what I am trying to do will never work and what else can I try?
Edit:
Using a modified version of the answer found here https://stackoverflow.com/a/36477512/9791536
from threading import Thread
def reader(f,buffer):
while True:
line=f.readline()
if line:
buffer.append(line)
else:
break
process = subprocess.Popen("C:\\Program Files (x86)\\SEGGER\\JLink\\jlink.exe -device EFM32GG380F1024 -if SWD -speed 14000 -autoconnect 1", stdout=subprocess.PIPE)
time.sleep(2)
linebuffer=[]
t=Thread(target=reader,args=(process.stdout,linebuffer))
t.daemon=True
t.start()
while True:
if linebuffer:
print(linebuffer.pop(0))
else:
print("nothing here")
time.sleep(1)
I get a stream of "Nothing here" messages. if I replace the command with simple ping that does not return, I get the expected ping results. Seems like Jlink is not playing ball....
process = subprocess.Popen("C:\\windows\system32\ping.exe 127.0.0.1 -t", stdout=subprocess.PIPE)
Edit 2:
In the past I have tried PyLink Module, but didn't work. So tried again now I have more experience with JLink.
Found I need to call set_tif(1) to set the interface to SWD from default of JTAG. So have thrown away subprocess above code.