Click here to Skip to main content
15,867,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a program which is written by the man who is not here. Now the problem is that the program throws the exception after writing some lines.

The code is as follows:

Class: InclinometerCommunicator.java
Java
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 this:

QService.java


import java.net.InetAddress;

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 is coming here"+e); // Here is the place where exception is coming.

        }

    }
 }
}
And the output is as follows:
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
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:67)
at InclinometerCommunicator.getAngle(InclinometerCommunicator.java:50)
at QService.run(QService.java:30)
at QService.main(QService.java:9)
Posted
Updated 23-Apr-14 22:06pm
v2
Comments
Shubhashish_Mandal 24-Apr-14 4:16am    
Somehow socket time out happened. Check the default socket timeout and reset as you needed.
Nosheen Javed 24-Apr-14 5:37am    
But i am not setting its timeout time in the code. I do not know how can I set that? As this code was written by another developer and he is not here now.
TorstenH. 24-Apr-14 8:24am    
check it. That one has a setting by default and might be set too low.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900