Python和Arduino的串行通信 [英] Python and arduino serial communication

查看:1914
本文介绍了Python和Arduino的串行通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经通过USB连接到我的笔记本电脑Arduino的乌诺。我运行在Windows 7 WAMP的Web服务器我有蟒蛇2.7和PY系列安装。我写的,其中的按钮,点击后调用led1.py(python脚本)一个HTML。 Python脚本将与Arduino的沟通,把一个领导,然后用户将preSS另一个按钮推杆关闭LED。
当pressed调用的python脚本的按钮,指示灯过得好,但随后的HTML页面被给了一个错误
内部服务器错误;
服务器遇到一个内部错误或配置错误,无法完成您的要求联系服务器管理员,管理员@本地,并通知他们的错误发生的时间,以及任何你可能已经做了可能造成的error.More信息这个错误可能是在服务器错误日志。结果
我在哪里去了?

在codeS是
code为HTML

I have a arduino Uno connected to my laptop through USB. I am running WAMP webserver on windows 7. I have python 2.7 and py serial installed. I wrote a HTML where the buttons when clicked will invoke the led1.py (python script). The python script would communicate with the arduino to put on a led and then the user would press another button to putt off the Led. The buttons when pressed are invoking the python script , the led is getting on , but then the HTML page is giving an error Internal Server Error; The server encountered an internal error or misconfiguration and was unable to complete your request.Please contact the server administrator, admin@localhost and inform them of the time the error occurred, and anything you might have done that may have caused the error.More information about this error may be available in the server error log.
Where am I going wrong?
The codes are Code for HTML

    <html>
    <head>
        <title>Sample Web Form</title>
    </head>
<body>

<h1>Fill Out This Form</h1>

<form action="/cgi-bin/led.py" method="POST">
    <input type="submit" name='action' value="LEFT">
    <input type="submit" style="background-color:yellow" name='action' value="LEFT"/><br><br><br>
    <input type="submit" style="background-color:yellow" name='action' value="BACK"/> 

</form>

</body>
</html>

蟒code是如下:

The code of python is as follows

#!python
import serial
import time
keyword =form.getvalue('action')
arduino = serial.Serial('COM4', baudrate=115200, bytesize=8, parity='N', stopbits=1, timeout=1)
arduino.open()
arduino.isOpen()
time.sleep(5) # waiting the initialization...
print("initialising")
while True:
    if keyword == 'LEFT':
       arduino.write("H\n") # turns LED ON
       break
    elif keyword == 'BACK':
       arduino.write('L\n') # turns LED OFF
       break
    elif break
arduino.close() #say goodbye to Arduino

和Arduino的code是非常简单的。

and the arduino code is very simple

int redpin =13;
int incomingbyte;

void setup()
{
  Serial.begin(115200);
  pinMode(redpin,OUTPUT);
  pinMode(greenpin,OUTPUT);
  pinMode(fanpin,OUTPUT);
  }

void loop()
{
  if(Serial.available()>0)
  {
    incomingbyte=Serial.read();
  }
  if(incomingbyte == 'H')
  {
    digitalWrite(redpin,HIGH);
  }
  if(incomingbyte == 'L')
  {
    digitalWrite(redpin,LOW);
  }
}

能否请你告诉我,我要去哪里错了?我是新来的蟒蛇。
此外,我想使用Python在同一个HTML页面来显示的Arduino的传感器的数据。这怎么可能。我可以同时拥有HTML和Python这种完整的小程序。

推荐答案

如果Python脚本的内容的cgi-bin / led.py的含量,它已经看起来像这样:

If the content of the python script is the content of cgi-bin/led.py, it has to look like this:

   7 print "Content-type: text/html"
   8 print
   9 
  10 print """
  11 <html>
  12 
  13 <head><title>Sample CGI Script</title></head>
  14 
  15 <body>
  16 
  17   <h3> Sample CGI Script </h3>
  18 """

http://wiki.python.org/moin/CgiScripts

您缺少的python脚本头。

You are missing the header in the python script.

这篇关于Python和Arduino的串行通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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