我尝试通过串行差异值读取,但我不知道如何将其分开,因为这两个值是数字,但来源不同
首先,我有一个PICAXE,通过光传感器的ADC将转换后的数据串行发送到python。其次,我让PICAXE通过串行方式将温度传感器的数据发送到python。
光码PICAXE
symbol puerto = B.5
main: readadc10 puerto,w1 ; read value into w1
sertxd(#w1,cr,lf)
goto main ; loop back to start临时代码PICAXE
symbol temp = B.4
readtemp temp, w0 ; read value into w1
debug
sertxd(#w0,cr,lf)
goto mainPython代码
import pygame
import sys, serial
from pygame.locals import *
ser = serial.Serial()
ser.port = 3
ser.baudrate = 4800
while True:
datos = ser.readline()
grados = float(datos)
print grados问题是picaxe同时发送来自light和temp的数据,但是当python接收数据时,我不知道如何识别每个数据。
有人能帮我吗??
谢谢!
发布于 2014-10-14 17:21:43
如果要同时发送温度读数和光电平读数,可以将它们放在一行中,中间用空格隔开。
PICAXE:
sertxd(#w0," ",#w1,cr,lf)Python:
readings = ser.readline()
[reading1, reading2] = readings.split()
temperature = float(reading1)
lightlevel = float(reading2)如果这两种类型的读数是不规则产生的,您可以在每个读数之前传输一个字符,以确定它是哪种类型。
PICAXE:
sertxd("T ",#w0,cr,lf)
...
sertxd("L ",#w1,cr,lf)Python:
reading = ser.readline()
[readingtype, readingvalue] = reading.split()
if readingtype == "T":
temperature = float(readingvalue)
elif readingtype == "L":
lightlevel = float(readingvalue)https://stackoverflow.com/questions/26305313
复制相似问题