如何在Windows上将串行数据从Python脚本发送到Arduino-什么都没有 [英] How to send serial data from Python script to Arduino on Windows - Nothing Works

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

问题描述

我无法正确地通过串行从Python脚本向Arduino Uno发送数据.我正在使用9600波特,并且Arduino可以正确重置,但是它无法读取我从Python脚本发送的字符.我致电time.sleep()以确保Arduino上的重置不会干扰,并且我使用的是Windows7.我应该说一句,我的桌面正在运行python脚本,并通过USB连接到Arduino Uno的Serial来进行澄清.然后,我便有了RX&我的Uno的TX引脚(引脚0和1)连接到我的Mega的Serial1(引脚18和19).然后,我在笔记本电脑(使用Mega的常规Serial)上的Arduino IDE中使用Serial Monitor来窥视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的代码:

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通信时有效,而在使用其他串行程序时失败.您被auto-reset 功能所困扰.

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天全站免登陆