在模拟器上工作,但在Android手机上没有

时间:2013-09-05 12:03:29

标签: android nullpointerexception plc libnodave

我正在开发一个包含libnodave库的应用程序(与Siemens PLC连接)。我想简单地阅读一些数据并定期放入Android屏幕。应用程序正在模拟器上正常工作,但在真实设备上我得到 NullPointerException 。我认为我的问题来自Java语言的使用,但我不确定。我正在添加以下代码:

任何人都可以解释为什么会发生这种情况吗?

这是错误消息:

09-05 13:28:01.359: E/HAHAHA(16267): java.lang.NullPointerException
09-05 13:28:01.359: E/HAHAHA(16267):    at nodave.DataIsoTCP.ReadMyData(DataIsoTCP.java:83)
09-05 13:28:01.359: E/HAHAHA(16267):    at com.example.haus.EnglishScreen$2$1$1.run(EnglishScreen.java:95)
09-05 13:28:01.359: E/HAHAHA(16267):    at android.os.Handler.handleCallback(Handler.java:587)
09-05 13:28:01.359: E/HAHAHA(16267):    at android.os.Handler.dispatchMessage(Handler.java:92)
09-05 13:28:01.359: E/HAHAHA(16267):    at android.os.Looper.loop(Looper.java:130)
09-05 13:28:01.359: E/HAHAHA(16267):    at android.app.ActivityThread.main(ActivityThread.java:3687)
09-05 13:28:01.359: E/HAHAHA(16267):    at java.lang.reflect.Method.invokeNative(Native Method)
09-05 13:28:01.359: E/HAHAHA(16267):    at java.lang.reflect.Method.invoke(Method.java:507)
09-05 13:28:01.359: E/HAHAHA(16267):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
09-05 13:28:01.359: E/HAHAHA(16267):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
09-05 13:28:01.359: E/HAHAHA(16267):    at dalvik.system.NativeStart.main(Native Method)

这是EnglishScreen.java

import nodave.DataIsoTCP;
import android.app.Activity;
import android.content.Intent;
import android.opengl.Visibility;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class EnglishScreen extends Activity {
Button read;
Button back;
Button next;
TextView user;
TextView bowlValue;
TextView scrollValue;
TextView productValue;
TextView polymerValue;
TextView flushValue;
TextView automaticValue;
//Intent i=new Intent();
String logiName="admin";
Handler myHandler = new Handler();
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.english);
    user=(TextView)findViewById(R.id.user);

    //textviews
    bowlValue=(TextView)findViewById(R.id.bowlValue);
    scrollValue=(TextView)findViewById(R.id.scrollValue);
    productValue=(TextView)findViewById(R.id.productValue);
    polymerValue=(TextView)findViewById(R.id.polymerValue);
    flushValue=(TextView)findViewById(R.id.flushValue);
    automaticValue=(TextView)findViewById(R.id.automaticValue);

    read=(Button)findViewById(R.id.read);
    back=(Button)findViewById(R.id.back);
    next=(Button)findViewById(R.id.Next);

    next.setVisibility(View.INVISIBLE);
    try{
        Intent i = getIntent();
        String login=i.getStringExtra("login");
        //i.putExtra("login", login);
        user.setText(login);
        if(logiName.equals(login)){
            next.setVisibility(View.VISIBLE);
        }

    }
        catch(Exception e){}

    try{
        DataIsoTCP.ConnectIsoTcp("192.168.1.10");}
        catch(Exception e){}

next.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Intent setIntent = new Intent(getApplicationContext(),SetScreen.class);
        startActivity(setIntent);
    }
});

    read.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            new Thread(new Runnable() {
                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    while (true) {
                        try {

                            myHandler.post(new Runnable() {

                                @Override
                                public void run() {
                                    try {
                                        // TODO Auto-generated method stub
                                        // Write your code here to update the UI.
                                        //  bowlValue.setText(String.valueOf(DataIsoTCP.ReadMyData()));

                                        bowlValue.setText(String
                                                .valueOf(DataIsoTCP
                                                        .ReadMyData(3,
                                                                28,4)));
                                        //  scrollValue.setText(String.valueOf(DataIsoTCP.ReadMyFloatData(4,30)));
                                        //  productValue.setText(String.valueOf(DataIsoTCP.ReadMyFloatData(5,24)));
                                        //  polymerValue.setText(String.valueOf(DataIsoTCP.ReadMyFloatData(6,10)));
                                        //  flushValue.setText(String.valueOf(DataIsoTCP.ReadMyFloatData(7,24)));
                                        //  automaticValue.setText(String.valueOf(DataIsoTCP.ReadMyFloatData(8,24)));
                                    } catch (Exception e) {
                                        // TODO: handle exception
                                        Log.e("HAHAHA","exception",e);
                                    }
                                }
                            });
                            Thread.sleep(5000);
                        } catch (Exception e) {
                            // TODO: handle exception
                        }
                    }
                }
            }).start();
        }
    });
    //User name from main page


    //Back button function

    back.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            finish();
        }
    });
    }
}

这是DataIsoTCP.java:

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import android.R.bool;

public class DataIsoTCP {
public static boolean Connection = false;
public static int i, j;
public static long a, b, c;
public static float d, e, f;
public static char buf[];
public static byte buf1[];
public static PLCinterface di;
public static TCPConnection dc;
public static Socket sock;
public static int slot;
public static byte[] by;
public static String IP;

// IP 192.168.1.10
DataIsoTCP(String host) {
    IP = host;
    // Nodave.Debug=Nodave.DEBUG_ALL;
    buf = new char[Nodave.OrderCodeSize];
    buf1 = new byte[Nodave.PartnerListSize];
    try {
        sock = new Socket(host, 102);
    } catch (IOException e) {
        System.out.println(e);
    }
}

public static void StartConnection() {
    Connection = false;
    OutputStream oStream = null;
    InputStream iStream = null;
    slot = 2;

    if (sock != null) {
        try {
            oStream = sock.getOutputStream();
        } catch (IOException e) {
        }
        try {
            iStream = sock.getInputStream();
        } catch (IOException e) {
        }
        di = new PLCinterface(oStream, iStream, "IF1", 0,
                Nodave.PROTOCOL_ISOTCP);

        dc = new TCPConnection(di, 0, slot);
        int res = dc.connectPLC();
        if (0 == res) {
            Connection = true;
            System.out.println("Connection OK ");
        } else {
            System.out.println("No connection");
        }
    }
}

public static void StopConnection() {
    if (Connection == true) {
        Connection = false;
        dc.disconnectPLC();
        di.disconnectAdapter();
    }
}

// read 4 bytes from MD 100
public static long ReadData() {
    dc.readBytes(Nodave.FLAGS, 0, 100, 4, null);
    a = dc.getU32();
    return (long) a;
}
public static int ReadMyData(int db,int bol,int lenght){
    //dc = new TCPConnection(di, 0, slot);

    dc.readBytes(Nodave.DB, db, bol, lenght,null);
    a=dc.getU32();
    return (int)a;
} 

public static float ReadMyFloatData(int db,int bol){
        dc = new TCPConnection(di, 0, slot);
        dc.readBytes(Nodave.DB, db, bol, 4, null);
        d = dc.getFloat();

    return (float) d;
}

// write 4 bytes to MD 100
public static void WriteData(long a) {
    by = Nodave.bswap_32(a);
    dc.writeBytes(Nodave.FLAGS, 0, 100, 4, by);

}
public static void WriteMyData(long a){
    by=Nodave.bswap_32(a);
    dc.writeBytes(Nodave.DB, 4, 0, 100, by);
}
public static void WriteMyFloatData(float deger,int db,int bol){

        try {
            by = Nodave.toPLCfloat(deger);
            dc.writeBytes(Nodave.DB, db, bol, 4, by);
        } catch (Exception e) {
            // TODO: handle exception
        }

}
public static void WriteThat(int onOff){
    byte x=0;
    if(onOff==1){
         x=(byte) (x | (1<<1));
    }
    else
    {
        x=(byte)(x & ~(1<<1));
    }
    dc.writeBytes(Nodave.FLAGS, 0, 1, 1, by);
}

public static void ConnectIsoTcp(String adres) {

    Nodave.Debug = Nodave.DEBUG_ALL
            ^ (Nodave.DEBUG_IFACE | Nodave.DEBUG_SPECIALCHARS);

    DataIsoTCP tp = new DataIsoTCP(adres);
    tp.StartConnection();
}
}

在Manifest文件中我放

<uses-permission android:name="android.permission.INTERNET"/>
<activity android:name="com.example.haus.EnglishScreen"></activity>

TCPConnection.java:

package nodave;

public class TCPConnection extends S7Connection {

int rack;
int slot;

public TCPConnection(PLCinterface ifa, int rack, int slot) {
    super(ifa);
    this.rack = rack;
    this.slot = slot;
    PDUstartIn = 7;
    PDUstartOut = 7;
}

protected int readISOPacket() {
    int res = iface.read(msgIn, 0, 4);
    if (res == 4) {
        int len = 0x100 * msgIn[2] + msgIn[3];
        res += iface.read(msgIn, 4, len);
    } else {
        return 0;
    }
    return res;
}

protected int sendISOPacket(int size) {
    size += 4;
    msgOut[0] = (byte) 0x03;
    msgOut[1] = (byte) 0x0;
    msgOut[2] = (byte) (size / 0x100);
    msgOut[3] = (byte) (size % 0x100);
    if ((Nodave.Debug & Nodave.DEBUG_EXCHANGE) != 0) {
        Nodave.dump(" send packet", msgOut, 0, size);
    }
   // iface = new PLCinterface();  // i put it there
    iface.write(msgOut, 0, size);
    return 0;
}

public int exchange(PDU p1) {
    int res;
    PDU p2;
    if ((Nodave.Debug & Nodave.DEBUG_EXCHANGE) != 0) {
        System.out.println(" enter TCP.Exchange");
    }
    msgOut[4] = (byte) 0x02;
    msgOut[5] = (byte) 0xf0;
    msgOut[6] = (byte) 0x80;
    sendISOPacket(3 + p1.hlen + p1.plen + p1.dlen);
    res = readISOPacket();
    return 0;
}

/**
 * We have our own connectPLC(), but no disconnect() Open connection to a
 * PLC. This assumes that dc is initialized by daveNewConnection and is not
 * yet used. (or reused for the same PLC ?)
 */
public int connectPLC() {
    byte[] b4 = {
        (byte) 0x11,
        (byte) 0xE0,
        (byte) 0x00,
        (byte) 0x00,
        (byte) 0x00,
        (byte) 0x01,
        (byte) 0x00,
        (byte) 0xC1,
        (byte) 0x02,
        (byte) 0x01,
        (byte) 0x00,
        (byte) 0xC2,
        (byte) 0x02,
        (byte) 0x01,
        (byte) 0x02,
        (byte) 0xC0,
        (byte) 0x01,
        (byte) 0x09};

    if ((Nodave.Debug & Nodave.DEBUG_CONNECT) != 0) {
        System.out.println("daveConnectPLC() step 1. rack:" + rack + " slot:" + slot);
    }
    System.arraycopy(b4, 0, msgOut, 4, b4.length);
    msgOut[17] = (byte) (rack + 1);
    msgOut[18] = (byte) slot;
    sendISOPacket(b4.length);
    readISOPacket();
    if ((Nodave.Debug & Nodave.DEBUG_CONNECT) != 0) {
        System.out.println("daveConnectPLC() step 1.");
    }
    return negPDUlengthRequest();
}
}

0 个答案:

没有答案