Main Page
Namespaces
Classes
Files
File List
src
xbee
tests
Fake.py
Go to the documentation of this file.
1
#! /usr/bin/python
2
"""
3
Fake.py
4
5
By Paul Malmsten, 2010
6
pmalmsten@gmail.com
7
8
Provides fake device objects for other unit tests.
9
"""
10
import
sys
11
12
class
FakeDevice
:
13
"""
14
Represents a fake serial port for testing purposes
15
"""
16
def
__init__
(self):
17
self.
data
=
''
18
19
def
write
(self, data):
20
"""
21
Writes data to the fake port for later evaluation
22
"""
23
self.
data
= data
24
25
class
FakeReadDevice
:
26
"""
27
Represents a fake serial port which can be read from in a similar
28
fashion to the real thing
29
"""
30
31
def
__init__
(self, data, silent_on_empty=False):
32
self.
data
= data
33
self.
read_index
= 0
34
self.
silent_on_empty
= silent_on_empty
35
36
def
read
(self, length=1):
37
"""
38
Read the indicated number of bytes from the port
39
"""
40
# If too many bytes would be read, raise exception
41
if
self.
read_index
+ length > len(self.
data
):
42
if
self.
silent_on_empty
:
43
sys.exit(0)
44
else
:
45
raise
ValueError(
"Not enough bytes exist!"
)
46
47
read_data = self.
data
[self.
read_index
:self.
read_index
+ length]
48
self.
read_index
+= length
49
50
return
read_data
51
52
def
inWaiting
(self):
53
"""
54
Returns the number of bytes available to be read
55
"""
56
return
len(self.
data
) - self.
read_index
xbee.tests.Fake.FakeReadDevice.data
data
Definition:
Fake.py:32
xbee.tests.Fake.FakeReadDevice.read_index
read_index
Definition:
Fake.py:33
xbee.tests.Fake.FakeReadDevice.__init__
def __init__(self, data, silent_on_empty=False)
Definition:
Fake.py:31
xbee.tests.Fake.FakeReadDevice
Definition:
Fake.py:25
xbee.tests.Fake.FakeReadDevice.silent_on_empty
silent_on_empty
Definition:
Fake.py:34
xbee.tests.Fake.FakeDevice.write
def write(self, data)
Definition:
Fake.py:19
xbee.tests.Fake.FakeReadDevice.inWaiting
def inWaiting(self)
Definition:
Fake.py:52
xbee.tests.Fake.FakeDevice.__init__
def __init__(self)
Definition:
Fake.py:16
xbee.tests.Fake.FakeDevice
Definition:
Fake.py:12
xbee.tests.Fake.FakeReadDevice.read
def read(self, length=1)
Definition:
Fake.py:36
xbee.tests.Fake.FakeDevice.data
data
Definition:
Fake.py:17
rosserial_xbee
Author(s): Adam Stambler
autogenerated on Fri Jun 7 2019 22:03:11