本文介绍了Arduino和PyBluez之间的蓝牙通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Python PyBluez在Arduino Uno板(带有蓝牙屏蔽)和Linux操作系统之间建立蓝牙通信.

I am trying to establish bluetooth communication between an Arduino Uno board (with a bluetooth shield) and my Linux OS, using Python PyBluez.

我已成功将笔记本电脑与Uno配对.我可以连接到板,但是板无法读取正在发送的数据,也无法发送数据.

I've successfully paired my laptop to the Uno. I'm able to connect to the board, however the board is not reading the data being sent nor is it able to send data.

这是Arduino Sketch

Here is the Arduino Sketch

#include <SoftwareSerial.h>

#define RxD 0    //receive data on digital 0
#define TxD 1 //transmit on digital 1

SoftwareSerial blueToothSerial(RxD, TxD);
int counter = 0;
int incoming;
void setup(void){
  Serial.begin(9600);
  //pinMode(RxD,INPUT);
  //pinMode(TxD,OUTPUT);
  setupBlueToothConnection();
}

void setupBlueToothConnection(){
  blueToothSerial.begin(19200);
  blueToothSerial.print("\r\n+STWMOD=0\r\n"); //set the bluetooth work in slave mode
  blueToothSerial.print("\r\n+STNA=SeeedBTSlave\r\n"); //set the bluetooth name as     "SeeedBTSlave"
  blueToothSerial.print("\r\n+STOAUT=1\r\n"); // Permit Paired device to connect me
  //blueToothSerial.print("\r\n+STAUTO=0\r\n"); // Auto-connection should be forbidden here
  delay(2000); // This delay is required.
  blueToothSerial.print("\r\n+INQ=1\r\n"); //make the slave bluetooth inquirable
  Serial.println("The slave bluetooth is inquirable!");
  delay(2000); // This delay is required.
  blueToothSerial.flush();
}

void loop(){
  if(blueToothSerial.available())
     Serial.println(blueToothSerial.read());
  blueToothSerial.write('x');
}

还有我的Python模块:

And my Python Module:

import bluetooth
import sys
bd_addr = "00:12:10:23:10:18" #itade address

port = 1
sock=bluetooth.BluetoothSocket( bluetooth.RFCOMM )
sock.connect((bd_addr, port))
print 'Connected'
sock.settimeout(1.0)
sock.send("x")
print 'Sent data'

data = sock.recv(1)
print 'received [%s]'%data

sock.close()

我有Arduino IDE 1.0.4,我的笔记本电脑正在运行Ubuntu 11.10

I have Arduino IDE 1.0.4, my laptop is running Ubuntu 11.10

推荐答案

您的这一行:

data = sock.recv(1)

可能没有收到足够的字节,因此请尝试由Tim发布的以下信息:仅从套接字接收一个字节

may be not receiving enough bytes so try this info posted by Tim:Only receiving one byte from socket

这篇关于Arduino和PyBluez之间的蓝牙通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 14:16