简体   繁体   English

在Python中重新排序列表以确保它以检查值开头

[英]Re-order list in Python to ensure it starts with check values

I'm reading in serial data using Pyserial, to populate a list of 17 values (1byte each) at a sampling rate of 256Hz. 我正在使用Pyserial读取串行数据,以256Hz的采样率填充17个值(每个1字节)的列表。

The bytes I ultimately want to use are the 5th to 8th in the list. 我最终想要使用的字节是列表中的第5到第8个字节。 Providing no bytes are dropped, the first two values of the stream are always the same ('165','90'). 如果不提供字节,则流的前两个值始终相同('165','90')。 I'm getting quite a few dropped values though, and my list values are shifting, so when I read the 5th-8th bytes, they aren't the correct values. 虽然我得到了很多丢弃的值,并且我的列表值正在移位,所以当我读取第5-8个字节时,它们不是正确的值。

I've partially combatted this by ensuring that before the wanted segement is captured, the first few values are checked against what they should be (ie if mylist[0]==165 &....). 我已经部分地通过确保在捕获想要的段落之前检查前几个值来确定它们应该是什么(即如果mylist [0] == 165&....)。
This is crude but ok since the chances of these two values appearing adjacent to each other in the list elsewhere is small. 这很粗糙但是没问题,因为这两个值在其他地方列表中彼此相邻的可能性很小。 The problem is that this means as soon as the bytes shift, I'm losing a load of values, until it eventually realigns. 问题是,这意味着一旦字节移位,我就会丢失一堆值,直到它最终重新排列。

My question is: what code can I use to either: 我的问题是:我可以使用哪些代码:

a) Force the list to realign once it has been detected that it no longer starts with 165,90. a)一旦检测到列表不再以165,90开始,强制列表重新排列。 (elif....). (ELIF ....)。

b) Detect where '165' & '90' are (next to each other) in the list and extract the values I want in relation to their position (next but one, onwards). b)检测列表中'165'和'90'(彼此相邻)的位置,并提取我想要的与其位置相关的值(下一个,一个,然后)。

Thanks in advance 提前致谢

S_S S_S

Just noticed from the related Qs that I could use 刚从相关Q中注意到我可以使用

mylist.append(mylist.pop(0)) 

multiple times until they are in the right place. 多次,直到他们在正确的地方。 Is there a better way that anyone can suggest? 有没有更好的方式,任何人都可以建议?

In case I understood you well, suppose you have a list like this: 如果我理解你,假设你有一个这样的列表:

l = [67, 126, 165, 90, 11, 1, 3, 5, 151, 99, 23]

you'd want to obtain: useful = [3,5,151,99] 你想得到:有用= [3,5,151,99]

Then, you could do: 然后,你可以这样做:

# obtain places where 165 is followed by 90
match = [x for x in xrange(len(l)-1) if l[x]==165 and l[x+1]==90]
# obtain range of useful values
useful = l[match[0]+4:match[0]+8]

You may have to tweak the numbers in case I misunderstood your problem. 如果我误解了你的问题,你可能需要调整数字。 Hope it helps, 希望能帮助到你,

Manuel 曼努埃尔

If I understand your problem correct, you have data continuously arriving, in 17-byte chunks. 如果我理解你的问题是正确的,你就会以17字节的数据块连续到达数据。 Assuming that's right, how about something like this: 假设是对的,这样的事情怎么样:

while True:    
    mylist.extend(get_more_data())

    # If we're misaligned, skip bytes until we're aligned again
    while mylist and mylist[0] != 165 or mylist[1] != 90:
        mylist.pop(0)

    # If we've skip bytes, we might not have enough data, so get more
    if len(mylist) < 17:
        continue

    # Process one chunk of data and remove it from the list
    process_data(mylist[:17])
    del mylist[:17]

I would use the 165 and 90 as header values, always checking the incoming byte for a match. 我会使用165和90作为标头值,始终检查传入的字节是否匹配。 This solution automatically re-syncs itself and it is as simple as: 该解决方案自动重新同步,它简单如下:

def get_message():
    while True: #Some timeout functionality is useful here ;-)
       message = []
       byte = xxx.get_byte()
       if byte == 165:
          byte = xxx.get_byte()
          if byte == 90:
             xxx.get_byte() #drop it
             xxx.get_byte() #drop it
             message[0] = xxx.get_byte()
             message[1] = xxx.get_byte()
             message[2] = xxx.get_byte()
             message[3] = xxx.get_byte()
             #Break out of loop. We have a message! 
             return message

If your designing both sides, you really should insert some kind of checksum. 如果你设计双方,你真的应该插入某种校验和。 If your just hacking something, look in the bytes for a checksum. 如果你只是在攻击某些内容,请查看字节以获取校验和。 It's probably there already. 它可能已经存在了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM