从串行端口连续接收变量数据的Python代码

从串行端口连续接收变量数据的Python代码,python,python-2.7,pyserial,Python,Python 2.7,Pyserial,我的电脑上安装了Python 2.7.4和pyserial-2.5 win32。在这里,我使用微控制器设备作为主设备(主设备),而我的电脑作为从设备(辅助设备)。这里每次单片机都会传输数据,而我的PC机都要通过串口接收数据。我想要一个Python代码来接收连续数据。在这里,传输的数据大小将一直变化。这里我写了一个代码来传输数据,代码是 import serial ser= serial.serial("COM10", 9600) ser.write("Hello world\n") x = se

我的电脑上安装了Python 2.7.4和pyserial-2.5 win32。在这里,我使用微控制器设备作为主设备(主设备),而我的电脑作为从设备(辅助设备)。这里每次单片机都会传输数据,而我的PC机都要通过串口接收数据。我想要一个Python代码来接收连续数据。在这里,传输的数据大小将一直变化。这里我写了一个代码来传输数据,代码是

import serial
ser= serial.serial("COM10", 9600)
ser.write("Hello world\n")
x = ser.readline()
print(x)     
有了这段代码,我可以将数据传输到另一台电脑,通过在另一台电脑上打开超级终端进行交叉检查,我可以看到传输的数据(hello world)

我还编写了接收数据的代码:

import serial
ser=serial.serial("COM10", 9600)  
while 1:
if ser.inwaiting():
val = ser.readline(ser.inwaiting())
 print(val)  
如果我从超级终端发送数据(您好),我可以在我的电脑中接收数据,并使用上述代码

在这之前,一切都很好


我现在的问题是,当微控制器在可变时间段传输可变数据时,我需要用Python在PC中接收这些数据。我是否需要使用缓冲区来存储接收到的数据?如果是,代码是什么?为什么以及如何在Python中使用缓冲区?根据我在互联网上的搜索,缓冲区是用来分割字符串的。

通常,与micro通信的方法是使用单个字符作为轻量级的内容或创建通信协议。基本上,您有一个开始标志、结束标志和某种校验和,以确保数据正确传递。有很多方法可以做到这一点

下面的代码是针对Python3的。您可能必须更改字节数据

# On micro
data = b"[Hello,1234]"
serial.write(data)
在你要运行的计算机上

def read_data(ser, buf=b'', callback=None):
    if callback is None:
        callback = print

    # Read enough data for a message
    buf += ser.read(ser.inwaiting()) # If you are using threading +10 or something so the thread has to wait for more data, this makes the thread sleep and allows the main thread to run.
    while b"[" not in buf or b"]" not in buf:
        buf += ser.read(ser.inwaiting())

    # There may be multiple messages received
    while b"[" in buf and b']' in buf:
        # Find the message
        start = buf.find(b'[')
        buf = buf[start+1:]
        end = buf.find(b']')
        msg_parts = buf[:end].split(",") # buf now has b"Hello, 1234"
        buf = buf[end+1:]

        # Check the checksum to make sure the data is valid
        if msg_parts[-1] == b"1234": # There are many different ways to make a good checksum
            callback(msg_parts[:-1])

   return buf

running = True
ser = serial.serial("COM10", 9600)
buf = b''
while running:
    buf = read_data(ser, buf)
如果您使用的是GUI,则线程非常有用。然后,当GUI显示数据时,您可以让线程在后台读取数据

import time
import threading

running = threading.Event()
running.set()
def thread_read(ser, callback=None):
    buf = b''
    while running.is_set():
        buf = read_data(ser, buf, callback)

def msg_parsed(msg_parts):
    # Do something with the parsed data
    print(msg_parsed)

ser = serial.serial("COM10", 9600)
th = threading.Thread(target=thread_read, args=(ser, msg_parsed))
th.start()


# Do other stuff while the thread is running in the background
start = time.clock()
duration = 5 # Run for 5 seconds
while running.is_set():
    time.sleep(1) # Do other processing instead of sleep
    if time.clock() - start > duration
        running.clear()

th.join() # Wait for the thread to finish up and exit
ser.close() # Close the serial port

请注意,在线程示例中,我使用了一个回调函数,该函数作为变量传递,稍后调用。另一种方法是将数据放入队列,然后在代码的不同部分处理队列中的数据。

我不太理解你的问题,你想只存储数据而不是打印数据吗?在这种情况下,创建一个新列表(
data=[]
)并将接收到的内容附加到循环中(
data.append(val)
)在打印之前是否需要将传输的数据存储在缓冲区中?您所说的“缓冲区”是什么意思?列表是存储所需数据的有效结构。