Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
import pyserial as serial
import time, sys, os
print "Welcome to the SERPINT test program."
print "This program expects that you have followed the instructions in doc/demo.odt"
port_name=raw_input("What serial port did you use during setup? [i.e. /dev/ttyS44] ")
port=serial.Serial(port_name)
port.write(chr(2))
while 1:
if ord(port.read())!=1: break
port.write(chr(30))
if ord(port.read())!=10:
print "ERROR!"
sys.exit()
#----------------------------------------------------------------------------------------#
getch=_GetchUnix()
print "\n\nPress to toggle the power on GPIO pin 4 [http://elinux.org/images/2/2a/GPIOs.png] and q to quit [close all SERPINT components]"
state=False
port.write(chr(22))
port.write(chr(7))
port.write(chr(26))
def serial_to_socket(ser_addr, port): #forward data from a virtual serial port to a socket, I would use remserial, but it only works with proper (non-virtual) serial ports
try:
ser=serial.Serial("/dev/"+ser_addr) #open the serial port
ser.open()
sock=socket.socket(socket.AF_INET, socket.SOCK_STREAM) #open and bind the socket
sock.bind(("", port))
sock.listen(1)
print "Waiting for remote connection to socket..." #wait for the other machine to run SOCKTOSERIAL
conn, addr=sock.accept()
print "Got connection!"
except BaseException as e:
throw_error(7,e) #error initilizing
thread.start_new_thread(ser_to_sock_a, (ser, conn)) #start thread a (forwarding from serial to socket)
time.sleep(0.1) #slight delay to keep the serial port psuedo-threadsafe
thread.start_new_thread(ser_to_sock_b, (ser, conn)) #start thread b (forwarding from socket to serial)
while 1:pass #keep running, as not to close the process with still-running threads
def serial_to_socket(ser_addr, port): #forward data from a virtual serial port to a socket, I would use remserial, but it only works with proper (non-virtual) serial ports
try:
ser=serial.Serial("/dev/"+ser_addr) #open the serial port
ser.open()
sock=socket.socket(socket.AF_INET, socket.SOCK_STREAM) #open and bind the socket
sock.bind(("", port))
sock.listen(1)
print "Waiting for remote connection to socket..." #wait for the other machine to run SOCKTOSERIAL
conn, addr=sock.accept()
print "Got connection!"
except BaseException as e:
throw_error(7,e) #error initilizing
thread.start_new_thread(ser_to_sock_a, (ser, conn)) #start thread a (forwarding from serial to socket)
time.sleep(0.1) #slight delay to keep the serial port psuedo-threadsafe
thread.start_new_thread(ser_to_sock_b, (ser, conn)) #start thread b (forwarding from socket to serial)
while 1:pass #keep running, as not to close the process with still-running threads
def serial_format_bridge_passable(port, ser_addr, ser_addr2): #A passable function object that is passed to the SFB child module, and then executed, creating the sock and ser objects
try:
ser=serial.Serial("/dev/"+ser_addr) #Open serial port
ser.open()
sock=socket.socket(socket.AF_INET, socket.SOCK_STREAM)#Open socket
sock.bind(("", port))
sock.listen(1)
print "Waiting for remote connection to socket..."
thread.start_new_thread(vsi_system_sock, (ser_addr2,'localhost',port)) #Start the system socket
conn, addr=sock.accept()#Meanwhile, wait for the system socket to become live
print "Got connection!"
print "Opening Phase 2 Port..."
return conn, ser
except BaseException as e:
throw_error(7,e)
def __init__(self, port=DEFAULT_PORT, baud=ESP_ROM_BAUD, trace_enabled=False):
"""Base constructor for ESPLoader bootloader interaction
Don't call this constructor, either instantiate ESP8266ROM
or ESP32ROM, or use ESPLoader.detect_chip().
This base class has all of the instance methods for bootloader
functionality supported across various chips & stub
loaders. Subclasses replace the functions they don't support
with ones which throw NotImplementedInROMError().
"""
if isinstance(port, serial.Serial):
self._port = port
else:
self._port = serial.serial_for_url(port)
self._slip_reader = slip_reader(self._port, self.trace)
# setting baud rate in a separate step is a workaround for
# CH341 driver on some Linux versions (this opens at 9600 then
# sets), shouldn't matter for other platforms/drivers. See
# https://github.com/espressif/esptool/issues/44#issuecomment-107094446
self._set_port_baudrate(baud)
self._trace_enabled = trace_enabled
def serial_format_bridge_passable(port, ser_addr, ser_addr2): #A passable function object that is passed to the SFB child module, and then executed, creating the sock and ser objects
try:
ser=serial.Serial("/dev/"+ser_addr) #Open serial port
ser.open()
sock=socket.socket(socket.AF_INET, socket.SOCK_STREAM)#Open socket
sock.bind(("", port))
sock.listen(1)
print "Waiting for remote connection to socket..."
thread.start_new_thread(vsi_system_sock, (ser_addr2,'localhost',port)) #Start the system socket
conn, addr=sock.accept()#Meanwhile, wait for the system socket to become live
print "Got connection!"
print "Opening Phase 2 Port..."
return conn, ser
except BaseException as e:
throw_error(7,e)
def __init__(self, port=DEFAULT_PORT, baud=ESP_ROM_BAUD, trace_enabled=False):
"""Base constructor for ESPLoader bootloader interaction
Don't call this constructor, either instantiate ESP8266ROM
or ESP32ROM, or use ESPLoader.detect_chip().
This base class has all of the instance methods for bootloader
functionality supported across various chips & stub
loaders. Subclasses replace the functions they don't support
with ones which throw NotImplementedInROMError().
"""
if isinstance(port, serial.Serial):
self._port = port
else:
self._port = serial.serial_for_url(port)
self._slip_reader = slip_reader(self._port, self.trace)
# setting baud rate in a separate step is a workaround for
# CH341 driver on some Linux versions (this opens at 9600 then
# sets), shouldn't matter for other platforms/drivers. See
# https://github.com/espressif/esptool/issues/44#issuecomment-107094446
self._set_port_baudrate(baud)
self._trace_enabled = trace_enabled