【问题标题】:Communication from arduino to a browser extension all run locally从 arduino 到浏览器扩展的通信都在本地运行
【发布时间】:2010-07-26 19:34:59
【问题描述】:

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

【问题讨论】:

    标签: python browser serial-port communication


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      python中一个非常简单的http服务器应该是这样的

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

      在 do_Get 方法中,您可以添加访问您的 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 上使用以太网屏蔽并完全跳过代理代码

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-05-12
        • 2011-03-03
        • 2012-02-17
        • 1970-01-01
        • 2013-11-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多