如何在 Windows 上将串行数据从 Python 脚本发送到 Arduino - 无济于事 [英] How to send serial data from Python script to Arduino on Windows - Nothing Works

查看:38
本文介绍了如何在 Windows 上将串行数据从 Python 脚本发送到 Arduino - 无济于事的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法正确地将数据从 Python 脚本串行发送到 Arduino Uno.我使用的是 9600 波特,Arduino 正确重置,但它没有读取我从 Python 脚本发送的字符.我调用 time.sleep() 以确保 Arduino 上的重置不会干扰,并且我使用的是 Windows 7.我应该澄清一下我的桌面正在运行 python 脚本并通过 USB 连接到我的 Arduino Uno 系列.然后我有 RX &我的 Uno 的 TX 引脚(引脚 0 和 1)连接到我的 Mega 的 Serial1(引脚 18 和 19).然后我在我的笔记本电脑上的 Arduino IDE 中使用串行监视器(它使用 Mega 的常规串行)来查看 Uno 所看到的内容.这是 Mega 的代码:

I can't correctly send data over serial from a Python script to an Arduino Uno. I'm using 9600 baud, and the Arduino correctly resets, but it does not read the char I'm sending from the Python script. I call time.sleep() to ensure the reset on the Arduino does not interfere, and I'm using Windows 7. I should clarify by saying that my desktop is running the python script and connected via USB to the Serial of my Arduino Uno. I then have the RX & TX pins (pins 0 & 1) of my Uno connected to my Mega's Serial1 (pins 18 & 19). I then use the Serial Monitor in the Arduino IDE on my laptop (which uses the Mega's regular Serial) to peek at what the Uno is seeing. Here is the code for the Mega:

void setup() {
  Serial1.begin(9600);
  Serial.begin(9600);
  Serial.println("Master Ready");
}

void loop() {
  if(Serial1.available() > 0) {
    char inByte = Serial1.read();
    Serial.write(inByte);
    Serial.write("\n");
  }
}

这是 Uno 的代码:

Here is the code for the Uno:

void setup() {
  Serial.begin(9600);
  Serial.println("Slave Ready");
}

void loop() {
 if(Serial.available() > 0) {
   char inByte = Serial.read();
   Serial.write(inByte);
   }
}

最后,这是python脚本:

And finally, here is the python script:

import sys
import serial
import time

ser = serial.Serial("COM23",9600)
n = int(sys.argv[1])
print n

time.sleep(10)
print ser
print n == 41
if (n == 70):
    ser.write(chr(97))
    print 'a'
elif n == 41:
    ser.write('ggggggg')
    print 'b'
elif n == 42:
    ser.write('hello world')
    print 'c'
elif n == 25:
    ser.write(chr(100))
elif n == 26:
    ser.write(chr(101))
elif n == 22:
    ser.write(chr(102))
elif n == 10:
    ser.write(chr(103))
elif n == 4:
    ser.write(chr(104))
elif n == 14:
    ser.write(chr(105))
elif n == 7:
    ser.write(chr(106))
elif n == 11:
    ser.write(chr(105))
elif n == 5:
    ser.write(chr(106))
elif n == 17:
    ser.write(chr(107))

# head - a - 70
# right bicep - b - 41
# right forearm - c - 42
# left bicep - d - 25
# left forearm - e - 26
# chest - f - 22
# right thigh - g - 10
# left thigh - h - 4
# right shin - i - 11 - 14
# left shin - j - 5 - 7
# waist - k - 17

如果有帮助,我基本上是在尝试将 Doom3 中的命中位置通过串行写入 Arduino,以便 Arduino 可以在您身体的适当位置打开电机.游戏代码是用 C++ 编写的,我第一次尝试使用 C++ 的串行库,但也没有用.

In case it helps, I'm essentially trying to write the hit locations in Doom3 to an Arduino over serial so that the Arduino can turn a motor on in the proper location on your body. The game code is in C++, and I first tried using a serial library for C++, but that didn't work either.

推荐答案

我刚刚遇到了类似的问题.这里的关键是它在从 IDE 与 arduino 通信时有效,而在使用其他串行程序时失败.您已经被自动重置 功能所困扰.

I just came across something similar. The key here is that it worked when communicating with the arduino from the IDE and failed when using some other serial program. You have been bit by the auto-reset feature.

查看Arduino Playground 上的此链接,详细了解正在发生的事情以及怎么办.该网站根据您的技能和舒适程度提供了多种解决方案.

Check out this link on the Arduino Playground for a detailed description of what is going on and what to do about it. The site gives several solutions depending on you skill and comfort level.

我希望您的 C++ 代码能够在您实施本站点的其中一种变通方法后起作用.

I expect your C++ code will work once you implement one of the work arounds from this site.

这篇关于如何在 Windows 上将串行数据从 Python 脚本发送到 Arduino - 无济于事的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆