Python与arduino串行通信

2024-07-05 14:21:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个arduino Uno通过USB连接到我的笔记本电脑上。我在Windows7上运行wampwebserver。我安装了python2.7和py serial。我编写了一个HTML,其中的按钮在单击时将调用led1.py(python脚本)。python脚本将与arduino通信以打开一个led,然后用户将按下另一个按钮来关闭led。 按下按钮时会调用python脚本,指示灯亮起,但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. More information about this error may be available in the server error log.

我哪里出错了?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>

Python代码如下:

^{pr2}$

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

你能告诉我我哪里出错了吗??我是python新手。 我还想用python在同一个HTML页面中显示来自arduino传感器的数据。怎么可能呢。我能有一个完整的HTML和python的小程序吗?


Tags: andthe代码py脚本inputledserver
1条回答
网友
1楼 · 发布于 2024-07-05 14:21:45

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

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

python脚本中缺少头。在

相关问题 更多 >