summaryrefslogtreecommitdiff
path: root/gr-serial/python/ser_functions.py
blob: 68ea18875e63da1a72f683dbf5cfa6721df23ff0 (plain)
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
39
40
41
42
43
44
45
#!/usr/bin/python

import gras
import numpy
import serial
class ser(gras.Block):
	

        def __init__(self):
                gras.Block.__init__(self,
                        name="ser",
                        in_sig=[numpy.float32],
                        out_sig=[numpy.float32])
		self.n = 1
        def set_parameters(self, port, baud, bytesize, parity, stopbits):
		
		try:
			print port
			self.ser_obj = serial.Serial(port, baud,  bytesize, parity,  stopbits)
			print("serial found on " + port )
			self.ser_obj.open()
		except:
			print "Couldn't Open Serial Port " + port + " Failed"
	

        def work(self, input_items, output_items):
		
		self.n = input_items[0][0]
                out = output_items[0][:self.n]
		# Input is size of output_items to be returned

		for i in range(self.n):
			
			# Try catch block to avoid Error
			# ValueError: invalid literal for int() with base 10: '\xfe354\r\n'
			try:
				out[i] = int(self.ser_obj.readline())
			except:
				pass
		
		print "OUT", out[:self.n]
			
		self.produce(0,len(out)) # Produce from port 0 output_items
		self.consume(0,1) # Consume from port 0 input_items