0

所以首先请耐心等待,这是我第一次使用这些。我能够编写一个简单的 python 代码,可以将信息发送到 arduino 并控制它的伺服:

def main(inp):
    import serial
    ser = serial.Serial("/dev/cu.usbmodemfa131")  # open arduino serial port
    ser.write(inp)      # write a string
    ser.close()             # close port

我能够制作一个带有数字滑块的 html/javascript 文件,我可以在我的计算机上使用 node.js 打开该文件。我的第一个想法可能是每次用户更改滑块上的数字时调用这个 python 脚本,因此让 arduino 的伺服相应地移动,但这在 javascript 中似乎很难做到。如何让网站上的用户输入写入 arduino 的串行端口?

这是 html 文件,滑块是使用 jquery 制作的。

<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Slider Demo</title>
  <link rel="stylesheet" href="jquery-ui.min.css">
  <style>#slider { margin: 50px; }  </style>
  <script src="external/jquery/jquery.js"></script>
  <script src="jquery-ui.min.js"></script>  
</head>
<body>

<div class="center" id="slider"></div>
<center>
<div id="slider-result">90</div>   
</center>
<input type="hidden" id="hidden"/>
<script>

$( "#slider" ).slider({
                animate: true,
                range: "min",
                value: 90,
                min: 0,
                max: 180,
                step: 1,
})

                //this gets a live reading of the value and prints it on the page
                slide: function( event, ui ) {
                    $( "#slider-result" ).html( ui.value );
                },

                //this updates the hidden form field so we can submit the data using a form
                change: function(event, ui) { 
                $('#hidden').attr('value', ui.value);
                }

                });
</script>

</body>
</html>
4

1 回答 1

0

From browser is difficult to access pc resources ( like access to printer ) , you can do a server that listen to requests sent from browser and make something based on request , in your case you can send a XmlHttpRequest POST to '/action ' with number input as parameter using jquery , and make a route in node server listening that action (app.post('/action',function()..) ) , then inside the function ,you get the parameters from the post request and call your python script ( or use node serial port to control arduino ) . This is a general idea but i hope could help you.

于 2015-11-12T04:54:16.963 回答