serialPort.open() outStr = '' inStr = '' serialPort.flushInput() serialPort.flushOutput() #for i, a in enumerate(range(33, 126)): # outStr += chr(a) outStr=rfile.read() outStr=outStr.encode() time.sleep(1) serialPort.setRTS(1) serialPort.write(outStr) #time.sleep(0.05) ct=serialPort.getCTS() if serialPort.getCTS(): inStr = serialPort.read(10000) inStr=inStr.decode() wfile.write(inStr) print("Serial Port Input is : ",inStr) outStr=outStr.decode() serialPort.close() rfile.close() wfile.close()
file_exists = os.path.isfile(filename) ser.setDTR(not file_exists) if file_exists != previous_file_exists: # File existence has changed. Don't allow accept any switch # presses for a second. time.sleep(1) elif r is not None: # CTS changed state (or system call interrupted). Read CTS, sample # 5 times in 250mS for switch debouncing cts_count = 0 for i in range(5): time.sleep(0.050) if ser.getCTS(): cts_count += 1 if cts_count >= 3: file_exists = not file_exists ser.setDTR(not file_exists) try: if file_exists: fh = open(filename, 'a') fh.close() else: os.remove(filename) # Dont' allow state to be changed immediately time.sleep(1) except Exception as e: print(str(e))
serialPort = Serial("/dev/ttyUSB2", baudrate=9600, timeout=10) if (serialPort.isOpen() == False): serialPort.open() st = bitarray.bitarray() wfile = open("Out.txt", "w") while (serialPort.getDSR()): #print("Receiving...") pass #serialPort.getCTS() #start=time.time() for i in range(0, 40): st.append(serialPort.getCTS()) time.sleep(1) #print(st) #end=time.time() #print("Time Taken is ",(end-start)) print("Received Output without Rx and Tx is:") try: print(st) print(st.tostring()) wfile.write(st.decode()) except: print(st)
# # swift.set_position(x=200, y=0, z=100) # time.sleep(5) # print(swift.get_polar()) # # # # # def test(ret): # # # print(ret) # # # # # # print(metal.send_cmd_sync('G0')) # # # metal.send_cmd_async('G0', callback=test) # # # # while True: # # time.sleep(1) import time from serial import Serial com = Serial("COM12", baudrate=115200) print('getCD:', com.getCD()) print('getCTS:', com.getCTS()) print('getDSR:', com.getDSR()) print('getRI:', com.getRI()) print('get_settings:', com.get_settings()) print('timeout:', com.timeout) while True: time.sleep(1)
def run(self, daemon_mode=True): if (daemon_mode): logger.info('Daemon starting, device=' + device + ', filename=' + filename) # Set up signal handler signal.signal(signal.SIGTERM, signal_handler) previous_device_state = 'none' previous_file_exists = os.path.isfile(filename) while True: try: file_exists = os.path.isfile(filename) if file_exists != previous_file_exists: # File existence has changed whilst serial device # is not available, cannot signal the change in # state here. if file_exists: logger.info(filename + ' created externally') else: logger.info(filename + ' removed externally') ser = Serial(device) logger.info('Opened ' + device) ser.setDTR(xor(not file_exists, led_active_low )) while True: r = timeout(ioctl, [ser.fd, TIOCMIWAIT, TIOCM_CTS], timeout_duration=10) file_exists = os.path.isfile(filename) ser.setDTR(xor(not file_exists, led_active_low)) if file_exists != previous_file_exists: # File existence has changed, serial device is # available so indicate the change. Don't act # on any switch input since it might be # inverse to what was intended. Wait a short # while before reading the switch again so # that the user can recognise the # externally-induced state change. if file_exists: logger.info(filename + ' created externally') else: logger.info(filename + ' removed externally') time.sleep(1) elif r is not None: # CTS changed state (or system call # interrupted). Read CTS, sample 5 times in # 250mS for switch debouncing cts_count = 0 for i in range(5): time.sleep(0.050) if ser.getCTS(): cts_count += 1 if cts_count >= 3: file_exists = not file_exists ser.setDTR(xor(not file_exists, led_active_low)) try: if file_exists: fh = open(filename, 'a') fh.close() logger.info(filename + ' created') else: os.remove(filename) logger.info(filename + ' removed') # Dont' allow state to be changed # immediately time.sleep(1) except Exception as e: logger.error(str(e)) for i in range(10): ser.setDTR(xor(i % 2, led_active_low)) time.sleep(0.2) previous_file_exists = file_exists previous_device_state = 'opened' except (SerialTimeoutException, IOError): if previous_device_state == 'opened': logger.error('Cannot access ' + device + ', waiting for it to appear') elif previous_device_state == 'none': logger.error('Cannot open ' + device + ', waiting for it to appear') previous_device_state = 'error' previous_file_exists = file_exists time.sleep(5)
#!/usr/bin/env python2 # # pyserial test from serial import Serial from eagleye import EasyConfig import time, sys config = EasyConfig("serial_test.cfg") try: ser = Serial(config.serial_device, 192000) except OSError: print "Cannot open serial device" exit(1) while True: if raw_input("eh?:").startswith("y"): ser.setRTS() exit(0) #print ser.name while True: if ser.getCTS(): print "True!" time.sleep(0.3) time.sleep(0.00001) exit(0)