简体   繁体   English

Python和Arduino串行通信

[英]Python and arduino serial communication

I have a arduino Uno connected to my laptop through USB. 我有一个通过USB连接到笔记本电脑的arduino Uno。 I am running WAMP webserver on windows 7. I have python 2.7 and py serial installed. 我在Windows 7上运行WAMP Web服务器。我安装了python 2.7和py serial。 I wrote a HTML where the buttons when clicked will invoke the led1.py (python script). 我写了一个HTML,其中的按钮在单击时将调用led1.py(python脚本)。 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. python脚本将与arduino通信以放置LED,然后用户将按下另一个按钮以延迟LED。 The buttons when pressed are invoking the python script, the led is getting on, but then the HTML page is giving an error: 当按下按钮时,它们正在调用python脚本,led指示灯亮起,但是HTML页面却出现错误:

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. 请与服务器管理员admin @ localhost联系,并通知他们错误发生的时间以及您可能做的任何可能导致错误的事情。 More information about this error may be available in the server error log. 服务器错误日志中可能会提供有关此错误的更多信息。

Where am I going wrong? 我要去哪里错了? The HTML code is as follows: 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>

The Python code is as follows: Python代码如下:

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

and the Arduino code is very simple: Arduino代码非常简单:

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);
  }
}

Can you please tell me where am i going wrong?? 你能告诉我我要去哪里错吗? I am new to python. 我是python的新手。 Also I want to display data from arduino's sensors in the same HTML page using python . 我也想使用python在同一HTML页面中显示来自arduino传感器的数据。 How can that be possible. 那怎么可能。 Can I have a complete small program of both HTML and python for this. 我可以为此编写一个完整的HTML和python小型程序吗?

If the content of the python script is the content of cgi-bin/led.py , it has to look like this: 如果python脚本的内容是cgi-bin/led.py的内容,则它必须看起来像这样:

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

from http://wiki.python.org/moin/CgiScripts 来自http://wiki.python.org/moin/CgiScripts

You are missing the header in the python script. 您在python脚本中缺少标头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM