Print system time when getting data from a local IP server

96 views Asked by At

I'm trying to debug embedded software using the method described here https://devzone.nordicsemi.com/question/83524/how-to-use-rtt-viewer-or-similar-on-gnulinux/

What the debugger does is setup a localhost server that provides all the printf like messages from the embedded device under test. Essentially I get the debug output when using the command telnet localhost 19021

What I would like is to print one, the system time and two, the time elapsed from the previous message along with the debug message received. This will help me know the exact time in debug messages in milliseconds (or even microseconds?). How can I easily do this? Thanks.

1

There are 1 answers

0
EarthLord On

With Python the data from a server can be received using socket. The following snippet does the job of receiving the data from the socket and print the time it was received.

# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Bind the socket to the port
server_address = ('localhost', 19021)

print "Connecting to the server @",server_address
sock.connect(server_address)

time.sleep(1)

past = 0
base = 0

while True:
    #read up to 1000 bytes from the socket (the max amount)
    rcv=sock.recv(1000)

    #we didn't get any data.
    if rcv=="":
        continue

    #Set base time as the time when first character is received
    if not base:
        base = time.time()

    current = time.time()
    elapsed = current - base
    delta = elapsed - past
    past = elapsed 

    current_str = datetime.datetime.now().strftime("%H:%M:%S.%f")
    print_msg = "[%s %2.6f] " % (current_str, delta)
    print (print_msg + rcv).strip()

For the specific case of the debugging the embedded application based on nRF5 SoCs like the link in the question, the complete script can be found here https://github.com/Appiko/nrf5x-firmware/blob/master/utils/rtt_logger.py