2014-04-12 2 views
0

컴퓨터에서 Bluetooth를 사용하여 Arduino로 정보를 보내려고합니다. 저는 블루투스 용 Python을 pybluez 라이브러리와 함께 Windows에서 작업하고 있습니다.Bluetooth 및 Arduino 관련 문제

import bluetooth 

target_name = "HC-05" 
target_address = None 

nearby_devices = bluetooth.discover_devices() 
print nearby_devices 

for bdaddr in nearby_devices: 
    print bluetooth.lookup_name(bdaddr) 
     if target_name == bluetooth.lookup_name(bdaddr): 
      target_address = bdaddr 
      break 

if target_address is not None: 
    print "found target bluetooth device with address ", target_address 
    sock=bluetooth.BluetoothSocket(bluetooth.RFCOMM) 

    print "Trying connection" 
    port = 3 
    sock.connect((target_address, port)) 
    print "Trying sending" 
    sock.send("1 2 3") 
    print "Finished sending" 
    sock.close() 
else: 
    print "could not find target bluetooth device nearby" 

arduino 쪽에서 다음 코드를 실행하고 있습니다.

#include <stdlib.h> 

//format [motor #][pwmA = 0, pwmB = 0] 
const int MOTOR_PINS[5][2] = {{3,4}, {5,6}, {9, 10}, {23, 22}, {21, 20}}; 
const int SIGN = 1; 
String msg = ""; 

void setup() { 
    Serial.begin(9600); //usb for testing 
    Serial1.begin(9600); 
} 

void loop() { 
    //check for inputs to bluetooth adapter 
    while (Serial1.available()) { 
    char reading = Serial1.read(); 
    if ((int)reading == 13) { 
     Serial.println(msg); 
     parseString(msg); 
     msg = ""; 
    } 
    else { 
     msg += reading; 
    } 
    } 
} 

는 (필자는 파이썬 포트가 나중에 정리 계획, 하드 알고 있습니다.) 내가 파이썬 코드를 실행하면

가 어쨌든, 나는 성공적으로 블루투스 장치를 찾을 수 있습니다. 그러나 연결하려고 할 때 오류가 발생합니다. IOError: A socket operation failed because the destination host was down. PuTTY로 COM3에 세션을 열면 Arduino에 데이터를 보낼 수 있습니다 (parseString이 호출됩니다.) 두 메시지의 차이점을 이해하지 못합니다. .

답변

0

좋아, 코드를 시작해 주셔서 감사합니다. 조금 modding하여 원하는 것을 얻을 수 있습니다.

심지어 똑같습니다. IOError: A socket operation failed because the destination host was down. 그래서 처음에는 HC-05을 PC (창)에 연결했는데, HC-05는 암호가 잠겼습니다. 다음으로 나는 주어진 범위 사이의 각 포트를 검사 할 수있는 루프를 만드는 것으로 시작했다.

큰 영향은 없지만 HC에 대한 직렬 BAUD @ 115200을 향상시키는 것이 좋을지 만, 더 잘 작동 할 것입니다.

그래서 나를위한 파이썬 코드는 다음과 같습니다.

import bluetooth 

#--Pair HC-05 with PC first 

target_name = "BLUESAND" 
target_address = None 

nearby_devices = bluetooth.discover_devices() 
print nearby_devices 

for bdaddr in nearby_devices: 
    print bluetooth.lookup_name(bdaddr) 
    if target_name == bluetooth.lookup_name(bdaddr): 
     target_address = bdaddr 
     break 

if target_address is not None: 
    print "found target bluetooth device with address ", target_address 
    sock=bluetooth.BluetoothSocket(bluetooth.RFCOMM) 

    print "Trying connection" 
    ####################################### 
    i=0 # ---- your port range starts here 
    maxPort = 3 # ---- your port range ends here 
    err = True 
    while err == True and i <= maxPort: 
     print "Checking Port ",i 
     port = i 
     try: 
      sock.connect((target_address, port)) 
      err = False 
     except Exception,e: 
      ## print the exception if you like 
      i += 1 
    if i > maxPort: 
     print "Port detection Failed." 
     exit(0) 
    ####################################### 
    print "Trying sending" 
    sock.send("Challange") 
    print "Finished sending" 
    print sock.recv() 
    print "Finished receiving" 
    sock.close() 
else: 
    print "could not find target bluetooth device nearby" 

내 Arduino 코드는 다음과 같습니다.

String str; 

void setup() { 
    Serial.begin(115200);  // opens serial port, sets data rate 
} 

void loop() { 
    // send data only when you receive data: 
    if (Serial.available() > 0) { 
     str = Serial.readStringUntil('\n'); 

     // say what you got: 
     Serial.print("I received: "); 
     Serial.println("\""+str+"\""); 
     //trim the string to skip the trailing CRLF 
     str.trim(); 
    } 
} 
+0

다른 질문/토론에 속한 답변과 비슷합니다. OP가 아니지만 주제가 괜찮습니다. "이 질문을 발견하면 비슷한 문제에 대한 솔루션을 구현할 수있었습니다."라는 라인을 따라 대답에 추가 할 수 있습니까? 아니면 나는 그 공헌을 오해 했습니까? 조언을 주거나 편집하십시오. 감사. – Dilettant

+1

사실 나는 주된 질문에 대답하여 포트 번호를 바꾸면 괜찮을 것입니다. 그러나 언젠가 저와 같은 누군가가이 질문에 답을 얻었을 때, 상자 밖에서 바로 대답을 얻어야합니다. 그게 내 해결책을 설명하는 뒤에 내 생각 이었어. +는 자신의 작업에 대한 원저자에게 감사해야했습니다. – S4nd33p