Revision 1941 trunk/code/projects/calibration_platform/server/cal_sta_server.py

View differences:

cal_sta_server.py
1 1
#!/usr/bin/python
2 2
import sys, serial
3
import sqlite3
3 4

  
4
try:
5
	serial0 = serial.Serial(port='/dev/ttyUSB0', baudrate=19200)    # Adruino
6
	serial1 = serial.Serial(port='/dev/ttyUSB1', baudrate=115200)   # Robot
7
except serial.SerialException as e:
8
	print 'Error: %s' % e
9
	sys.exit(0)
5
class Station(serial.Serial):
6
	def __init__(self, serial_path='/dev/arduino', baudrate=19200):
7
		try:
8
			serial.Serial.__init__(self, serial_path, baudrate)
9
		except serial.SerialException as e:
10
			print 'Warning: Unable to open serial communication to station (%s)'%e
11
			sys.exit(0)
12
	def db_register(self, sensor_name, db_cursor):
13
		t = (sensor_name,)
14
		db_cursor.execute('create table %s (value INTEGER)' % t)
15
	def db_add(self, sensor_name, db_cursor, db_conn, value):
16
		t = (sensor_name, value)
17
		db_cursor.execute('insert into %s (value) values (%s)' % t)
18
		db_conn.commit()
10 19

  
20
# SQLite3 database setup
21
db_path = '/home/colony/calibration_station/cal_sta_database.db'
22
ST_conn = sqlite3.connect(db_path)
23
ST_cursor = ST_conn.cursor()
24

  
25
ST = Station()
26
ST.db_register('sensor0', ST_cursor)
27
ST.db_register('sensor1', ST_cursor)
28

  
29
#	RO_serial = serial.Serial(port='/dev/ttyUSB1', baudrate=115200)   # Robot
11 30
while True:
12
	if serial0.inWaiting() > 0:
31
	if ST.inWaiting() > 0:
13 32
		try:
14
			print serial0.read()
33
			msg = ST.readline()[:-1]
15 34
		except OSError as e:
16 35
			print 'Error: %s' %e
17
	if serial1.inWaiting() > 0:
18
		try:
19
			print serial1.read()
20
		except OSError as e:
21
			print 'Error: %s' %e
22 36

  
23
usb.close()
37
		t = msg.split(':')
38
		print t
39

  
40
		if (len(t) == 3 and t[1] == '0'):
41
			ST.db_add('sensor0', ST_cursor, ST_conn, t[2])
42
		elif (len(t) == 3 and t[1] == '1'):
43
			ST.db_add('sensor1', ST_cursor, ST_conn, t[2])
44

  
45

  
46
ST_cursor.close()
47
ST.close()

Also available in: Unified diff