0
我正在建造一個使用Arduino Mega 2560和一些步進電機和磁編碼器來記錄其運動的機器人。與Sparkfun的Pyserial通信藍牙伴侶只響應一條消息
我的環境是:
- Arduino的兆豐2560
- 從Sparkfun藍牙伴侶
- 的Python 3.6.1
- Pyserial 2.7
- 的Windows 10
這是代碼我試圖在Python端使用:
#This code is designed to test the communication between the python based code
#and a bluetooth mate connected to an Arduino Mega
# Author: Tynan Stack
# Date: July 24 2017
# Python 3.6.1 Pyserial 2.7
import serial
from time import sleep
ser = serial.Serial()
ser.baudrate = 115200
ser.port = 'COM7'
ser
ser.open()
sleep(8)
print("sending data")
ser.write(b'u\r\n')
print("data sent")
sleep(2)
ser.write(b"u\r\n")
sleep(8)
ser.close()
下面是處理通信Arduino的代碼的相關部分:
void loop() {
// put your main code here, to run repeatedly:
if(Serial3.available()){
Serial.println("Its avalible");
timer = micros();
data = (char)Serial3.read();
Serial.print(data);
if(data == 'u'){//this corresponds to the linear stepper motor moving the stage up
Linear.SpinTheMotor(LOW, 1.8,720);
}
if(data == 'd'){ //this corresponds to the linear stepper motor moving the stage down
Linear.SpinTheMotor(HIGH, 1.8,720);
}
if (data == 'a'){
Angular1.SpinTheMotor(LOW, 0.018, 15);
Delays(micros(),1000);
Angular1.SpinTheMotor(HIGH, 0.018, 15);
}
if(data == 'b'){
Angular2.SpinTheMotor(LOW,0.018,15);
Delays(micros(),1000);
Angular2.SpinTheMotor(HIGH,0.018,15);
}
}
if(digitalRead(BUTTONPIN)==LOW){
BluetoothCycle();
}
bluetoothPrint();
}
對於其他命令它們無論是從米加將命令發送到步進電機控制器或將數據發送到藍牙芯片被計算機接收。數據從藍牙芯片傳輸到計算機的過程相當一致,而從計算機到芯片的數據通常是稀疏的,只能由代碼中可見的單個字母組成。
我遇到的問題是如果我使用像Coolterm這樣的串口連接軟件連接到藍牙芯片,一切正常,我可以發送多個命令到Arduino執行。但是,當我通過Python代碼連接到藍牙芯片時,只有第一條消息被Arduino接收並採取行動。我已經嘗試過各種不同的解決方案,但尚未奏效。任何建議將不勝感激。