从 arduino 到浏览器扩展的通信全部在本地运行

发布于 2024-09-11 15:38:42 字数 142 浏览 3 评论 0原文

我试图弄清楚如何从 Arduino 获取串行信息,该 Arduino 控制我在计算机本地打开的浏览器中运行的 Javascript 浏览器扩展。看来我需要某种中间人来内部化串行读数并将它们传递到浏览器(以激活我编码的功能)。 Python?非常感谢任何答案、帮助和参考。

I am trying to figure out how I would go about taking serial information from an Arduino which controls a Javascript browser extension I have running in an open browser locally on a computer. It would seem that I would need some sort of middleman to internalize the serial readings and pass them to the browser (to activate the functions I have coded). Python? Any answers, help, and reference is greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

厌倦 2024-09-18 15:38:42

另一种选择是使用浏览器插件从 javascript 访问串行端口:http://code。 google.com/p/seriality/

Another option is to use a browser plug-in to access the serial port from javascript: http://code.google.com/p/seriality/

永言不败 2024-09-18 15:38:42

python 中的一个非常简单的 http 服务器在 do_Get 方法中看起来像这样,

from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer

class MyServer(BaseHTTPRequestHandler):
    def do_GET(self):
        self.send_response(200, 'OK')
        self.send_header('Content-type', 'text/html')
        self.end_headers()
        self.wfile.write( "hello" )

HTTPServer(('', 8888), MyServer).serve_forever()

您可以添加访问 arduino 程序所需的代码,

...
ser = serial.Serial('/dev/tty.usbserial', 9600)
ser.write('5')
ser.readline()
...

另一个选择是使用 webrick 作为网络服务器部分在 ruby​​ 中进行编码

require "serialport.so"
require 'webrick';

SERIALPORT="/dev/ttyUSB0"

s =  HTTPServer.new( :Port => 2000 )

class DemoServlet < HTTPServlet::AbstractServlet
    def getValue()
        begin
            sp = SerialPort.new( SERIALPORT, 9600, 8, 1, SerialPort::NONE)
            sp.read_timeout = 500
            sp.write( "... whatever you like to send to your arduino" )
            body = sp.readline()
            sp.close
            return body
        rescue
            puts "cant open serial port"
        end
    end

    def do_GET( req, res )

        body = "--.--"
        body = getValue()

        res.body = body
        res['Content-Type'] = "text/plain"
    end
end
s.mount( "/test", DemoServlet )
trap("INT"){ s.shutdown }
s.start

第三个选项是使用以太网 -屏蔽arduino并完全跳过代理代码

A very simple http server in python would look like this

from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer

class MyServer(BaseHTTPRequestHandler):
    def do_GET(self):
        self.send_response(200, 'OK')
        self.send_header('Content-type', 'text/html')
        self.end_headers()
        self.wfile.write( "hello" )

HTTPServer(('', 8888), MyServer).serve_forever()

in the do_Get method you can add the code necessary to access your arduino program

...
ser = serial.Serial('/dev/tty.usbserial', 9600)
ser.write('5')
ser.readline()
...

another option would be coding this in ruby by using webrick as the webserver part

require "serialport.so"
require 'webrick';

SERIALPORT="/dev/ttyUSB0"

s =  HTTPServer.new( :Port => 2000 )

class DemoServlet < HTTPServlet::AbstractServlet
    def getValue()
        begin
            sp = SerialPort.new( SERIALPORT, 9600, 8, 1, SerialPort::NONE)
            sp.read_timeout = 500
            sp.write( "... whatever you like to send to your arduino" )
            body = sp.readline()
            sp.close
            return body
        rescue
            puts "cant open serial port"
        end
    end

    def do_GET( req, res )

        body = "--.--"
        body = getValue()

        res.body = body
        res['Content-Type'] = "text/plain"
    end
end
s.mount( "/test", DemoServlet )
trap("INT"){ s.shutdown }
s.start

a third option would be using an ethernet-shield on the arduino and skipping the proxy code completely

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文