> For the complete documentation index, see [llms.txt](https://smartpossdk.gitbook.io/cloudpossdk/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://smartpossdk.gitbook.io/cloudpossdk/faq/usb-serial-port/close-serial-after-disconnect.md).

# Close Serial after Disconnect

### Issue Identification:

During the operation of an application that reads from a serial port, if an error message stating "Level 3 halted" appears in the log, it typically indicates that the serial port cable has been disconnected.

### Recommended Solution:

1. **Immediate Action upon Error Detection:**
   * Upon encountering this error, the application should immediately close the serial port.
2. **Attempt to Reopen the Serial Port:**
   * After closing, the application should try to reopen the serial port.
3. **Handling Persistent Connection Issues:**
   * If the application fails to reopen the serial port, check and reconnect the serial port cable as necessary.

### Java SDK Implementation:

* **Catching DeviceException during Serial Port Read:**
  * In the Java SDK, you can handle this situation by catching the DeviceException when performing serial port read operations.

### Code Snippet Example:

{% code overflow="wrap" lineNumbers="true" %}

```java
        try {
            byte[] readBytes = new byte[256];
            SerialPortOperationResult serialPortOperationResult = serialPortDevice.waitForRead(readBytes.length, TimeConstants.FOREVER);
            int resultCode = serialPortOperationResult.getResultCode();
            if (resultCode == SerialPortOperationResult.SUCCESS) {
                byte[] data = serialPortOperationResult.getData();
                Logger.debug("read success:" + new String(data));
            } else if (resultCode == SerialPortOperationResult.LEVEL_3_HALTED) {
                Logger.debug("devices is gone, please close it first, then open it again.");
                serialPortDevice.close();
            }
        } catch (DeviceException e) {
            e.printStackTrace();
        }
```

{% endcode %}

This approach ensures the application can effectively manage unexpected disconnections and attempts to re-establish the serial port connection.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://smartpossdk.gitbook.io/cloudpossdk/faq/usb-serial-port/close-serial-after-disconnect.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
