-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSerial.py
38 lines (32 loc) · 994 Bytes
/
Serial.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import serial
import time
ser = serial.Serial(port='/dev/ttyUSB0',baudrate = 9600,parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS)
try:
ser.open()
except Exception, e:
print "Error: Cannot open serial port: " + str(e)
exit()
if ser.isOpen():
print ser.portstr
try:#flush input and output buffers that could be halting comunication
ser.flushInput()
ser.flushOutput()
except Exception, e1:
print "error communicating...: " + str(e1)
print 'enter your commands below. \r\nInsert "exit" to leave the aplication'
input = 1
while 1:
#get keyboard input
input = raw_input("<< ")
if input == 'exit' :
ser.close()
exit()
else:
#send character to the device
ser.write(input)# may need \r or\n or \r\n to respond correctly
trash = ''
time.sleep(1)
while ser.inWaiting() > 0:
trash += ser.read(1)
if trash != '':
print ">>"+trash