Python与arduino串口通信 [英] Python and arduino serial communication

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

问题描述

我有一个 arduino Uno 通过 USB 连接到我的笔记本电脑.我在 Windows 7 上运行 WAMP 网络服务器.我安装了 python 2.7 和 py serial.我写了一个 HTML,点击按钮时会调用 led1.py(python 脚本).python 脚本将与 arduino 通信以放置 LED,然后用户将按下另一个按钮以关闭 LED.按下时按钮正在调用 python 脚本,指示灯亮起,但随后 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:

内部服务器错误;
服务器遇到内部错误或配置错误,无法完成您的请求.请联系服务器管理员 admin@localhost 并告知他们发生错误的时间,以及您可能执行的任何可能导致错误的操作.服务器错误日志中可能提供有关此错误的更多信息.

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.

我哪里出错了?HTML代码如下:

Where am I going wrong? The HTML code is as follows:

    <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>

Python代码如下:

The Python code 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 代码非常简单:

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的新手.此外,我想使用 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天全站免登陆