首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >只响应一条消息与Spark神经科蓝牙伴侣的Pyserial通信

只响应一条消息与Spark神经科蓝牙伴侣的Pyserial通信
EN

Stack Overflow用户
提问于 2017-07-24 19:07:43
回答 1查看 722关注 0票数 0

我正在建造一个机器人使用Arduino Mega 2560和一些步进电机和磁编码器来记录它的运动。

我的环境是:

  • 阿迪诺·梅加2560
  • 来自Sparkfun的蓝牙伴侣
  • Python 3.6.1
  • 系列2.7
  • Windows 10

这是我试图在Python端使用的代码:

代码语言:javascript
复制
#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代码的相关部分:

代码语言:javascript
复制
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();
}

对于其他命令,它们要么向步进电机控制器发送命令,要么将数据从Mega发送到由计算机接收的蓝牙芯片。在那里,数据从蓝牙芯片传输到计算机是相当一致的,而数据从计算机到芯片是稀疏的,通常只有一个字母组成,如代码所示。

我遇到的问题是,如果我使用像Coolterm这样的串口连接软件连接到蓝牙芯片,一切都很好,我可以向Arduino发送多个命令来执行。然而,当我通过Python代码连接到蓝牙芯片时,只有第一条消息被Arduino接收和执行。我尝试过各种不同的解决方案,但至今还没有奏效。如有任何建议,将不胜感激。

EN

回答 1

Stack Overflow用户

发布于 2017-07-25 22:22:19

我遇到的问题与pyserial所使用的参数有关。我需要包括以下声明:

代码语言:javascript
复制
bluetoothconnection1.rtscts = 1
bluetoothconnection1.dsrdtr = 1

这些使得硬件流控制解决了我的问题。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45288245

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档