summaryrefslogtreecommitdiffstats
path: root/For Weekly Test/tricode/serialTest.py
blob: 956c37d2525650f556a23108656d676075240767 (plain) (blame)
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 time
from serial import * 

# configure the serial connections (the parameters differs on the device you are connecting to)
ser = Serial(
	port='/dev/ttyUSB0',
	baudrate=19200,
	bytesize=EIGHTBITS,
	parity=PARITY_NONE,
	stopbits=STOPBITS_ONE
)

ser.open()
ser.isOpen()

print 'Enter your commands below.\r\nInsert "exit" to leave the application.'

input=1
while 1 :
	# get keyboard input
	input = raw_input(">> ")
        # Python 3 users
        # input = input(">> ")
	if input == 'exit':
		ser.close()
		exit()
	else:
		# send the character to the device
		# (note that I happend a \r\n carriage return and line feed to the characters - this is requested by my device)
		ser.write(input + '\r\n')
		out = ''
		# let's wait one second before reading output (let's give device time to answer)
		time.sleep(1)
		while ser.inWaiting() > 0:
			out += ser.read(1)
			
		if out != '':
			print ">>" + out