-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenlaceRx.py
79 lines (61 loc) · 1.83 KB
/
enlaceRx.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#####################################################
# Camada Física da Computação
#Carareto
#17/02/2018
# Camada de Enlace
####################################################
# Importa pacote de tempo
import time
# Threads
import threading
# Class
class RX(object):
def __init__(self, fisica):
self.fisica = fisica
self.buffer = bytes(bytearray())
self.threadStop = False
self.threadMutex = True
self.READLEN = 1024
def thread(self):
while not self.threadStop:
if(self.threadMutex == True):
rxTemp, nRx = self.fisica.read(self.READLEN)
if (nRx > 0):
self.buffer += rxTemp
time.sleep(0.01)
def threadStart(self):
self.thread = threading.Thread(target=self.thread, args=())
self.thread.start()
def threadKill(self):
self.threadStop = True
def threadPause(self):
self.threadMutex = False
def threadResume(self):
self.threadMutex = True
def getIsEmpty(self):
if(self.getBufferLen() == 0):
return(True)
else:
return(False)
def getBufferLen(self):
return(len(self.buffer))
def getAllBuffer(self, len):
self.threadPause()
b = self.buffer[:]
self.clearBuffer()
self.threadResume()
return(b)
def getBuffer(self, nData):
self.threadPause()
b = self.buffer[0:nData]
self.buffer = self.buffer[nData:]
self.threadResume()
return(b)
def getNData(self, size):
while(self.getBufferLen() < size):
time.sleep(0.05)
return(self.getBuffer(size))
def clearBuffer(self):
self.buffer = b""