Skip to content

Send dummy request before closing serial port #740

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

Merged
merged 1 commit into from
Dec 13, 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
26 changes: 24 additions & 2 deletions src/main/python/main/ayab/engine/engine_fsm.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class State(Enum):
REQUEST_TEST = auto()
CONFIRM_TEST = auto()
RUN_TEST = auto()
DISCONNECT = auto()
FINISHED = auto()


Expand Down Expand Up @@ -196,8 +197,18 @@ def _API6_run_knit(control: Control, operation: Operation) -> Output:
if token == Token.reqLine:
pattern_finished = control.cnf_line_API6(param)
if pattern_finished:
control.state = State.FINISHED
return Output.KNITTING_FINISHED
# When closing the serial port, the final bytes written
# may be dropped by the driver
# (see https://github.com/serialport/serialport-rs/issues/117).
# This may cause the final `cnfLine` response to get lost and the
# firmware to get stuck knitting the previous row
# (see https://github.com/AllYarnsAreBeautiful/ayab-desktop/issues/662).
# To avoid this, before closing the port, we send a `reqInfo` message
# to the firmware and wait for the response.
control.com.req_info()
control.state = State.DISCONNECT
control.logger.debug("State DISCONNECT")
return Output.DISCONNECTING_FROM_MACHINE
else:
return Output.NEXT_LINE
# else
Expand Down Expand Up @@ -237,6 +248,17 @@ def _API6_run_test(control: Control, operation: Operation) -> Output:
control.check_serial_API6()
return Output.NONE

@staticmethod
def _API6_disconnect(control: Control, operation: Operation) -> Output:
token, _ = control.check_serial_API6()
if token == Token.cnfInfo:
# We received a response to our final `reqInfo` request,
# it is now safe to close the port.
control.state = State.FINISHED
return Output.KNITTING_FINISHED
# else
return Output.NONE

@staticmethod
def _API6_finished(control: Control, operation: Operation) -> Output:
control.logger.debug("State FINISHED")
Expand Down
4 changes: 4 additions & 0 deletions src/main/python/main/ayab/engine/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class Output(Enum):
ERROR_INVALID_SETTINGS = auto()
ERROR_SERIAL_PORT = auto()
CONNECTING_TO_MACHINE = auto()
DISCONNECTING_FROM_MACHINE = auto()
INITIALIZING_FIRMWARE = auto()
WAIT_FOR_INIT = auto()
ERROR_WRONG_API = auto()
Expand Down Expand Up @@ -65,6 +66,9 @@ def _none(self) -> None:
def _connecting_to_machine(self) -> None:
self.emit_notification("Connecting to machine...", False)

def _disconnecting_from_machine(self) -> None:
self.emit_notification("Disconnecting from machine...")

def _initializing_firmware(self) -> None:
self.emit_notification("Initializing firmware")

Expand Down