Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Keyword Functions #1

Merged
merged 1 commit into from
Apr 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ Example Test
- **Read**: Reads data from the serial device.
- **Write**: Writes data to the serial device.
- **Read until**: Reads data from the serial device until a specified string is encountered.
- **Read All**: Reads all the data from the input buffer
- **Reset Input Buffer**: Clear the input buffer for the serial device
- **Reset Output Buffer**: Clear the output buffer for the serial device
- **Save buffer to file**: Saves the data buffer into a file.

## Documentation
Expand Down
41 changes: 39 additions & 2 deletions Serial.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,19 @@ class Serial:
| 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

"""

Expand Down Expand Up @@ -169,6 +173,40 @@ def read_until(self, expected: str) -> str:
self.buffer.write(buff)
return buff.decode(self.unicode)

@keyword("Read All")
def read_all(self):
"""
Reads all the data available in the buffer

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
"""
if not self.device:
raise PySerialError("Device not connected to reset buffer")
self.device.reset_input_buffer()
return

@keyword("Reset Output Buffer")
def reset_output_buffer(self):
"""
Clear the output buffer for the serial device
"""
if not self.device:
raise PySerialError("Device not connected to reset buffer")
self.device.reset_output_buffer()
return

@keyword("Close")
def close_connection(self):
"""
Expand All @@ -187,8 +225,7 @@ def save_into_file(self, outputfile: str):
- outputfile: The path to the output file.
"""
self.set_timeout(10)
buff = self.device.read()
self.buffer.write(buff)
self.read_all()
with open(outputfile, 'wb+') as outf:
outf.write(self.buffer.getvalue())
print(f"File saved: {outputfile}")
2 changes: 2 additions & 0 deletions example/example.robot
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@ Suite Teardown Save buffer into file serial.log
Basic Read and Write
Serial.Connect /dev/ttyUSB0 115200
Serial.Set Timeout ${5}
Serial.Reset Input Buffer
Serial.Reset Output Buffer
Serial.Write Hello
Serial.Read