-
Notifications
You must be signed in to change notification settings - Fork 0
/
fakeduino.py
77 lines (67 loc) · 2.51 KB
/
fakeduino.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
# fakeSerial.py
# D. Thiebaut
# A very crude simulator for PySerial assuming it
# is emulating an Arduino.
# a Serial class emulator
class FakeSerial:
## init(): the constructor. Many of the arguments have default values
# and can be skipped when calling the constructor.
def __init__( self, port='COM1', baudrate = 19200, timeout=1,
bytesize = 8, parity = 'N', stopbits = 1, xonxoff=0,
rtscts = 0):
self.name = port
self.port = port
self.timeout = timeout
self.parity = parity
self.baudrate = baudrate
self.bytesize = bytesize
self.stopbits = stopbits
self.xonxoff = xonxoff
self.rtscts = rtscts
self._isOpen = True
self._receivedData = ""
self._data = "It was the best of times.\nIt was the worst of times.\n"
self.inWaiting = 0
## isOpen()
# returns True if the port to the Arduino is open. False otherwise
def isOpen( self ):
return self._isOpen
## open()
# opens the port
def open( self ):
self._isOpen = True
## close()
# closes the port
def close( self ):
self._isOpen = False
## write()
# writes a string of characters to the Arduino
def write( self, string ):
print( 'Arduino got: "' + string + '"' )
self._receivedData += string
## read()
# reads n characters from the fake Arduino. Actually n characters
# are read from the string _data and returned to the caller.
def read( self, n=1 ):
s = self._data[0:n]
self._data = self._data[n:]
#print( "read: now self._data = ", self._data )
return s
## readline()
# reads characters from the fake Arduino until a \n is found.
def readline( self ):
returnIndex = self._data.index( "\n" )
if returnIndex != -1:
s = self._data[0:returnIndex+1]
self._data = self._data[returnIndex+1:]
return s
else:
return ""
## __str__()
# returns a string representation of the serial class
def __str__( self ):
return "Serial<id=0xa81c10, open=%s>( port='%s', baudrate=%d," \
% ( str(self.isOpen), self.port, self.baudrate ) \
+ " bytesize=%d, parity='%s', stopbits=%d, xonxoff=%d, rtscts=%d)"\
% ( self.bytesize, self.parity, self.stopbits, self.xonxoff,
self.rtscts )