def serial_handler(connect): if connect == '1': # print 'js is asking to connect serial' if not SerialManager.is_connected(): try: global SERIAL_PORT, BITSPERSECOND, GUESS_PREFIX if not SERIAL_PORT: SERIAL_PORT = SerialManager.match_device(GUESS_PREFIX, BITSPERSECOND) SerialManager.connect(SERIAL_PORT, BITSPERSECOND) ret = "Serial connected to %s:%d." % (SERIAL_PORT, BITSPERSECOND) + '<br>' time.sleep(1.0) # allow some time to receive a prompt/welcome SerialManager.flush_input() SerialManager.flush_output() return ret except serial.SerialException: SERIAL_PORT = None print "Failed to connect to serial." return "" elif connect == '0': # print 'js is asking to close serial' if SerialManager.is_connected(): if SerialManager.close(): return "1" else: return "" elif connect == "2": # print 'js is asking if serial connected' if SerialManager.is_connected(): return "1" else: return "" else: print 'ambigious connect request from js: ' + connect return ""
def serial_handler(connect): if connect == '1': # print 'js is asking to connect serial' if not SerialManager.is_connected(): try: global SERIAL_PORT, BITSPERSECOND, GUESS_PREFIX if not SERIAL_PORT: SERIAL_PORT = SerialManager.match_device( GUESS_PREFIX, BITSPERSECOND) SerialManager.connect(SERIAL_PORT, BITSPERSECOND) ret = "Serial connected to %s:%d." % (SERIAL_PORT, BITSPERSECOND) + '<br>' time.sleep(1.0) # allow some time to receive a prompt/welcome SerialManager.flush_input() SerialManager.flush_output() return ret except serial.SerialException: SERIAL_PORT = None print "Failed to connect to serial." return "" elif connect == '0': # print 'js is asking to close serial' if SerialManager.is_connected(): if SerialManager.close(): return "1" else: return "" elif connect == "2": # print 'js is asking if serial connected' if SerialManager.is_connected(): return "1" else: return "" else: print 'ambigious connect request from js: ' + connect return ""
def run_with_callback(host, port, rfidreader, logger, powertimer): """ Start a wsgiref server instance with control over the main loop. This is a function that I derived from the bottle.py run() """ debug(True) handler = default_app() handler.catchall = False handler.rfidreader = rfidreader handler.logger = logger handler.powertimer = powertimer server = make_server(host, port, handler, handler_class=HackedWSGIRequestHandler) server.timeout = 0.01 #server.quiet = True print "Persistent storage root is: " + storage_dir() print "-----------------------------------------------------------------------------" print "Bottle server starting up ..." print "Serial is set to %d bps" % BITSPERSECOND print "Point your browser to: " print "http://%s:%d/ (local)" % ('127.0.0.1', port) # if host == '': # try: # print "http://%s:%d/ (public)" % (socket.gethostbyname(socket.gethostname()), port) # except socket.gaierror: # # print "http://beaglebone.local:4444/ (public)" # pass print "Use Ctrl-C to quit." print "-----------------------------------------------------------------------------" print # auto-connect on startup global SERIAL_PORT if not SERIAL_PORT: SERIAL_PORT = SerialManager.match_device(GUESS_PREFIX, BITSPERSECOND) SerialManager.connect(SERIAL_PORT, BITSPERSECOND) # open web-browser try: webbrowser.open_new_tab('http://127.0.0.1:'+str(port)) pass except webbrowser.Error: print "Cannot open Webbrowser, please do so manually." sys.stdout.flush() # make sure everything gets flushed server.timeout = 0 while 1: try: SerialManager.send_queue_as_ready() server.handle_request() time.sleep(0.0004) except KeyboardInterrupt: break print "\nShutting down..." SerialManager.close()
def run_with_callback(host, port): """ Start a wsgiref server instance with control over the main loop. This is a function that I derived from the bottle.py run() """ handler = default_app() server = make_server(host, port, handler, handler_class=HackedWSGIRequestHandler) server.timeout = 0.01 server.quiet = True print "Persistent storage root is: " + storage_dir() print "-----------------------------------------------------------------------------" print "Bottle server starting up ..." print "Serial is set to %d bps" % BITSPERSECOND print "Point your browser to: " print "http://%s:%d/ (local)" % ('127.0.0.1', port) # if host == '': # try: # print "http://%s:%d/ (public)" % (socket.gethostbyname(socket.gethostname()), port) # except socket.gaierror: # # print "http://beaglebone.local:4444/ (public)" # pass print "Use Ctrl-C to quit." print "-----------------------------------------------------------------------------" print # auto-connect on startup global SERIAL_PORT if not SERIAL_PORT: SERIAL_PORT = SerialManager.match_device(GUESS_PREFIX, BITSPERSECOND) SerialManager.connect(SERIAL_PORT, BITSPERSECOND) # open web-browser try: webbrowser.open_new_tab('http://127.0.0.1:' + str(port)) pass except webbrowser.Error: print "Cannot open Webbrowser, please do so manually." sys.stdout.flush() # make sure everything gets flushed server.timeout = 0 while 1: try: SerialManager.send_queue_as_ready() server.handle_request() time.sleep(0.0004) except KeyboardInterrupt: break print "\nShutting down..." SerialManager.close()
def stopBalancing(stackNum,cellNum): balanceCommand='S'+str(stackNum)+str(cellNum) SerialManager.read_existing() #clear rx_buffer if SerialManager.write( balanceCommand +'\r'): balanceStopTime=SerialManager.read_to('\r') print 'balance stop time is:',balanceStopTime if __name__ == '__main__': #if not SERIAL_PORT: # SERIAL_PORT = SerialManager.match_device(GUESS_PREFIX, BITSPERSECOND) commandGenerator(); flag=[] voltage=[] temperature=[] relayStatus=0 SerialManager.connect(SERIAL_PORT, BITSPERSECOND) #while SerialManager.is_connected(): SerialManager.read_existing() #clear rx_buffer if SerialManager.write('C000'+'\r'): relayStatus=SerialManager.read_to('\r') print 'relay status is:',relayStatus SerialManager.read_existing() #clear rx_buffer if SerialManager.write('R000'+'\r'): totalVoltage=SerialManager.read_to('\r') print 'total voltage is:',totalVoltage SerialManager.read_existing() #clear rx_buffer if SerialManager.write('I000'+'\r'): totalCurrent=SerialManager.read_to('\r')
# Define command line argument interface parser = argparse.ArgumentParser( description= 'Stream g-code file to grbl. (pySerial and argparse libraries required)') parser.add_argument('gcode_file', type=argparse.FileType('r'), help='g-code filename to be streamed') parser.add_argument('device_file', help='serial device path') parser.add_argument('-q', '--quiet', action='store_true', default=False, help='suppress output text') args = parser.parse_args() SerialManager.connect(args.device_file, 9600) s = SerialManager # Periodic timer to query for status reports # TODO: Need to track down why this doesn't restart consistently before a release. # def periodic(): # s.write('?') # t = threading.Timer(0.1, periodic) # In seconds # t.start() # Initialize f = args.gcode_file verbose = True if args.quiet: verbose = False # Wake up grbl
def run_with_callback(host, port): """ Start a wsgiref server instance with control over the main loop. This is a function that I derived from the bottle.py run() """ handler = default_app() server = make_server(host, port, handler, handler_class=HackedWSGIRequestHandler) if CERTSDIR: server.socket = ssl.wrap_socket(server.socket, keyfile=KEYFILE, certfile=CERTSFILE, server_side=True, cert_reqs=ssl.CERT_REQUIRED, ca_certs=CA_CERTSFILE) loadAccounts() server.verify_request = verify_request server.timeout = 0.01 server.quiet = True print "Persistent storage root is: " + storage_dir() print "-----------------------------------------------------------------------------" print "Bottle server starting up ..." print "Serial is set to %d bps" % BITSPERSECOND print "Point your browser to: " if CERTSDIR: print "https://%s:%d/" % (COMMON_NAME, port) elif COMMON_NAME: print "http://%s:%d/" % (COMMON_NAME, port) else: print "http://%s:%d/" % ('127.0.0.1', port) print "Use Ctrl-C to quit." print "-----------------------------------------------------------------------------" print # auto-connect on startup global SERIAL_PORT if not SERIAL_PORT: SERIAL_PORT = SerialManager.match_device(GUESS_PREFIX, BITSPERSECOND) SerialManager.connect(SERIAL_PORT, BITSPERSECOND) # I:Mega Start time.sleep(1.0) # I:Mega End # # open web-browser # try: # webbrowser.open_new_tab('http://127.0.0.1:'+str(port)) # pass # except webbrowser.Error: # print "Cannot open Webbrowser, please do so manually." sys.stdout.flush() # make sure everything gets flushed server.timeout = 0 lastPowerStatus = 0 powerStateChange = 0 while 1: try: serial_handler('1') SerialManager.send_queue_as_ready() server.handle_request() if HARDWARE == 'raspberrypi': powerStatus = RPiPowerControl.interval_check() if powerStatus != lastPowerStatus: powerStateChange = 1 lastPowerStatus = powerStatus if powerStateChange: powerStateChange = checkStatus() time.sleep(0.0004) except KeyboardInterrupt: if HARDWARE == 'raspberrypi': RPiPowerControl.gpio_cleanup() break except: import traceback traceback.print_exc() break print "\nShutting down..." if redirect_pid: os.kill(redirect_pid, signal.SIGTERM) SerialManager.close()
import argparse # import threading RX_BUFFER_SIZE = 128 # Define command line argument interface parser = argparse.ArgumentParser(description='Stream g-code file to grbl. (pySerial and argparse libraries required)') parser.add_argument('gcode_file', type=argparse.FileType('r'), help='g-code filename to be streamed') parser.add_argument('device_file', help='serial device path') parser.add_argument('-q','--quiet',action='store_true', default=False, help='suppress output text') args = parser.parse_args() SerialManager.connect(args.device_file, 9600) s = SerialManager # Periodic timer to query for status reports # TODO: Need to track down why this doesn't restart consistently before a release. # def periodic(): # s.write('?') # t = threading.Timer(0.1, periodic) # In seconds # t.start() # Initialize f = args.gcode_file verbose = True if args.quiet : verbose = False # Wake up grbl
Provided as an illustration of the basic communication interface for grbl. When grbl has finished parsing the g-code block, it will return an 'ok' or 'error' response. When the planner buffer is full, grbl will not send a response until the planner buffer clears space. G02/03 arcs are special exceptions, where they inject short line segments directly into the planner. So there may not be a response from grbl for the duration of the arc. """ from serial_manager import SerialManager import time # Open grbl serial port SerialManager.connect("COM4", 9600) s = SerialManager # Open g-code file f = open('electric_turtle.nc','r'); # Wake up grbl s.write("\r\n\r\n") # Stream g-code to grbl for line in f: l = line.strip() # Strip all EOL characters for consistency print 'Sending: ' + l, s.write(l + '\n') # Send g-code block to grbl grbl_out = s.read_to('\n') # Wait for grbl response with carriage return print ' : ' + grbl_out.strip()
Provided as an illustration of the basic communication interface for grbl. When grbl has finished parsing the g-code block, it will return an 'ok' or 'error' response. When the planner buffer is full, grbl will not send a response until the planner buffer clears space. G02/03 arcs are special exceptions, where they inject short line segments directly into the planner. So there may not be a response from grbl for the duration of the arc. """ from serial_manager import SerialManager import time # Open grbl serial port SerialManager.connect("COM4", 9600) s = SerialManager # Open g-code file f = open('electric_turtle.nc', 'r') # Wake up grbl s.write("\r\n\r\n") # Stream g-code to grbl for line in f: l = line.strip() # Strip all EOL characters for consistency print 'Sending: ' + l, s.write(l + '\n') # Send g-code block to grbl grbl_out = s.read_to('\n') # Wait for grbl response with carriage return print ' : ' + grbl_out.strip()