def __open_usb_device(self, match_usb_func, comport, vid, pid): if match_usb_func(vid, pid): time.sleep(0.1) try: self.__ser = serial.Serial(comport, 115200) except serial.SerialException as e: logging.debug('%s, retry...' % (str(e))) else: logging.info('Got %s' % (comport)) return True return False
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 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
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 <space> 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))
def connection(self): return pyserial.Serial('/dev/ttyACM0', baudrate=9600, timeout=0.1)