从本地IP服务器获取数据时打印系统时间

时间:2017-01-05 14:14:37

标签: localhost systemtime

我正在尝试使用此处描述的方法调试嵌入式软件https://devzone.nordicsemi.com/question/83524/how-to-use-rtt-viewer-or-similar-on-gnulinux/

调试器所做的是设置一个localhost服务器,它提供来自被测嵌入式设备的所有类似printf的消息。基本上,当使用命令telnet localhost 19021

时,我得到调试输出

我想要的是打印一个,系统时间和两个,从前一个消息开始经过的时间以及收到的调试消息。这将帮助我知道调试消息中的确切时间,以毫秒(甚至微秒?)为单位。我怎么能轻松做到这一点?感谢。

1 个答案:

答案 0 :(得分:0)

使用Python,可以使用socket接收来自服务器的数据。以下代码片段负责从套接字接收数据并打印接收时间。

# 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()

对于调试基于nRF5 SoC的嵌入式应用程序(如问题中的链接)的具体情况,可以在此处找到完整的脚本https://github.com/Appiko/nrf5x-firmware/blob/master/utils/rtt_logger.py