简体   繁体   English

通过串行端口java.net.SocketException的通信:软件导致连接中止:套接字写入错误

[英]Communication through serial port, java.net.SocketException: Software caused connection abort: socket write error

I have written code which helps my device to communicate using serial port. 我已经编写了代码,可以帮助我的设备使用串行端口进行通信。 But after writing some lines the code sends the exception. 但是在写了几行之后,代码发送了异常。 And i do not know why it is doing this. 而且我不知道为什么要这样做。

The code i have written is as under: 我写的代码如下:

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.SocketException;
import javax.comm.SerialPort;
import javax.comm.UnsupportedCommOperationException;
import org.dellroad.jvser.TelnetSerialPort;
import org.apache.commons.net.telnet.TelnetClient;
public class InclinometerCommunicator 
{

private static final int DEFAULT_TCP_PORT = 10001;

private static final byte ADDRESS = (byte) 0xB1;
private static final byte ENQUIRY = 5;

private static final int PACKET_LENGTH = 11;

private TelnetSerialPort port; 
public static final int BAUD_RATE = 9600;
public static final int DATA_BITS = SerialPort.DATABITS_8;
public static final int PARITY_BITS = SerialPort.PARITY_NONE;
public static final int STOP_BITS = SerialPort.STOPBITS_1;
public static final int FLOW_CONTROL = SerialPort.FLOWCONTROL_NONE;

public InclinometerCommunicator(InetAddress host) throws UnsupportedCommOperationException, SocketException, IOException {
    this(host, DEFAULT_TCP_PORT);
}

public InclinometerCommunicator(InetAddress host, int tcpPort) throws UnsupportedCommOperationException, SocketException, IOException {
    port = new TelnetSerialPort();
    port.setSerialPortParams(BAUD_RATE, DATA_BITS, STOP_BITS, PARITY_BITS);
    port.setFlowControlMode(FLOW_CONTROL);
    port.setDTR(true);
    port.setRTS(false);
    port.getTelnetClient().connect(host, tcpPort);


}

public float getAngle() throws IOException, InterruptedException 
    {
    sendFlowControl();
    Thread.sleep(100);
    sendEnquiry();
    Thread.sleep(200);
    receiveFlowControl();
    Thread.sleep(200);
    byte[] packet = readPacket();

    return parsePacket(packet);
    //return (float)1.5;
}

private void sendFlowControl() {
    port.setDTR(false);
    port.setRTS(true);
}

private void sendEnquiry() throws IOException {
    OutputStream out = port.getOutputStream();
    out.write(new byte[]{ADDRESS, ENQUIRY});
    out.flush();

}

private void receiveFlowControl() {
    port.setRTS(false);
    port.setDTR(true);
}

private byte[] readPacket() throws IOException {
    InputStream in = port.getInputStream();
    byte[] buf = new byte[PACKET_LENGTH];
    int totalRead = 0;
    int i = 0;

    while (totalRead < PACKET_LENGTH && i < 100) {
        totalRead += in.read(buf, totalRead, PACKET_LENGTH - totalRead);

        i++;
    }
    return buf;
}

private float parsePacket(byte[] packet) {
    //TODO add additional checking
    /*
    for(byte b: packet)
        System.out.print(b+" ");
    System.out.print("\n");
    */
    return (float) ((100*atoi(packet[1])) + (10*atoi(packet[2])) + atoi(packet[3]) + (.1*atoi(packet[5])) + (.01*atoi(packet[6])));
}

private int atoi(byte a) {
    return (byte) (a - '0');
}
}

And the other class is here: 另一个班级在这里:

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.Socket;

public class QService {

private InclinometerCommunicator communicator;


public static void main(String[] args) {
    QService rc = new QService();
    rc.run("10.168.217.106");
}

public void run(String ip) 
{

    try 
    {

        communicator = new InclinometerCommunicator(InetAddress.getByName(ip), 9999);
    } 
    catch (Exception e) 
    {
        e.printStackTrace();
    }

    while (true) 
    {
            float angle ;

        try 
        {


            angle = communicator.getAngle();
            System.out.println("Angle:" + angle);
            Thread.sleep(1000);


        } 
        catch (Exception e) 
        {
            System.out.println("Exception"+"::"+e); // Exception coming here
            e.printStackTrace();
        }

    }
}

} }

And the output is 输出是

Angle:-670.48
Angle:7118.36
Angle:367.57
Angle:7351.34
Angle:3094.42
Angle:-1599.83
Angle:527.55
Angle:7119.96
Angle:3857.8
Angle:209.53
Exception::java.net.SocketException: Software caused connection abort: socket write error
java.net.SocketException: Software caused connection abort: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(Unknown Source)
at java.net.SocketOutputStream.write(Unknown Source)
at java.io.BufferedOutputStream.flushBuffer(Unknown Source)
at java.io.BufferedOutputStream.flush(Unknown Source)
at org.dellroad.jvser.telnet.TelnetClient._flushOutputStream(TelnetClient.java:81)
at org.dellroad.jvser.telnet.TelnetOutputStream.flush(TelnetOutputStream.java:146)
at InclinometerCommunicator.sendEnquiry(InclinometerCommunicator.java:66)
at InclinometerCommunicator.getAngle(InclinometerCommunicator.java:48)
at QService.run(QService.java:38)
at QService.main(QService.java:14)

This error is originating at the network layer, not the application layer. 此错误起源于网络层,而不是应用程序层。 Looks like the underlying TCP connection is getting disconnected, perhaps due to the other side closing it unexpectedly. 看起来底层的TCP连接正在断开连接,可能是由于另一端意外关闭了它。 Check the logs on the other end. 检查另一端的日志。 A tcpdump (or Wireshark) packet trace will verify what's going on at the TCP level. tcpdump (或Wireshark)数据包跟踪将验证TCP级别的情况。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在FTP中存储ObjectOutputStream导致java.net.SocketException:软件导致连接中止:套接字写入错误 - Store ObjectOutputStream in FTP causes java.net.SocketException: Software caused connection abort: socket write error java.net.SocketException:软件导致连接中止:重新提交请求时套接字写入错误 - java.net.SocketException: Software caused connection abort: socket write error when resubmitting the request BufferedReader.readLine()给出错误java.net.SocketException:软件导致连接中止:recv失败 - BufferedReader.readLine() gives error java.net.SocketException: Software caused connection abort: recv failed java.net.SocketException:软件导致连接中止:Java 8 recv失败 - java.net.SocketException: Software caused connection abort: recv failed with Java 8 java.net.SocketException:软件导致连接中止:recv失败; 原因和治疗方法? - java.net.SocketException: Software caused connection abort: recv failed; Causes and cures? java.net.SocketException:软件导致连接中止:使用其他jre安全性时recv失败 - java.net.SocketException: Software caused connection abort: recv failed when using different jre security JnrpeClient:java.net.SocketException:软件导致连接中止:recv失败 - JnrpeClient : java.net.SocketException: Software caused connection abort: recv failed 软件导致连接中止:套接字写入错误 - Software caused connection abort: socket write error Java SocketException:软件导致连接中止 - Java SocketException: Software caused connection abort 在与线程的套接字通信中获取 java.net.SocketException 连接重置错误 - Getting java.net.SocketException Connection reset error in socket communication with threads
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM