我正在尝试使用Android设备(目前使用模拟器)与S7-1200 PLC进行通信。 我找到了示例程序,但我在使其工作方面遇到了问题。
这是主程序:
public class Avvio extends Activity {
private final static String newline = "\n";
public final static String PlcIpAddress = "10.9.15.58";
Button btnConnectRetreive, btnDelete;
TextView lblPlcAddress, txtRisultato, lblStatus, txtStatus;
TestISOTCP tp;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
settaLayout();
gestiscoEventi();
validaIndirizzoIp();
}
private void settaLayout() {
lblPlcAddress = (TextView) findViewById(R.id.lblPlcAddress);
btnConnectRetreive = (Button) findViewById(R.id.btnConnectRetreive);
btnDelete = (Button) findViewById(R.id.btnDelete);
lblStatus = (TextView) findViewById(R.id.lblStatus);
txtStatus = (TextView) findViewById(R.id.txtStatus);
txtRisultato = (TextView) findViewById(R.id.txtRisultato);
}
private void validaIndirizzoIp() {
ValidateIP validaIp = new ValidateIP();
if (validaIp.validate(PlcIpAddress)) {
btnConnectRetreive.setEnabled(true);
lblPlcAddress.setText("PLC Address: " + PlcIpAddress);
} else {
btnConnectRetreive.setEnabled(false);
}
}
private void gestiscoEventi() {
// Gestico Evento per il Bottone Collega e Leggi
// I handle event for the button Connect Retrive
btnConnectRetreive.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
PlcState.ErrPlc = false;
long timeOut;
timeOut = network.testConnection("10.9.15.58", 102, 20000);
if (timeOut > -1) {// connection is alive
PlcState.ErrPlc = false;
// System.out.println("time " +
// String.valueOf(timeOut));
txtStatus.setText("OK " + "timeout: " + String.valueOf(timeOut));
// Now re try to read and write
readSomeData();
} else {// connection is not alive
PlcState.ErrPlc = true;
txtStatus.setText("!!! " + "timeout: " + String.valueOf(timeOut));
}
}
});
// Gestisco Evento per cancellare i dati letti
// I handle event for the button delete result
btnDelete.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
txtRisultato.setText("");
}
});
}
private void readSomeData() {
try {
TestISOTCP.prepara();
txtRisultato.append("=======================" + newline);
txtRisultato.append("DBW1.DBB0 DoubleInt " + DB1.DBB0 + newline);
txtRisultato.append("DBW1.DBB1 DoubleInt " + DB1.DBB1 + newline);
txtRisultato.append("MD0 DoubleInt " + Merker.MD0 + newline);
txtRisultato.append("MD1 DoubleInt " + Merker.MD4 + newline);
txtRisultato.append("MD2 DoubleInt " + Merker.MD8 + newline);
txtRisultato.append("MD3 Float " + Merker.MD12 + newline);
} catch (Exception e) {
PlcState.ErrPlc = true;
Toast.makeText(this, "Errore" + e, Toast.LENGTH_SHORT).show();
}
}
}
这是TestISOTCP类:
public class TestISOTCP
{
static boolean doWrite = true;
static int useProtocol = Nodave.PROTOCOL_ISOTCP;
static int slot = 2;
public int i, j;
public char buf[];
public byte buf1[];
public PLCinterface di;
public TCPConnection dc;
public Socket sock;
TestISOTCP(String host) {
buf = new char[Nodave.OrderCodeSize];
buf1 = new byte[Nodave.PartnerListSize];
try
{
System.out.println("Sono nel costruttore");
sock = new Socket(host, 102);
} catch (SocketException e)
{
System.out.println("Errore con soket in costruttore " + e);
e.printStackTrace();
} catch (IOException e)
{
System.out.println("Errore in costruttore " + e);
e.printStackTrace();
}
System.out.println("Esco dal costruttore");
}
public static void prepara()
{
// Plc Address to connect
TestISOTCP tp = new TestISOTCP("10.9.15.58");
tp.runSomething();
}
void runSomething()
{
OutputStream oStream = null;
InputStream iStream = null;
System.out.println("sono in run2");
byte[] by;
if (sock != null)
{
try
{
oStream = sock.getOutputStream();
} catch (IOException e)
{
PlcState.ErrPlc = true;
System.out
.println("oStream = sock.getOutputStream() "
+ e);
}
try
{
iStream = sock.getInputStream();
} catch (IOException e)
{
PlcState.ErrPlc = true;
System.out
.println("iStream = sock.getInputStream() "
+ e);
}
di = new PLCinterface(oStream, iStream, "IF1", 0,
Nodave.PROTOCOL_ISOTCP);
dc = new TCPConnection(di, 0, slot);
int res = dc.connectPLC();
if (0 == res)// Here we Read from PLC
{
System.out
.println("Read flags");
dc.readBytes(Nodave.FLAGS, 1, 0, 1, null);// Read
// DataBlok
DB1.DBB0 = dc.getBYTE();
System.out.println("DB1:DW0:" + DB1.DBB0);
//DB1.DBB2 = dc.getWORD();
//System.out.println("DB1:DW1: " + DB1.DBB2);
/*
System.out
.println("Trying to read 16 bytes from FW0.\n");
dc.readBytes(Nodave.FLAGS, 0, 0, 16, null);// Read
// Flag
Merker.MD0 = dc.getU32();
Merker.MD4 = dc.getU32();
Merker.MD8 = dc.getU32();
Merker.MD12 = dc.getFloat();
System.out.println("4 DWORDS " + Merker.MD0 + " "
+ Merker.MD4 + " " + Merker.MD8);
System.out.println("1 Float: " + Merker.MD12);
if (doWrite)// Here we write from PLC
{
System.out
.println("Now we write back these data after incrementing the first 3 by 1,2,3 and the float by 1.1.\n");
by = Nodave.bswap_32(Merker.MD0 + 1);
dc.writeBytes(Nodave.FLAGS, 0, 0, 4, by);
by = Nodave.bswap_32(Merker.MD4 + 1);
dc.writeBytes(Nodave.FLAGS, 0, 4, 4, by);
by = Nodave.bswap_32(Merker.MD8 + 1);
dc.writeBytes(Nodave.FLAGS, 0, 8, 4, by);
by = Nodave.toPLCfloat(Merker.MD12 + 1.1);
dc.writeBytes(Nodave.FLAGS, 0, 12, 4, by);
dc.readBytes(Nodave.FLAGS, 0, 0, 16, null);
Merker.MD0 = dc.getU32();
Merker.MD4 = dc.getU32();
Merker.MD8 = dc.getU32();
Merker.MD12 = dc.getFloat();
System.out.println("FD0: " + Merker.MD0);
System.out.println("FD4:" + Merker.MD4);
System.out.println("FD8:" + Merker.MD8);
System.out.println("FD12: " + Merker.MD12);
} // doWrite
*/
System.out.println("Now disconnecting\n");
dc.disconnectPLC();
di.disconnectAdapter();
} else
{
PlcState.ErrPlc = true;
System.out
.println("Couldn't connect to PLC. Error:"
+ res);
}
} else
{
PlcState.ErrPlc = true;
System.out.println("Couldn't open connection");
}
}
}
连接似乎正常工作我可以ping它,第一部分代码显示连接状态确定。
但是,每个返回值等于0,我确定我在PLC中设置它。 我还在PLC中启用了webserver功能(我理解这是必要的)。
答案 0 :(得分:0)
您的问题是1200插槽中的插槽= s7-300中的插槽= 400插槽= 2