Python代码从串口连续接收可变数据

时间:2013-06-01 07:38:01

标签: python python-2.7 pyserial

我的PC上安装了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)     

使用此代码我可以将数据传输到另一台PC,我通过在另一台PC上打开HyperTerminal进行交叉检查,我可以看到传输的数据(hello world)。

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

import serial
ser=serial.serial("COM10", 9600)  
while 1:
if ser.inwaiting():
val = ser.readline(ser.inwaiting())
 print(val)  

如果我从HyperTerminal发送数据(你好吗),我可以使用上面的代码在我的电脑上接收数据。

直到这一切都很好。

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

1 个答案:

答案 0 :(得分:3)

通常,您与micro进行通信的方法是将单个字符用于轻量级或创建通信协议。基本上你有一个开始标志,结束标志和某种校验和,以确保数据正确通过。有很多方法可以做到这一点。

以下代码适用于Python 3.您可能必须对字节数据进行更改。

# 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

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

相关问题