java in.available_java.io.IOException: No error in nativeavailable
时间: 2024-06-05 12:12:56 浏览: 27
这个异常通常是由于调用`InputStream`的`available()`方法时,底层的系统调用返回了一个错误码。这个错误码可能是`EBADF`(文件描述符不正确)或`EINVAL`(参数无效)等。
可能的原因是你的程序在使用`InputStream`时,底层的文件描述符出现了问题,或者在调用`available()`方法时传入了无效的参数。
你可以尝试重新打开文件或者网络连接,或者检查你的代码是否正确地使用了`InputStream`。如果问题仍然存在,可以尝试在操作系统级别上排除故障。
相关问题
java.io.IOException: Broken pipe
This error occurs when a program tries to write data to a closed or unreachable pipe/socket. In other words, the program is trying to send data to a destination that is no longer available or has been disconnected.
This error often occurs when a client program tries to communicate with a server program and the server program terminates unexpectedly or the connection between the client and server is lost. The client program may continue to send data to the server, but the server is no longer available to receive it, resulting in a broken pipe error.
To resolve this error, the program needs to handle the broken pipe exception and take appropriate action, such as closing the connection and notifying the user of the error. The program may also need to retry the operation or establish a new connection if possible.
java实现485通讯
要实现485通讯,需要使用Java串口通讯库和Java CRC校验库。下面是一个简单的Java程序示例,用于发送和接收485数据:
```java
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.Enumeration;
import gnu.io.CommPortIdentifier;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import gnu.io.UnsupportedCommOperationException;
public class SerialTest implements SerialPortEventListener {
private SerialPort serialPort;
private InputStream inputStream;
private OutputStream outputStream;
private static final String PORT_NAME = "COM1";// 串口名称
private static final int BAUD_RATE = 9600;// 波特率
private static final int DATA_BITS = 8;// 数据位
private static final int STOP_BITS = 1;// 停止位
private static final int PARITY = 0;// 校验位
/**
* 初始化串口
*/
public void init() {
CommPortIdentifier portIdentifier;
try {
portIdentifier = CommPortIdentifier.getPortIdentifier(PORT_NAME);
if (portIdentifier.isCurrentlyOwned()) {
System.out.println("Error: Port is currently in use");
} else {
serialPort = (SerialPort) portIdentifier.open(this.getClass().getName(), 2000);
serialPort.setSerialPortParams(BAUD_RATE, DATA_BITS, STOP_BITS, PARITY);
serialPort.addEventListener(this);
serialPort.notifyOnDataAvailable(true);
inputStream = serialPort.getInputStream();
outputStream = serialPort.getOutputStream();
}
} catch (PortInUseException | UnsupportedCommOperationException | IOException | gnu.io.NoSuchPortException e) {
e.printStackTrace();
}
}
/**
* 发送数据
*
* @param data 发送的数据
*/
public void sendData(byte[] data) {
try {
outputStream.write(data);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 接收数据
*/
@Override
public void serialEvent(SerialPortEvent event) {
switch (event.getEventType()) {
case SerialPortEvent.DATA_AVAILABLE:// 有数据到达
byte[] readBuffer = new byte[1024];
try {
while (inputStream.available() > 0) {
int numBytes = inputStream.read(readBuffer);
System.out.println("Received: " + Arrays.toString(readBuffer));
}
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
/**
* 关闭串口
*/
public void close() {
if (serialPort != null) {
serialPort.removeEventListener();
serialPort.close();
}
}
public static void main(String[] args) {
SerialTest test = new SerialTest();
test.init();// 初始化串口
byte[] data = new byte[] { 0x01, 0x02, 0x03, 0x04 };// 要发送的数据
test.sendData(data);// 发送数据
test.close();// 关闭串口
}
}
```
在上面的示例中,我们使用了RXTXcomm库来实现串口通讯。如果你使用的是其他串口通讯库,代码实现可能会有所不同。此外,需要注意485通讯需要在发送和接收数据时进行CRC校验。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)