<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import <EOL> from __future__ import print_function <EOL> from __future__ import division <EOL> from __future__ import unicode_literals <EOL> import time <EOL> import threading <EOL> from . import pyserial <EOL> from . import settings <EOL> from . import message_queue <EOL> serials_in_use = [ ] <EOL> serial_monitor_dict = { } <EOL> class SerialMonitor ( object ) : <EOL> def __init__ ( self , serial_port , console = None ) : <EOL> super ( SerialMonitor , self ) . __init__ ( ) <EOL> self . port = serial_port <EOL> self . serial = pyserial . Serial ( ) <EOL> self . serial . port = serial_port <EOL> self . queue = message_queue . MessageQueue ( console ) <EOL> self . arduino_settings = settings . get_arduino_settings ( ) <EOL> self . is_alive = False <EOL> def is_running ( self ) : <EOL> return self . is_alive <EOL> def start ( self ) : <EOL> self . queue . start_print ( ) <EOL> if not self . is_alive : <EOL> baudrate = self . arduino_settings . get ( '<STR_LIT>' , <NUM_LIT> ) <EOL> self . serial . baudrate = baudrate <EOL> if is_serial_available ( self . port ) : <EOL> self . serial . open ( ) <EOL> self . is_alive = True <EOL> monitor_thread = threading . Thread ( target = self . receive ) <EOL> monitor_thread . start ( ) <EOL> else : <EOL> msg = '<STR_LIT>' <EOL> msg += '<STR_LIT>' <EOL> self . queue . put ( msg , self . port ) <EOL> self . stop ( ) <EOL> def stop ( self ) : <EOL> self . is_alive = False <EOL> self . queue . stop_print ( ) <EOL> def receive ( self ) : <EOL> length_before = <NUM_LIT:0> <EOL> while self . is_alive : <EOL> number = self . serial . inWaiting ( ) <EOL> if number > <NUM_LIT:0> : <EOL> in_text = self . serial . read ( number ) <EOL> length_in_text = len ( in_text ) <EOL> in_text = convert_mode ( in_text , length_before ) <EOL> self . queue . put ( in_text ) <EOL> length_before += length_in_text <EOL> length_before %= <NUM_LIT:20> <EOL> time . sleep ( <NUM_LIT> ) <EOL> self . serial . close ( ) <EOL> def send ( self , out_text ) : <EOL> line_ending = self . arduino_settings . get ( '<STR_LIT>' , '<STR_LIT:\n>' ) <EOL> out_text += line_ending <EOL> self . queue . put ( '<STR_LIT>' , out_text ) <EOL> out_text = out_text . encode ( '<STR_LIT:utf-8>' , '<STR_LIT:replace>' ) <EOL> self . serial . write ( out_text ) <EOL> def convert_mode ( in_text , str_len = <NUM_LIT:0> ) : <EOL> arduino_settings = settings . get_arduino_settings ( ) <EOL> text = u'<STR_LIT>' <EOL> display_mode = arduino_settings . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if display_mode == '<STR_LIT>' : <EOL> for character in in_text : <EOL> text += chr ( character ) <EOL> elif display_mode == '<STR_LIT>' : <EOL> for ( index , character ) in enumerate ( in_text ) : <EOL> text += u'<STR_LIT>' % character <EOL> if ( index + str_len + <NUM_LIT:1> ) % <NUM_LIT:8> == <NUM_LIT:0> : <EOL> text += '<STR_LIT:\t>' <EOL> if ( index + str_len + <NUM_LIT:1> ) % <NUM_LIT:16> == <NUM_LIT:0> : <EOL> text += '<STR_LIT:\n>' <EOL> else : <EOL> text = in_text . decode ( '<STR_LIT:utf-8>' , '<STR_LIT:replace>' ) <EOL> return text <EOL> def convert_to_ascii_mode ( ) : <EOL> pass <EOL> def convert_to_hex_mode ( ) : <EOL> pass <EOL> def is_serial_available ( serial_port ) : <EOL> state = False <EOL> serial = pyserial . Serial ( ) <EOL> serial . port = serial_port <EOL> try : <EOL> serial . open ( ) <EOL> except pyserial . serialutil . SerialException : <EOL> pass <EOL> except UnicodeDecodeError : <EOL> pass <EOL> else : <EOL> if serial . isOpen ( ) : <EOL> state = True <EOL> serial . close ( ) <EOL> return state </s>
