Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from array import *
- import serial
- import time
- import multiprocessing
- import sqlite3
- conn=sqlite3.connect('rover.db')
- curs=conn.cursor()
- class SerialProcess(multiprocessing.Process):
- def __init__(self, taskQ, resultQ):
- multiprocessing.Process.__init__(self)
- self.taskQ = taskQ
- self.resultQ = resultQ
- self.usbPort = '/dev/ttyAMA0'
- self.sp = serial.Serial(self.usbPort, 2400, timeout=1)
- def close(self):
- self.sp.close()
- def sendData(self, data):
- print "sendData start..."
- self.sp.write(data)
- time.sleep(3)
- print "sendData done: " + data
- def run(self):
- self.sp.flushInput()
- while True:
- # look for incoming tornado request
- if not self.taskQ.empty():
- task = self.taskQ.get()
- # send it to the arduino
- self.sp.write(task)
- print "received from client, sending to radio: JOYSTICK_X " + str(ord(task[1])) + " JOYSTICK_Y: " + str(ord(task[2]))
- # look for incoming serial data
- if (self.sp.inWaiting() > 0):
- receivedArray = bytes()
- while (self.sp.inWaiting()):
- receivedArray = receivedArray + self.sp.read(1)
- # encode string to two utf-8 characters (A through P) per byte
- encoded = bytes()
- index = 0
- while (index < len(receivedArray)):
- encoded += chr(ord(receivedArray[index]) / 16 + 65)
- encoded += chr(ord(receivedArray[index]) % 16 + 65)
- index += 1
- if (len(receivedArray) == 2):
- curs.execute("INSERT INTO pings values((?), (?))", (ord(receivedArray[0]), ord(receivedArray[1])))
- conn.commit()
- # send it to tornado
- self.resultQ.put(encoded)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement