diff --git a/README.md b/README.md index 1fad660..69af4e6 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/Serial.py b/Serial.py index e9cd1b5..9c3cb63 100644 --- a/Serial.py +++ b/Serial.py @@ -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 """ @@ -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): """ @@ -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}") diff --git a/example/example.robot b/example/example.robot index f8d5016..501f9c8 100644 --- a/example/example.robot +++ b/example/example.robot @@ -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