-
Notifications
You must be signed in to change notification settings - Fork 3
/
SerialLibrary.py
277 lines (225 loc) · 7.61 KB
/
SerialLibrary.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# pylint: disable=C0103
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""
Author: reharish
Requirements: robotframework, pyserial
"""
from io import BytesIO
import serial
from serial import SerialException
from robot.api.deco import keyword
class PySerialError(Exception):
"""Represents the Serial exceptions"""
class SerialLibrary:
"""
Library for interacting with serial devices.
== Example ==
| Serial.Connect | /dev/ttyUSB0 | 115200
| Serial.Write | Hello
| ${DATA}= | Serial.Read
| ${DATA}= | Serial.Read All
| Serial.Save Buffer to file | /path/to/serial.log
== Another Example ==
| Connect | /dev/ttyUSB1 | 115200
| Set Timeout | 2
| Reset Input Buffer
| Reset Output Buffer
| Write | Hello
| Write | Hello
| Read until | World
| Read All
"""
ROBOT_LIBRARY_SCOPE = "GLOBAL"
ROBOT_AUTO_KEYWORDS = False
def __init__(self, unicode='utf-8'):
"""
Initializes the Serial Library.
``unicode`` - Unicode encoding for data communication.
"""
self.device = None
self.timeout = 10
self.unicode = unicode
self.buffer = BytesIO()
@keyword("Connect")
def connect_to_serial_url(self, url: str, baudrate: int) -> serial.Serial:
"""
Connects to a serial device.
``url`` - The device name or URL. see: https://pythonhosted.org/pyserial/url_handlers.html#urls
``baudrate`` - The baud rate to use for communication.
NOTE: baudrate is kept for backwords compatibility.
=== Example ===
| Connect | <device> | <baudrate>
| Connect | /dev/ttyUSB0 | 115200
| Connect | spy:///dev/ttyUSB0/file=dump-comms.txt | 115200
=== Returns ===
The connected serial device object.
"""
try:
self.device = serial.serial_for_url(url, do_not_open=True)
self.device.baudrate = baudrate
self.device.open()
except SerialException as exc:
raise PySerialError(f"Failed to connect {url}: {exc}") from exc
return self.device
@keyword("Disconnect")
def disconnect_from_serial(self):
"""
Disconnects from the serial device.
=== Example ===
| Disconnect
"""
self.device.close()
@keyword("Set Timeout")
def set_timeout(self, seconds: int):
"""
Sets the read timeout for the serial device.
``seconds`` - The timeout value in seconds.
=== Example ===
| Set Timeout | <seconds>
| Set Timeout | 10
"""
if not self.device:
raise PySerialError("Device not initialized to set timeout")
self.device.timeout = seconds
@keyword("Set Write Timeout")
def set_write_timeout(self, seconds: int):
"""
Sets the write timeout for the serial device.
``seconds`` - The timeout value in seconds.
=== Example ===
| Set Write Timeout | <seconds>
| Set Write Timeout | 10
"""
if not self.device:
raise PySerialError("Device not connected to set write timeout")
self.device.write_timeout = seconds
@keyword("Set Unicode")
def set_unicode(self, unicode: str):
"""
Sets the Unicode encoding for data communication.
``unicode`` - The Unicode encoding to use.
=== Example ===
| Set Unicode | <unicode>
| Set Unicode | utf-8
"""
self.unicode = unicode
@keyword("Read")
def read(self) -> str:
"""
Reads data from the serial device.
=== Example ===
| Read
=== Returns ===
The read data as a string.
"""
if not self.device:
raise PySerialError("Device not connected to start read")
buff = self.device.read()
self.buffer.write(buff)
buff = buff.decode(self.unicode)
return buff
@keyword("Write")
def write(self, data: str):
"""
Writes data to the serial device.
``data`` - The data to write.
=== Example ===
| Write
"""
if not self.device:
raise PySerialError("Device not connected to start write")
try:
self.device.write(data.encode())
except SerialException as exc:
raise PySerialError(f"Failed to write to device: {exc}") from exc
@keyword("Read until")
def read_until(self, expected: str, quiet=True) -> str:
"""
Reads data from the serial device until a specified string is encountered.
``expected`` - The string to read until.
=== Example ===
| Read until | <expected>
| Read until | string quiet=False
=== Returns ===
The read data as a string.
If the expected not found in the read data, It throws error.
"""
if not self.device:
raise PySerialError("Device not connected to start read")
expected = expected.encode()
buff = self.device.read_until(expected)
self.buffer.write(buff)
data = buff.decode(self.unicode)
if quiet or expected in buff.decode(self.unicode):
return data
raise PySerialError(f"Expected: {expected} not in {data}")
@keyword("Read all")
def read_all(self) -> str:
"""
Reads all the data available in the buffer
=== Example ===
| Read All
=== Returns ===
The read data as a string.
"""
if not self.device:
raise PySerialError("Device not connected to start read")
buff = self.device.read_all()
self.buffer.write(buff)
return buff.decode(self.unicode)
@keyword("Reset Input Buffer")
def reset_input_buffer(self):
"""
Clear the input buffer for the serial device
=== Example ===
| Reset Input Buffer
"""
if not self.device:
raise PySerialError("Device not connected to reset buffer")
self.device.reset_input_buffer()
@keyword("Reset Output Buffer")
def reset_output_buffer(self):
"""
Clear the output buffer for the serial device
=== Example ===
| Reset Output Buffer
"""
if not self.device:
raise PySerialError("Device not connected to reset buffer")
self.device.reset_output_buffer()
@keyword("Close")
def close_connection(self):
"""
Closes the connection to the serial device.
=== Example ===
| Close
"""
if self.device:
self.device.close()
self.device = None
@keyword("Save buffer to file")
def save_into_file(self, outputfile: str):
"""
Saves the data buffer into a file.
``outputfile`` - The path to the output file.
=== Example ===
| Save buffer to file | <outputfile>
| Save buffer to file | test.log
"""
self.set_timeout(10)
self.read_all()
with open(outputfile, 'wb+') as outf:
outf.write(self.buffer.getvalue())
print(f"File saved: {outputfile}")