How to Manage Serial Port Disconnections in Application Development: Difference between revisions
(Created page with " When app read from serial port, if occurs the error message: "Level 3 halted" in the log, that means the serial port cable is disconnected, then close the serial port in...") |
No edit summary |
||
Line 3: | Line 3: | ||
In Java SDK, you can catch the DeviceException when do serail port read, snippet code: | In Java SDK, you can catch the DeviceException when do serail port read, snippet code: | ||
<syntaxhighlight lang="java"> | <syntaxhighlight lang="java"> | ||
try{ | 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(); | |||
} | |||
</syntaxhighlight > | </syntaxhighlight > |
Revision as of 07:38, 22 July 2022
When app read from serial port, if occurs the error message: "Level 3 halted" in the log, that means the serial port cable is disconnected, then close the serial port in the app, then try to open it again. If app can not open serial port, please try to connect the serial port cable again. In Java SDK, you can catch the DeviceException when do serail port read, snippet code:
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();
}