通过atmega32中的spi将数据组件与实时输入分离到pc

时间:2015-05-06 11:05:57

标签: python csv bluetooth analysis

我有三个atmega32使用SPI协议向蓝牙模块发送三组传感器值(加速度计,陀螺仪,磁力计)。我收到了蓝牙数据,但它就是这样的,

0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,54.00

0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,51.00

0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,53.00

它是作为csv数据接收的,我无法将各个组件分开。我想将它们存储在单独的变量中(如x1,y1,z1,...)

这是我的代码:

# Author: P. Vinod Ranganath
# Description: Receiving data from ATmega32 via bluetooth
# Date: 27/04/2015


import bluetooth
import sys

#address of the bluetooth device
addr = "00:06:66:61:1E:76"
#port number
port = 1

#create socket and connect to it
socket = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
socket.connect((addr, port))




#if there is an incoming transmission, enter loop
while True:
    #receive transmitted data
    data = socket.recv(1024) 

    #print data
    sys.stdout.write(data)  

2 个答案:

答案 0 :(得分:0)

Python有一个csv模块,可以完成您通常想要的所有CSV解析。

在你的情况下,它更简单:python中的字符串有split(char)方法,所以

data.split(",") 

应该给你一个子串的数组:

["0.0", "0.0", ....

现在,您想拥有浮点数,对吧?

坚持你最喜欢的Python教程(真的,读一个!)并做

values = [float(substring) for substring in data.split(",")]

以便values是一个浮点数数组。

然后您可以执行类似

的操作
x1 = values[10]

但是你通常不想这样做 - 将这种数据作为数组通常更有用。

答案 1 :(得分:0)

AndroidFullScreen.leanMode(function(){alert("success");}, function(){alert("error");});

在堆栈溢出时发现了这一点。

它分离不同的元素,浮动转换没有错误。但是,列表没有按顺序填充(最后一个项目到第二个位置,依此类推,随机)