I am trying to figure out how I would go about taking serial inform开发者_如何学运维ation 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.
Another option is to use a browser plug-in to access the serial port from javascript: http://code.google.com/p/seriality/
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
精彩评论