The parameter descriptions in the API reference are consistent with the comments in the @ohos.busManager.serial.d.ts file in the SDK.
The initial APIs of this module are supported since API version 19. Newly added APIs in later versions are marked with a superscript to indicate their initial API version.
This document provides error code descriptions for serial communication API calls, helping developers quickly locate and resolve serial communication issues. It applies to scenarios such as developing serial communication applications and debugging serial communication exceptions. Only the error codes specific to this module are described below. For general error codes, see Universal Error Codes.
| ID | Error Message | Triggering Interface |
|---|---|---|
| 35700001 | Service error. | getSerialPortList, open, close, write, onDataRead, offDataRead, flush, drain, setRts, getCts, getDsr, sendBrk, onDisconnect, offDisconnect, setDtr, addPortAuthorization |
| 35700002 | Invalid parameter. | open, write, addPortAuthorization |
| 35700003 | Virtual serial port disconnected. | open, write, onDataRead, flush, drain, setRts, getCts, sendBrk, getDsr, setDtr |
| 35700004 | Port already in use. | open |
| 35700005 | Port not open. | close, write, onDataRead, offDataRead, flush, drain, setRts, getCts, sendBrk, getDsr, setDtr, onDisconnect, offDisconnect |
| 35700006 | Transmission timeout. | write |
| 35700007 | User authorization required. | open |
| 35700008 | Permission denied. | addPortAuthorization |
Error Message
Service error.
Description
An internal exception (such as a service crash or insufficient resources) occurs in the serial port communication service when a serial communication-related interface is called.
Possible Causes
The serial port communication service is not started.
The serial port communication service process crashes or system resources are insufficient.
Solution
Check whether the serial port function of the device is normal.
Restart the device and try again.
Error Message
Invalid parameter.
Description
The input parameter is invalid when the serial port communication API is called.
Possible Causes
The baud rate is not within the supported range (a common baud rate such as 9600/19200/38400/57600/115200, or a positive integer in the range [1, 115200], in baud).
The length of the data to be written exceeds the range (0, 4096] (in bytes).
The timeout exceeds the range [0, 300000] (in ms).
The parameter type does not match the interface definition, for example, a string is passed when a number is expected.
Solution
Check whether the baud rate is a supported common baud rate (such as 9600/19200/38400/57600/115200) or a positive integer in the range [1, 115200], in baud.
Check whether the length of the data to be written is within the range (0, 4096] (in bytes).
Check whether the timeout parameter is within the range [0, 300000] (unit: ms).
Check whether the parameter types are correct.
Error Message
Virtual serial port disconnected.
Description
When a serial port communication API is called, the USB virtual serial port device is disconnected.
Possible Causes
The USB-to-serial port cable is removed.
The USB virtual serial port is disconnected abnormally.
Solution
Check whether the USB-to-serial cable is properly connected and ensure that the device connection is stable.
After re-plugging the USB device, call getSerialPortList again to obtain the device list, and then call open to open the device, so as to restore the serial communication function.
Error Message
Port already in use.
Description
When calling open to open a serial port, ensure that the serial port is not occupied by other applications or processes. Otherwise, an error is reported because the port is already in use.
Paired call description
Possible Causes
The same serial port is opened by another application.
The serial port is not properly disabled in the previous operation.
Solution
Close other applications that occupy the serial port.
Call close to close the serial port and then open it again.
Error Message
Port not open.
Description
The serial port is not opened when a serial port communication API is called.
State transition description
Possible Causes
The open interface is not called to open the serial port.
The serial port has been closed.
Solution
Call the open interface to open the serial port first.
Check whether the serial port is closed. If so, open it again.
Error Message
Transmission timeout.
Description
When the write interface is called to write data (ensure that the serial port is open), this error is reported if data transmission times out.
Prerequisites
Possible Causes
The waiting time of the write operation exceeds the configured timeout limit.
The receiving device does not respond.
The serial port communication parameters do not match.
Write requests are too frequent, causing congestion in the hardware buffer.
Solution
Check whether the receiving device is properly connected.
Increase the timeout parameter appropriately (unit: ms).
Check whether the serial port communication parameters (such as the baud rate) are consistent with those of the peer end.
When the serial port is open, reduce the write frequency or call the drain API between writes to wait until the previous data is sent before sending more data, so as to avoid congestion of the hardware buffer.
Using write and drain together
Error Message
User authorization required.
Description
When the open API is called to open a serial port, the system checks whether the user has authorized the application to access the target serial port. If not, this error code is returned.
Possible Causes
Solution
Error Message
Permission denied.
Description
When the serial.addPortAuthorization API is called, this API can be called only by the serial port authorization pop-up application (a preset application designated by the system to handle serial port device authorization pop-ups). If the current application is not the serial port authorization pop-up application, a permission denied error is reported.
Possible Causes
Solution