2

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:

  1. Start the J-Link program
  2. Get the initial output from the program (so I can parse it and determine if the script should continue)
  3. Issue commands to J-Link program
  4. Get any responses, parse them and use this in my script
  5. 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.

  • don't you just need to write to input? use `stdin=subprocess.PIPE` then write to the input to give it commands. – Tadhg McDonald-Jensen Dec 11 '19 at 22:40
  • @TadhgMcDonald-Jensen, yes I will need to do that, but first I want to capture the inital output 'SEGGER J-Link Commander V6.56b (Compiled Dec 6 2019 15:00:15)...'. At the moment it blocks before I can try and send something. – orsapihki wiioiv Dec 11 '19 at 23:23
  • `.stdout.read()` will block until the process is terminated, [are you looking for non blocking way to read the output](https://stackoverflow.com/questions/375427/non-blocking-read-on-a-subprocess-pipe-in-python) because that already has answers. – Tadhg McDonald-Jensen Dec 11 '19 at 23:34
  • @TadhgMcDonald-Jensen yes I guess I am, not sure I think of it that way. I have seen the post you refer to, but thought it was two complex. I have now tried jsf answer, simply replacing the command. Didn't block, but id did not get any output. I just want to access the stdout buffer before the process ends... – orsapihki wiioiv Dec 12 '19 at 21:43
  • hmmm,.... I seem to be getting similar results... I wonder if the output is being caught in some buffer before getting to your program and when the program ends it flushes that buffer. I'm not really sure how to help in this scenario sorry. – Tadhg McDonald-Jensen Dec 13 '19 at 02:47
  • @TadhgMcDonald-Jensen, that okay thanks for looking into my problem. I have found a dirty workaround, if I issue the 'qc' command before the read, then the J-Link software quits and I get the information I need. Not What I wanted, but keeps me moving – orsapihki wiioiv Dec 15 '19 at 22:49

0 Answers0