用于计算通过串行端口接收到的两个连续数据包之间的时间的 Python 脚本

发布于 2024-12-05 19:52:51 字数 144 浏览 0 评论 0原文

我正在调试我的代码,我需要编写一个 python 脚本,该脚本可以读取通过蓝牙通过串行端口发送的数据,并计算每个连续数据包之间经过的时间。我知道如何从串行端口读取数据,但我计算每个数据包之间的时间时遇到问题。

任何建议都会非常有帮助。

谢谢!

I am debugging my code for which I need to write a python script that can read data being sent over the serial port through bluetooth and calculate the time elapsed between each successive packet.I know how to read the data from the serial port but I am having issues with calculating the time between each packet.

Any suggestions would be very helpful.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

又怨 2024-12-12 19:52:51

像这样的东西可能会起作用。只需创建一个 IntTimer() 对象,并在收到数据包时对其调用 .stamp() 即可。这只是一个开始,所以如果它满足您的要求,那么您可能需要更改它以清理旧的时间戳等,否则 self.timestamps 只会不断增长。您可以使用 self.timestamps 来计算数据包之间的平均时间等。

import time

class IntTimer:
    def __init__(self):
        self.timestamps = []

    def stamp(self):
        if self.timestamps:
            last = self.timestamps[-1]
        else:
            last = False

        now = time.time()

        self.timestamps.append(now)

        if last:
            #return the time since the last packet
            return now - last  
        else:
            return -1          

这是一个非常简单的答案,所以如果您问一个更复杂的问题,请直接说出来。

Something like this might do the trick. Just create a IntTimer() object and call .stamp() on it whenever you receive a packet. It's just a start, so if it does what you want then you might need to change it to clean up the old stamps etc., otherwise self.timestamps will just grow and grow. You could use self.timestamps to work out average times etc. between packets.

import time

class IntTimer:
    def __init__(self):
        self.timestamps = []

    def stamp(self):
        if self.timestamps:
            last = self.timestamps[-1]
        else:
            last = False

        now = time.time()

        self.timestamps.append(now)

        if last:
            #return the time since the last packet
            return now - last  
        else:
            return -1          

This is quite a simple answer, so if you're asking a more complicated question then do say so.

撩人痒 2024-12-12 19:52:51

为什么不使用 python time 模块来计算时间差异?如果您需要更高的精度,您可以使用 select 系统调用来实现您自己的计时器。

但更好的解决方案是使用类似 Portmon

Why don't you use python time module to calculate time diff? If you need beter precision you can implement your own timer using the select system call.

But a better solution is to use something like Portmon

笔落惊风雨 2024-12-12 19:52:51

我会使用 time.time() 并减去来找到经过的秒数。

import time

# receive one packet
t0 = time.time()
# then receive the other packet
t1 = time.time()

print 'Time between packets (seconds):', t1 - t0

I would use time.time() and subtract to find the number of seconds elapsed.

import time

# receive one packet
t0 = time.time()
# then receive the other packet
t1 = time.time()

print 'Time between packets (seconds):', t1 - t0
顾挽 2024-12-12 19:52:51

这就是我写的并且有效。谢谢大家的回答。

#!/bin/python

import serial
import time


time_stamp_prev = 0

ser = serial.Serial(  \
    port="/dev/tty.bluetoothmodule", \
    baudrate=115200, \
    parity=serial.PARITY_NONE, \
    stopbits=serial.STOPBITS_ONE, \
    bytesize=serial.EIGHTBITS )

while True:
    if ser.inWaiting() > 0:


    print ser.readline();
            time_stamp_curr = time.time()
            time_between_packets = time_stamp_curr - time_stamp_prev
            time_stamp_prev = time_stamp_curr

So this is what I wrote and it worked.Thank you all for your answers.

#!/bin/python

import serial
import time


time_stamp_prev = 0

ser = serial.Serial(  \
    port="/dev/tty.bluetoothmodule", \
    baudrate=115200, \
    parity=serial.PARITY_NONE, \
    stopbits=serial.STOPBITS_ONE, \
    bytesize=serial.EIGHTBITS )

while True:
    if ser.inWaiting() > 0:


    print ser.readline();
            time_stamp_curr = time.time()
            time_between_packets = time_stamp_curr - time_stamp_prev
            time_stamp_prev = time_stamp_curr
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文