private int connetti() { int i, a = 0, j, res, b = 0, c = 0; float d = 0; byte[] buf1 = new byte[libnodave.davePartnerListSize]; fds.rfd = libnodave.openS7online(param.s7online); fds.wfd = fds.rfd; if (fds.rfd >= 0) { di = new libnodave.daveInterface(fds, param.IF, param.localMPI, param.useProto, param.speed); di.setTimeout(param.timeOut); for (i = 0; i < 3; i++) { if (0 == di.initAdapter()) { initSuccess = true; a = di.listReachablePartners(buf1); plc.setTextln("daveListReachablePartners List length: " + a); if (a > 0) { for (j = 0; j < a; j++) { if (buf1[j] == libnodave.daveMPIReachable) { plc.setTextln("Device at address: " + j); } } } break; } else { di.disconnectAdapter(); } } if (!initSuccess) { plc.setTextln("Couldn't connect to Adapter!.\n Please try again. You may also try the option -2 for some adapters."); return(-3); } dc = new libnodave.daveConnection(di, param.plcMPI, 0, 0); return(dc.connectPLC()); } else { plc.setTextln("Couldn't open s7 online "); return(-1); } }
public static int Main(string[] args) { int i, a = 0, j, res, b = 0, c = 0; float d = 0; fds.rfd = libnodave.setPort(args[0], "38400", 'O'); fds.wfd = fds.rfd; if (fds.rfd > 0) { di = new libnodave.daveInterface(fds, "IF1", localMPI, libnodave.daveProtoMPI, libnodave.daveSpeed187k); di.setTimeout(1000000); res = di.initAdapter(); if (res == 0) { dc = new libnodave.daveConnection(di, plcMPI, 0, 0); if (0 == dc.connectPLC()) { res = dc.readBytes(libnodave.daveFlags, 0, 0, 16, null); if (res == 0) { a = dc.getS32(); b = dc.getS32(); c = dc.getS32(); d = dc.getFloat(); Console.WriteLine("FD0: " + a); Console.WriteLine("FD4: " + b); Console.WriteLine("FD8: " + c); Console.WriteLine("FD12: " + d); } else { Console.WriteLine("error " + res + " " + libnodave.daveStrerror(res)); } } dc.disconnectPLC(); } di.disconnectAdapter(); libnodave.closePort(fds.rfd); } else { Console.WriteLine("Couldn't open serial port " + args[0]); return(-1); } return(0); }
public bool ConnectPLC() { try { daveSerial.rfd = libnodave.openSocket(plcConf.Port, plcConf.IP); daveSerial.wfd = daveSerial.rfd; if (daveSerial.rfd > 0) { daveInterface = new libnodave.daveInterface(daveSerial, "", 0, libnodave.daveProtoISOTCP, libnodave.daveSpeed187k); //daveInterface.setTimeout(1000000); if (0 == daveInterface.initAdapter()) { daveConnection = new libnodave.daveConnection(daveInterface, 0, plcConf.Rack, plcConf.Slot); if (0 == daveConnection.connectPLC()) { return(true); } else { daveConnection.disconnectPLC(); libnodave.closeSocket(daveSerial.rfd); return(false); } } else { daveInterface.disconnectAdapter(); libnodave.closeSocket(daveSerial.rfd); return(false); } } else { return(false); } } catch (Exception ex) { Console.WriteLine(ex.Message); return(false); } }
public PlcS7Access(String ip, Int32 port, Int32 rack, Int32 slot, Int32 timeOut, String name = "FN1") { if (CheckArguments(ip, port, rack, slot)) { _ip = ip; _port = port; _rack = rack; _slot = slot; _timeOut = timeOut; _name = name; _serialType.rfd = libnodave.openSocket(_port, _ip); _serialType.wfd = _serialType.rfd; if (_serialType.rfd == 0) { SendPlcException(DEFAULTERRORCODE, ResPlcAccess.SocketNotOpen); } _daveInterface = new libnodave.daveInterface(_serialType, _name, LOCALMPI, libnodave.daveProtoISOTCP, libnodave.daveSpeed187k); if (_daveInterface == null) { SendPlcException(DEFAULTERRORCODE, ResPlcAccess.DaveInterfaceIsNoll); } _daveInterface.setTimeout(_timeOut); int errorCode = _daveInterface.initAdapter(); if (errorCode != 0) { SendPlcException(errorCode); } _state = PlcConnectionState.Disconnect; } else { _state = PlcConnectionState.Error; } }
private void button1_Click(object sender, EventArgs e) { fds.rfd = libnodave.openSocket(102, textBox2.Text); fds.wfd = fds.rfd; di = new libnodave.daveInterface(fds, "IF1", 0, libnodave.daveProtoISOTCP, libnodave.daveSpeed187k); di.setTimeout(1000); res = di.initAdapter(); try { dc = new libnodave.daveConnection(di, 0, 0, 1); res = dc.connectPLC(); MessageBox.Show("连接成功"); } catch (Exception) { MessageBox.Show("连接失败"); } }
private void button1_Click(object sender, EventArgs e) { fds.rfd = libnodave.openSocket(102, IP); fds.wfd = fds.rfd; if (fds.rfd > 0) { di = new libnodave.daveInterface(fds, "IF1", localMPI, useProto, speed); di.setTimeout(5000000); if (0 == di.initAdapter()) { initSuccess = 1; dc = new libnodave.daveConnection(di, plcMPI, rack, slot); int ret = dc.connectPLC(); if (ret == 0) { Connection = true; this.lblstatus.Text = "已经连接PLC" + IP; } } } }
public static int Main(string[] args) { int i,a=0,j,res,b=0,c=0; float d=0; fds.rfd=libnodave.setPort(args[0],"38400",'O'); fds.wfd=fds.rfd; if (fds.rfd>0) { di =new libnodave.daveInterface(fds, "IF1", localMPI, libnodave.daveProtoMPI, libnodave.daveSpeed187k); di.setTimeout(1000000); res=di.initAdapter(); if(res==0) { dc = new libnodave.daveConnection(di,plcMPI, 0, 0); if (0==dc.connectPLC()) { res=dc.readBytes(libnodave.daveFlags, 0, 0, 16, null); if (res==0) { a=dc.getS32(); b=dc.getS32(); c=dc.getS32(); d=dc.getFloat(); Console.WriteLine("FD0: " + a); Console.WriteLine("FD4: " + b); Console.WriteLine("FD8: " + c); Console.WriteLine("FD12: " + d); } else Console.WriteLine("error "+res+" "+libnodave.daveStrerror(res)); } dc.disconnectPLC(); } di.disconnectAdapter(); libnodave.closePort(fds.rfd); } else { Console.WriteLine("Couldn't open serial port "+args[0]); return -1; } return 0; }
public static int Main(string[] args) { int i, a = 0, j, res, b = 0, c = 0; float d = 0; byte[] buf1 = new byte[libnodave.davePartnerListSize]; if (args.Length < 1) { usage(); return(-1); } while (args[adrPos][0] == '-') { if (args[adrPos].StartsWith("--debug=")) { libnodave.daveSetDebug(Convert.ToInt32(args[adrPos].Substring(8))); Console.WriteLine("setting debug to: ", Convert.ToInt32(args[adrPos].Substring(8))); } else if (args[adrPos].StartsWith("-d")) { libnodave.daveSetDebug(libnodave.daveDebugAll); } else if (args[adrPos].StartsWith("-s")) { doStop = true; } else if (args[adrPos].StartsWith("-w")) { doWrite = true; } else if (args[adrPos].StartsWith("-b")) { doBenchmark = true; } else if (args[adrPos].StartsWith("--readoutall")) { doReadout = true; doSFBandSFC = true; } else if (args[adrPos].StartsWith("--readout")) { doReadout = true; } else if (args[adrPos].StartsWith("-r")) { doRun = true; } else if (args[adrPos].StartsWith("-e")) { doExperimental = true; } else if (args[adrPos].StartsWith("--local=")) { localMPI = Convert.ToInt32(args[adrPos].Substring(8)); Console.WriteLine("setting local MPI address to: " + localMPI); } else if (args[adrPos].StartsWith("--mpi=")) { plcMPI = Convert.ToInt32(args[adrPos].Substring(6)); Console.WriteLine("setting MPI address of PLC to: " + plcMPI); } else if (args[adrPos].StartsWith("--mpi2=")) { plc2MPI = Convert.ToInt32(args[adrPos].Substring(7)); Console.WriteLine("setting MPI address of 2md PLC to: " + plc2MPI); } else if (args[adrPos].StartsWith("--wbit=")) { wbit = Convert.ToInt32(args[adrPos].Substring(7)); Console.WriteLine("setting bit number: " + wbit); doWbit = true; } else if (args[adrPos].StartsWith("-z")) { doSZLread = true; } else if (args[adrPos].StartsWith("-a")) { doSZLreadAll = true; } else if (args[adrPos].StartsWith("-m")) { doMultiple = true; } else if (args[adrPos].StartsWith("-c")) { doClear = true; } else if (args[adrPos].StartsWith("-n")) { doNewfunctions = true; } else if (args[adrPos].StartsWith("-9")) { speed = libnodave.daveSpeed9k; } else if (args[adrPos].StartsWith("-19")) { speed = libnodave.daveSpeed19k; } else if (args[adrPos].StartsWith("-45")) { speed = libnodave.daveSpeed45k; } else if (args[adrPos].StartsWith("-93")) { speed = libnodave.daveSpeed93k; } else if (args[adrPos].StartsWith("-500")) { speed = libnodave.daveSpeed500k; } else if (args[adrPos].StartsWith("-1500")) { speed = libnodave.daveSpeed1500k; } adrPos++; if (args.Length <= adrPos) { usage(); return(-1); } } fds.rfd = libnodave.openS7online(args[adrPos]); fds.wfd = fds.rfd; if (fds.rfd >= 0) { di = new libnodave.daveInterface(fds, "IF1", localMPI, useProto, speed); di.setTimeout(5000000); for (i = 0; i < 3; i++) { if (0 == di.initAdapter()) { initSuccess = 1; a = di.listReachablePartners(buf1); Console.WriteLine("daveListReachablePartners List length: " + a); if (a > 0) { for (j = 0; j < a; j++) { if (buf1[j] == libnodave.daveMPIReachable) { Console.WriteLine("Device at address: " + j); } } } break; } else { di.disconnectAdapter(); } } if (initSuccess == 0) { Console.WriteLine("Couldn't connect to Adapter!.\n Please try again. You may also try the option -2 for some adapters."); return(-3); } dc = new libnodave.daveConnection(di, plcMPI, 0, 0); if (0 == dc.connectPLC()) { ; res = dc.readBytes(libnodave.daveFlags, 0, 0, 16, null); if (res == 0) { a = dc.getS32(); b = dc.getS32(); c = dc.getS32(); d = dc.getFloat(); Console.WriteLine("FD0: " + a); Console.WriteLine("FD4: " + b); Console.WriteLine("FD8: " + c); Console.WriteLine("FD12: " + d); } else { Console.WriteLine("error " + res + " " + libnodave.daveStrerror(res)); } if (doExperimental) { Console.WriteLine("Trying to read outputs"); res = dc.readBytes(libnodave.daveOutputs, 0, 0, 2, null); Console.WriteLine("function result: " + res + "=" + libnodave.daveStrerror(res) + " " + dc.getAnswLen()); if (res == 0) { Console.Write("Bytes:"); for (b = 0; b < dc.getAnswLen(); b++) { c = dc.getU8(); Console.Write(String.Format(" {0:X0}, ", c)); } Console.WriteLine(""); } a = 0x01; Console.WriteLine("Trying to write outputs"); res = dc.writeBytes(libnodave.daveOutputs, 0, 0, 1, BitConverter.GetBytes(a)); Console.WriteLine("function result: " + res + "=" + libnodave.daveStrerror(res) + " " + dc.getAnswLen()); libnodave.daveSetDebug(libnodave.daveDebugAll); res = dc.force200(libnodave.daveOutputs, 0, 0); Console.WriteLine("function result: " + res + "=" + libnodave.daveStrerror(res) + " " + dc.getAnswLen()); libnodave.daveSetDebug(0); res = dc.force200(libnodave.daveOutputs, 0, 1); Console.WriteLine("function result of force: " + res + "=" + libnodave.daveStrerror(res) + " " + dc.getAnswLen()); wait(); res = dc.force200(libnodave.daveOutputs, 0, 2); Console.WriteLine("function result of force: " + res + "=" + libnodave.daveStrerror(res) + " " + dc.getAnswLen()); wait(); res = dc.force200(libnodave.daveOutputs, 0, 3); Console.WriteLine("function result of force: " + res + "=" + libnodave.daveStrerror(res) + " " + dc.getAnswLen()); wait(); res = dc.force200(libnodave.daveOutputs, 1, 4); Console.WriteLine("function result of force: " + res + "=" + libnodave.daveStrerror(res) + " " + dc.getAnswLen()); wait(); res = dc.force200(libnodave.daveOutputs, 2, 5); Console.WriteLine("function result of force: " + res + "=" + libnodave.daveStrerror(res) + " " + dc.getAnswLen()); wait(); res = dc.force200(libnodave.daveOutputs, 3, 7); Console.WriteLine("function result of force: " + res + "=" + libnodave.daveStrerror(res) + " " + dc.getAnswLen()); wait(); Console.WriteLine("Trying to read outputs again\n"); res = dc.readBytes(libnodave.daveOutputs, 0, 0, 4, null); Console.WriteLine("function result: " + res + "=" + libnodave.daveStrerror(res) + " " + dc.getAnswLen()); if (res == 0) { Console.Write("Bytes:"); for (b = 0; b < dc.getAnswLen(); b++) { c = dc.getU8(); Console.Write(String.Format(" {0:X0}, ", c)); } Console.WriteLine(""); } } if (doWrite) { Console.WriteLine("Now we write back these data after incrementing the integers by 1,2 and 3 and the float by 1.1.\n"); wait(); /* * Attention! you need to daveSwapIed little endian variables before using them as a buffer for * daveWriteBytes() or before copying them into a buffer for daveWriteBytes()! */ a = libnodave.daveSwapIed_32(a + 1); dc.writeBytes(libnodave.daveFlags, 0, 0, 4, BitConverter.GetBytes(a)); b = libnodave.daveSwapIed_32(b + 2); dc.writeBytes(libnodave.daveFlags, 0, 4, 4, BitConverter.GetBytes(b)); c = libnodave.daveSwapIed_32(c + 3); dc.writeBytes(libnodave.daveFlags, 0, 8, 4, BitConverter.GetBytes(c)); d = libnodave.toPLCfloat(d + 1.1f); dc.writeBytes(libnodave.daveFlags, 0, 12, 4, BitConverter.GetBytes(d)); /* * Read back and show the new values, so users may notice the difference: */ dc.readBytes(libnodave.daveFlags, 0, 0, 16, null); a = dc.getU32(); b = dc.getU32(); c = dc.getU32(); d = dc.getFloat(); Console.WriteLine("FD0: " + a); Console.WriteLine("FD4: " + b); Console.WriteLine("FD8: " + c); Console.WriteLine("FD12: " + d); } // doWrite if (doClear) { Console.WriteLine("Now writing 0 to the bytes FB0...FB15.\n"); // wait(); byte[] aa = { 0, 0, 0, 0 }; dc.writeBytes(libnodave.daveFlags, 0, 0, 4, aa); dc.writeBytes(libnodave.daveFlags, 0, 4, 4, aa); dc.writeBytes(libnodave.daveFlags, 0, 8, 4, aa); dc.writeBytes(libnodave.daveFlags, 0, 12, 4, aa); dc.readBytes(libnodave.daveFlags, 0, 0, 16, null); a = dc.getU32(); b = dc.getU32(); c = dc.getU32(); d = dc.getFloat(); Console.WriteLine("FD0: " + a); Console.WriteLine("FD4: " + b); Console.WriteLine("FD8: " + c); Console.WriteLine("FD12: " + d); } // doClear if (doSZLread) { readSZL(dc, 0x92, 0x0); readSZL(dc, 0xB4, 0x1024); readSZL(dc, 0x111, 0x1); readSZL(dc, 0xD91, 0x0); readSZL(dc, 0x232, 0x4); readSZL(dc, 0x1A0, 0x0); readSZL(dc, 0x0A0, 0x0); } if (doSZLreadAll) { readSZLAll(dc); } if (doStop) { dc.stop(); } if (doRun) { dc.start(); } if (doBenchmark) { rBenchmark(dc, libnodave.daveFlags); } if (doNewfunctions) { int saveDebug = libnodave.daveGetDebug(); Console.WriteLine("Trying to read two consecutive bits from DB11.DBX0.1");; res = dc.readBits(libnodave.daveDB, 11, 1, 2, null); Console.WriteLine("function result:" + res + "=" + libnodave.daveStrerror(res)); Console.WriteLine("Trying to read no bit (length 0) from DB17.DBX0.1"); res = dc.readBits(libnodave.daveDB, 17, 1, 0, null); Console.WriteLine("function result:" + res + "=" + libnodave.daveStrerror(res)); libnodave.daveSetDebug(libnodave.daveGetDebug() | libnodave.daveDebugPDU); Console.WriteLine("Trying to read a single bit from DB17.DBX0.3\n"); res = dc.readBits(libnodave.daveDB, 17, 3, 1, null); Console.WriteLine("function result:" + res + "=" + libnodave.daveStrerror(res)); Console.WriteLine("Trying to read a single bit from E0.2\n"); res = dc.readBits(libnodave.daveInputs, 0, 2, 1, null); Console.WriteLine("function result:" + res + "=" + libnodave.daveStrerror(res)); a = 0; Console.WriteLine("Writing 0 to EB0\n"); res = dc.writeBytes(libnodave.daveOutputs, 0, 0, 1, BitConverter.GetBytes(libnodave.daveSwapIed_32(a))); Console.WriteLine("function result:" + res + "=" + libnodave.daveStrerror(res)); a = 1; Console.WriteLine("Trying to set single bit E0.5\n"); res = dc.writeBits(libnodave.daveOutputs, 0, 5, 1, BitConverter.GetBytes(libnodave.daveSwapIed_32(a))); Console.WriteLine("function result:" + res + "=" + libnodave.daveStrerror(res)); Console.WriteLine("Trying to read 1 byte from AAW0\n"); res = dc.readBytes(libnodave.daveAnaIn, 0, 0, 2, null); Console.WriteLine("function result:" + res + "=" + libnodave.daveStrerror(res)); a = 2341; Console.WriteLine("Trying to write 1 word (2 bytes) to AAW0\n"); res = dc.writeBytes(libnodave.daveAnaOut, 0, 0, 2, BitConverter.GetBytes(libnodave.daveSwapIed_32(a))); Console.WriteLine("function result:" + res + "=" + libnodave.daveStrerror(res)); Console.WriteLine("Trying to read 4 items from Timers\n"); res = dc.readBytes(libnodave.daveTimer, 0, 0, 4, null); Console.WriteLine("function result:" + res + "=" + libnodave.daveStrerror(res)); if (res == 0) { d = dc.getSeconds(); Console.WriteLine("Time: %0.3f, ", d); d = dc.getSeconds(); Console.WriteLine("%0.3f, ", d); d = dc.getSeconds(); Console.WriteLine("%0.3f, ", d); d = dc.getSeconds(); Console.WriteLine(" %0.3f\n", d); d = dc.getSecondsAt(0); Console.WriteLine("Time: %0.3f, ", d); d = dc.getSecondsAt(2); Console.WriteLine("%0.3f, ", d); d = dc.getSecondsAt(4); Console.WriteLine("%0.3f, ", d); d = dc.getSecondsAt(6); Console.WriteLine(" %0.3f\n", d); } Console.WriteLine("Trying to read 4 items from Counters\n"); res = dc.readBytes(libnodave.daveCounter, 0, 0, 4, null); Console.WriteLine("function result:" + res + "=" + libnodave.daveStrerror(res)); if (res == 0) { c = dc.getCounterValue(); Console.WriteLine("Count: %d, ", c); c = dc.getCounterValue(); Console.WriteLine("%d, ", c); c = dc.getCounterValue(); Console.WriteLine("%d, ", c); c = dc.getCounterValue(); Console.WriteLine(" %d\n", c); c = dc.getCounterValueAt(0); Console.WriteLine("Count: %d, ", c); c = dc.getCounterValueAt(2); Console.WriteLine("%d, ", c); c = dc.getCounterValueAt(4); Console.WriteLine("%d, ", c); c = dc.getCounterValueAt(6); Console.WriteLine(" %d\n", c); } libnodave.PDU p = dc.prepareReadRequest(); p.addVarToReadRequest(libnodave.daveInputs, 0, 0, 1); p.addVarToReadRequest(libnodave.daveFlags, 0, 0, 4); p.addVarToReadRequest(libnodave.daveDB, 6, 20, 2); p.addVarToReadRequest(libnodave.daveTimer, 0, 0, 4); p.addVarToReadRequest(libnodave.daveTimer, 0, 1, 4); p.addVarToReadRequest(libnodave.daveTimer, 0, 2, 4); p.addVarToReadRequest(libnodave.daveCounter, 0, 0, 4); p.addVarToReadRequest(libnodave.daveCounter, 0, 1, 4); p.addVarToReadRequest(libnodave.daveCounter, 0, 2, 4); libnodave.resultSet rs = new libnodave.resultSet(); res = dc.execReadRequest(p, rs); libnodave.daveSetDebug(saveDebug); } // System.GarbageCollect(); if (doMultiple) { Console.WriteLine("Now testing read multiple variables.\n" + "This will read 1 Byte from inputs,\n" + "4 bytes from flags, 2 bytes from DB6,\n" + "and other 2 bytes from flags"); wait(); libnodave.PDU p = dc.prepareReadRequest(); p.addVarToReadRequest(libnodave.daveInputs, 0, 0, 1); p.addVarToReadRequest(libnodave.daveFlags, 0, 0, 4); p.addVarToReadRequest(libnodave.daveDB, 6, 20, 2); p.addVarToReadRequest(libnodave.daveFlags, 0, 12, 2); p.addBitVarToReadRequest(libnodave.daveFlags, 0, 25 /* 25 is 3.1*/, 1); libnodave.resultSet rs = new libnodave.resultSet(); res = dc.execReadRequest(p, rs); Console.Write("Input Byte 0: "); res = dc.useResult(rs, 0); // first result if (res == 0) { a = dc.getU8(); Console.WriteLine(a); } else { Console.WriteLine("*** Error: " + libnodave.daveStrerror(res)); } Console.Write("Flag DWord 0: "); res = dc.useResult(rs, 1); // 2nd result if (res == 0) { a = dc.getS16(); Console.WriteLine(a); } else { Console.WriteLine("*** Error: " + libnodave.daveStrerror(res)); } Console.Write("DB 6 Word 20: "); res = dc.useResult(rs, 2); // 3rd result if (res == 0) { a = dc.getS16(); Console.WriteLine(a); } else { Console.WriteLine("*** Error: " + libnodave.daveStrerror(res)); } Console.Write("Flag Word 12: "); res = dc.useResult(rs, 3); // 4th result if (res == 0) { a = dc.getU16(); Console.WriteLine(a); } else { Console.WriteLine("*** Error: " + libnodave.daveStrerror(res)); } Console.Write("Flag F3.1: "); res = dc.useResult(rs, 4); // 4th result if (res == 0) { a = dc.getU8(); Console.WriteLine(a); } else { Console.WriteLine("*** Error: " + libnodave.daveStrerror(res)); } Console.Write("non existing result (we read 5 items, but try to use a 6th one): "); res = dc.useResult(rs, 5); // 5th result if (res == 0) { a = dc.getU16(); Console.WriteLine(a); } else { Console.WriteLine("*** Error: " + libnodave.daveStrerror(res)); } if (doWrite) { Console.WriteLine("Now testing write multiple variables:\n" + "IB0, FW0, QB0, DB6:DBW20 and DB20:DBD24 in a single multiple write."); wait(); // libnodave.daveSetDebug(0xffff); byte[] aa = { 0 }; libnodave.PDU p2 = dc.prepareWriteRequest(); p2.addVarToWriteRequest(libnodave.daveInputs, 0, 0, 1, aa); p2.addVarToWriteRequest(libnodave.daveFlags, 0, 4, 2, aa); p2.addVarToWriteRequest(libnodave.daveOutputs, 0, 0, 2, aa); p2.addVarToWriteRequest(libnodave.daveDB, 6, 20, 2, aa); p2.addVarToWriteRequest(libnodave.daveDB, 20, 24, 4, aa); aa[0] = 1; p2.addBitVarToWriteRequest(libnodave.daveFlags, 0, 27 /* 27 is 3.3*/, 1, aa); rs = new libnodave.resultSet(); res = dc.execWriteRequest(p2, rs); Console.WriteLine("Result code for the entire multiple write operation: " + res + "=" + libnodave.daveStrerror(res)); /* * // I could list the single result codes like this, but I want to tell * // which item should have been written, so I do it in 5 individual lines: * * for (i=0;i<rs.numResults;i++){ * res=rs.results[i].error; * Console.WriteLine("result code from writing item %d: %d=%s\n",i,res,libnodave.libnodave.daveStrerror(res)); * } */ int err = rs.getErrorOfResult(0); Console.WriteLine("Result code for writing IB0: " + err + "=" + libnodave.daveStrerror(err)); err = rs.getErrorOfResult(1); Console.WriteLine("Result code for writing FW4: " + err + "=" + libnodave.daveStrerror(err)); err = rs.getErrorOfResult(2); Console.WriteLine("Result code for writing QB0: " + err + "=" + libnodave.daveStrerror(err)); err = rs.getErrorOfResult(3); Console.WriteLine("Result code for writing DB6:DBW20: " + err + "=" + libnodave.daveStrerror(err)); err = rs.getErrorOfResult(4); Console.WriteLine("Result code for writing DB20:DBD24:" + err + "=" + libnodave.daveStrerror(err)); err = rs.getErrorOfResult(5); Console.WriteLine("Result code for writing F3.3: " + err + "=" + libnodave.daveStrerror(err)); /* * Read back and show the new values, so users may notice the difference: */ dc.readBytes(libnodave.daveFlags, 0, 0, 16, null); a = dc.getU32(); b = dc.getU32(); c = dc.getU32(); d = dc.getFloat(); Console.WriteLine("FD0: %d\n", a); Console.WriteLine("FD4: %d\n", b); Console.WriteLine("FD8: %d\n", c); Console.WriteLine("FD12: %f\n", d); } // doWrite } dc.disconnectPLC(); } di.disconnectAdapter(); libnodave.closeS7online(fds.rfd); GC.Collect(); GC.WaitForPendingFinalizers(); Console.WriteLine("Here we are"); } else { Console.WriteLine("Couldn't open s7 online " + args[adrPos]); return(-1); } return(0); }
public static int Main(string[] args) { int i,a=0,j,res,b=0,c=0; float d=0; byte[] buf1=new byte[libnodave.davePartnerListSize]; if (args.Length <1) { usage(); return -1; } while (args[adrPos][0]=='-') { if (args[adrPos].StartsWith("--debug=")) { libnodave.daveSetDebug(Convert.ToInt32(args[adrPos].Substring(8))); Console.WriteLine("setting debug to: ",Convert.ToInt32(args[adrPos].Substring(8))); } else if (args[adrPos].StartsWith("-d")) { libnodave.daveSetDebug(libnodave.daveDebugAll); } else if (args[adrPos].StartsWith("-s")) { doStop=true; } else if (args[adrPos].StartsWith("-w")) { doWrite=true; } else if (args[adrPos].StartsWith("-b")) { doBenchmark=true; } else if (args[adrPos].StartsWith("--readoutall")) { doReadout=true; doSFBandSFC=true; } else if (args[adrPos].StartsWith("--readout")) { doReadout=true; } else if (args[adrPos].StartsWith("-r")) { doRun=true; } else if (args[adrPos].StartsWith("-e")) { doExperimental=true; } else if (args[adrPos].StartsWith("--local=")) { localMPI=Convert.ToInt32(args[adrPos].Substring(8)); Console.WriteLine("setting local MPI address to: "+localMPI); } else if (args[adrPos].StartsWith("--mpi=")) { plcMPI=Convert.ToInt32(args[adrPos].Substring(6)); Console.WriteLine("setting MPI address of PLC to: "+plcMPI); } else if (args[adrPos].StartsWith("--mpi2=")) { plc2MPI=Convert.ToInt32(args[adrPos].Substring(7)); Console.WriteLine("setting MPI address of 2md PLC to: "+plc2MPI); } else if (args[adrPos].StartsWith("--wbit=")) { wbit=Convert.ToInt32(args[adrPos].Substring(7)); Console.WriteLine("setting bit number: "+wbit); doWbit=true; } else if (args[adrPos].StartsWith("-z")) { doSZLread=true; } else if (args[adrPos].StartsWith("-a")) { doSZLreadAll=true; } else if (args[adrPos].StartsWith("-m")) { doMultiple=true; } else if (args[adrPos].StartsWith("-c")) { doClear=true; } else if (args[adrPos].StartsWith("-n")) { doNewfunctions=true; } else if (args[adrPos].StartsWith("-2")) { useProto=libnodave.daveProtoMPI2; } else if (args[adrPos].StartsWith("-3")) { useProto=libnodave.daveProtoMPI3; } else if (args[adrPos].StartsWith("-9")) { speed=libnodave.daveSpeed9k; } else if (args[adrPos].StartsWith("-19")) { speed=libnodave.daveSpeed19k; } else if (args[adrPos].StartsWith("-45")) { speed=libnodave.daveSpeed45k; } else if (args[adrPos].StartsWith("-93")) { speed=libnodave.daveSpeed93k; } else if (args[adrPos].StartsWith("-500")) { speed=libnodave.daveSpeed500k; } else if (args[adrPos].StartsWith("-1500")) { speed=libnodave.daveSpeed1500k; } adrPos++; if (args.Length<=adrPos) { usage(); return -1; } } fds.rfd=libnodave.setPort(args[adrPos],"38400",'O'); fds.wfd=fds.rfd; if (fds.rfd>0) { di =new libnodave.daveInterface(fds, "IF1", localMPI, useProto, speed); di.setTimeout(5000000); for (i=0; i<3; i++) { if (0==di.initAdapter()) { initSuccess=1; // a= libnodave.daveListReachablePartners(di,buf1); a= di.listReachablePartners(buf1); Console.WriteLine("daveListReachablePartners List length: "+a); if (a>0) { for (j=0;j<a;j++) { if (buf1[j]==libnodave.daveMPIReachable) Console.WriteLine("Device at address: "+j); } } break; } else di.disconnectAdapter(); } if (initSuccess==0) { Console.WriteLine("Couldn't connect to Adapter!.\n Please try again. You may also try the option -2 for some adapters."); return -3; } dc = new libnodave.daveConnection(di,plcMPI,0,0); if (0==dc.connectPLC()) {; res=dc.readBytes(libnodave.daveFlags, 0, 0, 16, null); if (res==0) { a=dc.getS32(); b=dc.getS32(); c=dc.getS32(); d=dc.getFloat(); Console.WriteLine("FD0: " + a); Console.WriteLine("FD4: " + b); Console.WriteLine("FD8: " + c); Console.WriteLine("FD12: " + d); } else Console.WriteLine("error "+res+" "+libnodave.daveStrerror(res)); if(doExperimental) { Console.WriteLine("Trying to read outputs"); res=dc.readBytes(libnodave.daveOutputs, 0, 0, 2, null); Console.WriteLine("function result: "+res+"="+libnodave.daveStrerror(res)+" "+dc.getAnswLen()); if (res==0) { Console.Write("Bytes:"); for (b=0; b<dc.getAnswLen(); b++) { c=dc.getU8(); Console.Write(String.Format(" {0:X0}, ",c)); } Console.WriteLine(""); } a=0x01; Console.WriteLine("Trying to write outputs"); res=dc.writeBytes(libnodave.daveOutputs, 0, 0, 1, BitConverter.GetBytes(a)); Console.WriteLine("function result: "+res+"="+libnodave.daveStrerror(res)+" "+dc.getAnswLen()); libnodave.daveSetDebug(libnodave.daveDebugAll); res=dc.force200(libnodave.daveOutputs,0,0); Console.WriteLine("function result: "+res+"="+libnodave.daveStrerror(res)+" "+dc.getAnswLen()); libnodave.daveSetDebug(0); res=dc.force200(libnodave.daveOutputs,0,1); Console.WriteLine("function result of force: "+res+"="+libnodave.daveStrerror(res)+" "+dc.getAnswLen()); wait(); res=dc.force200(libnodave.daveOutputs,0,2); Console.WriteLine("function result of force: "+res+"="+libnodave.daveStrerror(res)+" "+dc.getAnswLen()); wait(); res=dc.force200(libnodave.daveOutputs,0,3); Console.WriteLine("function result of force: "+res+"="+libnodave.daveStrerror(res)+" "+dc.getAnswLen()); wait(); res=dc.force200(libnodave.daveOutputs,1,4); Console.WriteLine("function result of force: "+res+"="+libnodave.daveStrerror(res)+" "+dc.getAnswLen()); wait(); res=dc.force200(libnodave.daveOutputs,2,5); Console.WriteLine("function result of force: "+res+"="+libnodave.daveStrerror(res)+" "+dc.getAnswLen()); wait(); res=dc.force200(libnodave.daveOutputs,3,7); Console.WriteLine("function result of force: "+res+"="+libnodave.daveStrerror(res)+" "+dc.getAnswLen()); wait(); Console.WriteLine("Trying to read outputs again\n"); res=dc.readBytes(libnodave.daveOutputs, 0, 0, 4, null); Console.WriteLine("function result: "+res+"="+libnodave.daveStrerror(res)+" "+dc.getAnswLen()); if (res==0) { Console.Write("Bytes:"); for (b=0; b<dc.getAnswLen(); b++) { c=dc.getU8(); Console.Write(String.Format(" {0:X0}, ",c)); } Console.WriteLine(""); } } if(doWrite) { Console.WriteLine("Now we write back these data after incrementing the integers by 1,2 and 3 and the float by 1.1.\n"); wait(); /* Attention! you need to daveSwapIed little endian variables before using them as a buffer for daveWriteBytes() or before copying them into a buffer for daveWriteBytes()! */ a=libnodave.daveSwapIed_32(a+1); dc.writeBytes(libnodave.daveFlags,0,0,4,BitConverter.GetBytes(a)); b=libnodave.daveSwapIed_32(b+2); dc.writeBytes(libnodave.daveFlags,0,4,4,BitConverter.GetBytes(b)); c=libnodave.daveSwapIed_32(c+3); dc.writeBytes(libnodave.daveFlags,0,8,4,BitConverter.GetBytes(c)); d=libnodave.toPLCfloat(d+1.1f); dc.writeBytes(libnodave.daveFlags,0,12,4,BitConverter.GetBytes(d)); /* * Read back and show the new values, so users may notice the difference: */ dc.readBytes(libnodave.daveFlags,0,0,16, null); a=dc.getU32(); b=dc.getU32(); c=dc.getU32(); d=dc.getFloat(); Console.WriteLine("FD0: "+a); Console.WriteLine("FD4: "+b); Console.WriteLine("FD8: "+c); Console.WriteLine("FD12: "+d); } // doWrite if(doClear) { Console.WriteLine("Now writing 0 to the bytes FB0...FB15.\n"); // wait(); byte[] aa={0,0,0,0}; dc.writeBytes(libnodave.daveFlags,0,0,4,aa); dc.writeBytes(libnodave.daveFlags,0,4,4,aa); dc.writeBytes(libnodave.daveFlags,0,8,4,aa); dc.writeBytes(libnodave.daveFlags,0,12,4,aa); dc.readBytes(libnodave.daveFlags,0,0,16, null); a=dc.getU32(); b=dc.getU32(); c=dc.getU32(); d=dc.getFloat(); Console.WriteLine("FD0: "+a); Console.WriteLine("FD4: "+b); Console.WriteLine("FD8: "+c); Console.WriteLine("FD12: "+d); } // doClear if(doSZLread) { readSZL(dc,0x92,0x0); readSZL(dc,0xB4,0x1024); readSZL(dc,0x111,0x1); readSZL(dc,0xD91,0x0); readSZL(dc,0x232,0x4); readSZL(dc,0x1A0,0x0); readSZL(dc,0x0A0,0x0); } if(doSZLreadAll) { readSZLAll(dc); } if(doStop) { dc.stop(); } if(doRun) { dc.start(); } if(doBenchmark) { rBenchmark(dc,libnodave.daveFlags); } if(doReadout) { loadBlocksOfType(dc, libnodave.daveBlockType_OB, doReadout); loadBlocksOfType(dc, libnodave.daveBlockType_FC, doReadout); loadBlocksOfType(dc, libnodave.daveBlockType_FB, doReadout); loadBlocksOfType(dc, libnodave.daveBlockType_DB, doReadout); loadBlocksOfType(dc, libnodave.daveBlockType_SDB, doReadout); } if(doNewfunctions) { int saveDebug=libnodave.daveGetDebug(); Console.WriteLine("Trying to read two consecutive bits from DB11.DBX0.1");; res=dc.readBits(libnodave.daveDB, 11, 1, 2, null); Console.WriteLine("function result:" + res+ "="+libnodave.daveStrerror(res)); Console.WriteLine("Trying to read no bit (length 0) from DB17.DBX0.1"); res=dc.readBits(libnodave.daveDB, 17, 1, 0, null); Console.WriteLine("function result:" + res+ "="+libnodave.daveStrerror(res)); libnodave.daveSetDebug(libnodave.daveGetDebug()|libnodave.daveDebugPDU); Console.WriteLine("Trying to read a single bit from DB17.DBX0.3\n"); res=dc.readBits(libnodave.daveDB, 17, 3, 1, null); Console.WriteLine("function result:" + res+ "="+libnodave.daveStrerror(res)); Console.WriteLine("Trying to read a single bit from E0.2\n"); res=dc.readBits(libnodave.daveInputs, 0, 2, 1, null); Console.WriteLine("function result:" + res+ "="+libnodave.daveStrerror(res)); a=0; Console.WriteLine("Writing 0 to EB0\n"); res=dc.writeBytes(libnodave.daveOutputs, 0, 0, 1, BitConverter.GetBytes(libnodave.daveSwapIed_32(a))); Console.WriteLine("function result:" + res+ "="+libnodave.daveStrerror(res)); a=1; Console.WriteLine("Trying to set single bit E0.5\n"); res=dc.writeBits(libnodave.daveOutputs, 0, 5, 1, BitConverter.GetBytes(libnodave.daveSwapIed_32(a))); Console.WriteLine("function result:" + res+ "="+libnodave.daveStrerror(res)); Console.WriteLine("Trying to read 1 byte from AAW0\n"); res=dc.readBytes(libnodave.daveAnaIn, 0, 0, 2, null); Console.WriteLine("function result:" + res+ "="+libnodave.daveStrerror(res)); a=2341; Console.WriteLine("Trying to write 1 word (2 bytes) to AAW0\n"); res=dc.writeBytes(libnodave.daveAnaOut, 0, 0, 2, BitConverter.GetBytes(libnodave.daveSwapIed_32(a))); Console.WriteLine("function result:" + res+ "="+libnodave.daveStrerror(res)); Console.WriteLine("Trying to read 4 items from Timers\n"); res=dc.readBytes(libnodave.daveTimer, 0, 0, 4, null); Console.WriteLine("function result:" + res+ "="+libnodave.daveStrerror(res)); if(res==0) { d=dc.getSeconds(); Console.WriteLine("Time: %0.3f, ",d); d=dc.getSeconds(); Console.WriteLine("%0.3f, ",d); d=dc.getSeconds(); Console.WriteLine("%0.3f, ",d); d=dc.getSeconds(); Console.WriteLine(" %0.3f\n",d); d=dc.getSecondsAt(0); Console.WriteLine("Time: %0.3f, ",d); d=dc.getSecondsAt(2); Console.WriteLine("%0.3f, ",d); d=dc.getSecondsAt(4); Console.WriteLine("%0.3f, ",d); d=dc.getSecondsAt(6); Console.WriteLine(" %0.3f\n",d); } Console.WriteLine("Trying to read 4 items from Counters\n"); res=dc.readBytes(libnodave.daveCounter, 0, 0, 4, null); Console.WriteLine("function result:" + res+ "="+libnodave.daveStrerror(res)); if(res==0) { c=dc.getCounterValue(); Console.WriteLine("Count: %d, ",c); c=dc.getCounterValue(); Console.WriteLine("%d, ",c); c=dc.getCounterValue(); Console.WriteLine("%d, ",c); c=dc.getCounterValue(); Console.WriteLine(" %d\n",c); c=dc.getCounterValueAt(0); Console.WriteLine("Count: %d, ",c); c=dc.getCounterValueAt(2); Console.WriteLine("%d, ",c); c=dc.getCounterValueAt(4); Console.WriteLine("%d, ",c); c=dc.getCounterValueAt(6); Console.WriteLine(" %d\n",c); } libnodave.PDU p=dc.prepareReadRequest(); p.addVarToReadRequest(libnodave.daveInputs,0,0,1); p.addVarToReadRequest(libnodave.daveFlags,0,0,4); p.addVarToReadRequest(libnodave.daveDB,6,20,2); p.addVarToReadRequest(libnodave.daveTimer,0,0,4); p.addVarToReadRequest(libnodave.daveTimer,0,1,4); p.addVarToReadRequest(libnodave.daveTimer,0,2,4); p.addVarToReadRequest(libnodave.daveCounter,0,0,4); p.addVarToReadRequest(libnodave.daveCounter,0,1,4); p.addVarToReadRequest(libnodave.daveCounter,0,2,4); libnodave.resultSet rs=new libnodave.resultSet(); res=dc.execReadRequest(p, rs); libnodave.daveSetDebug(saveDebug); } // System.GarbageCollect(); if(doMultiple) { Console.WriteLine("Now testing read multiple variables.\n" +"This will read 1 Byte from inputs,\n" +"4 bytes from flags, 2 bytes from DB6,\n" +"and other 2 bytes from flags"); wait(); libnodave.PDU p=dc.prepareReadRequest(); p.addVarToReadRequest(libnodave.daveInputs,0,0,1); p.addVarToReadRequest(libnodave.daveFlags,0,0,4); p.addVarToReadRequest(libnodave.daveDB,6,20,2); p.addVarToReadRequest(libnodave.daveFlags,0,12,2); p.addBitVarToReadRequest(libnodave.daveFlags, 0, 25 /* 25 is 3.1*/, 1); libnodave.resultSet rs=new libnodave.resultSet(); res=dc.execReadRequest(p, rs); Console.Write("Input Byte 0: "); res=dc.useResult(rs, 0); // first result if (res==0) { a=dc.getU8(); Console.WriteLine(a); } else Console.WriteLine("*** Error: "+libnodave.daveStrerror(res)); Console.Write("Flag DWord 0: "); res=dc.useResult(rs, 1); // 2nd result if (res==0) { a=dc.getS16(); Console.WriteLine(a); } else Console.WriteLine("*** Error: "+libnodave.daveStrerror(res)); Console.Write("DB 6 Word 20: "); res=dc.useResult(rs, 2); // 3rd result if (res==0) { a=dc.getS16(); Console.WriteLine(a); } else Console.WriteLine("*** Error: "+libnodave.daveStrerror(res)); Console.Write("Flag Word 12: "); res=dc.useResult(rs, 3); // 4th result if (res==0) { a=dc.getU16(); Console.WriteLine(a); } else Console.WriteLine("*** Error: "+libnodave.daveStrerror(res)); Console.Write("Flag F3.1: "); res=dc.useResult(rs, 4); // 4th result if (res==0) { a=dc.getU8(); Console.WriteLine(a); } else Console.WriteLine("*** Error: "+libnodave.daveStrerror(res)); Console.Write("non existing result (we read 5 items, but try to use a 6th one): "); res=dc.useResult(rs, 5); // 5th result if (res==0) { a=dc.getU16(); Console.WriteLine(a); } else Console.WriteLine("*** Error: "+libnodave.daveStrerror(res)); if (doWrite){ Console.WriteLine("Now testing write multiple variables:\n" +"IB0, FW0, QB0, DB6:DBW20 and DB20:DBD24 in a single multiple write."); wait(); // libnodave.daveSetDebug(0xffff); byte[] aa={0}; libnodave.PDU p2=dc.prepareWriteRequest(); p2.addVarToWriteRequest(libnodave.daveInputs,0,0,1, aa); p2.addVarToWriteRequest(libnodave.daveFlags,0,4,2, aa); p2.addVarToWriteRequest(libnodave.daveOutputs,0,0,2, aa); p2.addVarToWriteRequest(libnodave.daveDB,6,20,2, aa); p2.addVarToWriteRequest(libnodave.daveDB,20,24,4, aa); aa[0]=1; p2.addBitVarToWriteRequest(libnodave.daveFlags, 0, 27 /* 27 is 3.3*/, 1, aa); rs =new libnodave.resultSet(); res=dc.execWriteRequest(p2, rs); Console.WriteLine("Result code for the entire multiple write operation: "+res+"="+libnodave.daveStrerror(res)); /* // I could list the single result codes like this, but I want to tell // which item should have been written, so I do it in 5 individual lines: for (i=0;i<rs.numResults;i++){ res=rs.results[i].error; Console.WriteLine("result code from writing item %d: %d=%s\n",i,res,libnodave.libnodave.daveStrerror(res)); } */ int err=rs.getErrorOfResult(0); Console.WriteLine("Result code for writing IB0: "+err+"="+libnodave.daveStrerror(err)); err=rs.getErrorOfResult(1); Console.WriteLine("Result code for writing FW4: "+err+"="+libnodave.daveStrerror(err)); err=rs.getErrorOfResult(2); Console.WriteLine("Result code for writing QB0: "+err+"="+libnodave.daveStrerror(err)); err=rs.getErrorOfResult(3); Console.WriteLine("Result code for writing DB6:DBW20: "+err+"="+libnodave.daveStrerror(err)); err=rs.getErrorOfResult(4); Console.WriteLine("Result code for writing DB20:DBD24:"+err+"="+libnodave.daveStrerror(err)); err=rs.getErrorOfResult(5); Console.WriteLine("Result code for writing F3.3: "+err+"="+libnodave.daveStrerror(err)); /* * Read back and show the new values, so users may notice the difference: */ dc.readBytes(libnodave.daveFlags,0,0,16, null); a=dc.getU32(); b=dc.getU32(); c=dc.getU32(); d=dc.getFloat(); Console.WriteLine("FD0: %d\n",a); Console.WriteLine("FD4: %d\n",b); Console.WriteLine("FD8: %d\n",c); Console.WriteLine("FD12: %f\n",d); } // doWrite } dc.disconnectPLC(); } di.disconnectAdapter(); GC.Collect(); GC.WaitForPendingFinalizers(); Console.WriteLine("Here we are"); } else { Console.WriteLine("Couldn't open serial port "+args[adrPos]); return -1; } return 0; }
private int connetti() { int i, a = 0, j, res, b = 0, c = 0; float d = 0; byte[] buf1 = new byte[libnodave.davePartnerListSize]; fds.rfd = libnodave.openS7online(param.s7online); fds.wfd = fds.rfd; if (fds.rfd >= 0) { di = new libnodave.daveInterface(fds, param.IF, param.localMPI, param.useProto, param.speed); di.setTimeout(param.timeOut); for (i = 0; i < 3; i++) { if (0 == di.initAdapter()) { initSuccess = true; a = di.listReachablePartners(buf1); plc.setTextln("daveListReachablePartners List length: " + a); if (a > 0) { for (j = 0; j < a; j++) { if (buf1[j] == libnodave.daveMPIReachable) plc.setTextln("Device at address: " + j); } } break; } else di.disconnectAdapter(); } if (!initSuccess) { plc.setTextln("Couldn't connect to Adapter!.\n Please try again. You may also try the option -2 for some adapters."); return -3; } dc = new libnodave.daveConnection(di, param.plcMPI, 0, 0); return dc.connectPLC(); } else { plc.setTextln("Couldn't open s7 online "); return -1; } }
public static int Main(string[] args) { int rack, slot; int DB = 0, DataByte = 0, len = 0; int res; int p0 = 0, p1 = 0; int area = 0; int bitnumber = 0; int IO = 0; var WriteBuffer = new byte[] { 0x00, 0x00, 0x00, 0x00 }; bool myBit = false; byte myByte = 0; System.Int16 myWord = 0; System.Int32 myDWord = 0; System.Single mySingle = 0.0f; Byte[] byteArray = { 0x00, 0x00, 0x00, 0x00 }; string str = ""; string Address = ""; string PLC = "255.255.255.255"; libnodave.daveSetDebug(libnodave.daveDebugRawRead); if (args.Length != 4 && args.Length != 5) { Console.WriteLine(""); Console.WriteLine("Usage:"); Console.WriteLine("S7PLCVar.exe [ipaddress] [rack] [slot] [address] [value]"); Console.WriteLine(""); Console.WriteLine("Examples:"); Console.WriteLine(" Read bit value from I7.3"); Console.WriteLine(" S7PLCVar.exe 172.24.40.191 0 3 I7.3"); Console.WriteLine(""); Console.WriteLine(" Read bit value from DB10.DBX14.5"); Console.WriteLine(" S7PLCVar.exe 172.24.40.191 0 3 DB10.DBX14.5"); Console.WriteLine(""); Console.WriteLine(" Write DWord value 1234 to DB10.DBD14"); Console.WriteLine(" S7PLCVar.exe 172.24.40.191 0 3 DB10.DBD14 1234"); Console.WriteLine(""); Console.WriteLine(" Write Output bit True to Q3.5"); Console.WriteLine(" S7PLCVar.exe 172.24.40.191 0 3 Q3.5 True"); Console.WriteLine(""); return(-2); } else { PLC = args[0]; rack = Convert.ToInt32(args[1]); slot = Convert.ToInt32(args[2]); Address = args[3].ToUpper(); if (args.Length == 5) { IO = OUTPUTS; } else { IO = INPUTS; } // *************************************************************************** // Datablocks // *************************************************************************** if (Address.Contains(".DBX")) { area = libnodave.daveDB; len = 1; p0 = 2; p1 = Address.IndexOf(".DBX") - p0; str = Address.Substring(p0, p1); DB = Convert.ToInt32(str); p0 = Address.IndexOf(".DBX") + 4; p1 = Address.Length - p0; str = Address.Substring(p0, p1); DataByte = Convert.ToInt32(Convert.ToDouble(str)); p0 = Address.Length - 1; p1 = 1; str = Address.Substring(p0, p1); bitnumber = Convert.ToInt32(str) + 1; if (IO == OUTPUTS) { myBit = Convert.ToBoolean(args[4]); } } else if (Address.Contains(".DBB")) { area = libnodave.daveDB; len = 1; p0 = 2; p1 = Address.IndexOf(".DBB") - p0; str = Address.Substring(p0, p1); DB = Convert.ToInt32(str); p0 = Address.IndexOf(".DBB") + 4; p1 = Address.Length - p0; str = Address.Substring(p0, p1); DataByte = Convert.ToInt32(str); if (IO == OUTPUTS) { myByte = Convert.ToByte(args[4]); } } else if (Address.Contains(".DBW")) { area = libnodave.daveDB; len = 2; p0 = 2; p1 = Address.IndexOf(".DBW") - p0; str = Address.Substring(p0, p1); DB = Convert.ToInt32(str); p0 = Address.IndexOf(".DBW") + 4; p1 = Address.Length - p0; str = Address.Substring(p0, p1); DataByte = Convert.ToInt32(str); if (IO == OUTPUTS) { myWord = Convert.ToInt16(args[4]); } } else if (Address.Contains(".DBD")) { area = libnodave.daveDB; len = 4; p0 = 2; p1 = Address.IndexOf(".DBD") - p0; str = Address.Substring(p0, p1); DB = Convert.ToInt32(str); p0 = Address.IndexOf(".DBD") + 4; p1 = Address.Length - p0; str = Address.Substring(p0, p1); DataByte = Convert.ToInt32(str); if (IO == OUTPUTS) { if (args[4].Contains(".")) { mySingle = Convert.ToSingle(args[4]); byteArray = BitConverter.GetBytes(mySingle); myDWord = BitConverter.ToInt32(byteArray, 0); } else { myDWord = Convert.ToInt32(args[4]); } } } // *************************************************************************** // INPUTS // *************************************************************************** else if (Address.Substring(0, 3).Contains("PIB")) { area = libnodave.daveInputs; len = 1; p0 = 3; p1 = Address.Length - p0; str = Address.Substring(p0, p1); DataByte = Convert.ToInt32(str); } else if (Address.Substring(0, 3).Contains("PIW")) { area = libnodave.daveInputs; len = 2; p0 = 3; p1 = Address.Length - p0; str = Address.Substring(p0, p1); DataByte = Convert.ToInt32(str); } else if (Address.Substring(0, 3).Contains("PID")) { area = libnodave.daveInputs; len = 4; p0 = 3; p1 = Address.Length - p0; str = Address.Substring(p0, p1); DataByte = Convert.ToInt32(str); } else if (Address.Substring(0, 2).Contains("IB")) { area = libnodave.daveInputs; len = 1; p0 = 2; p1 = Address.Length - p0; str = Address.Substring(p0, p1); DataByte = Convert.ToInt32(str); } else if (Address.Substring(0, 2).Contains("IW")) { area = libnodave.daveInputs; len = 2; p0 = 2; p1 = Address.Length - p0; str = Address.Substring(p0, p1); DataByte = Convert.ToInt32(str); } else if (Address.Substring(0, 2).Contains("ID")) { area = libnodave.daveInputs; len = 4; p0 = 2; p1 = Address.Length - p0; str = Address.Substring(p0, p1); DataByte = Convert.ToInt32(str); } else if (Address.Substring(0, 1).Contains("I")) { area = libnodave.daveInputs; p0 = 1; p1 = Address.IndexOf(".") - 1; str = Address.Substring(p0, p1); DataByte = Convert.ToInt32(str); p1 = p1 + 2; str = Address.Substring(p1, 1); bitnumber = Convert.ToInt32(str) + 1; len = 1; } // *************************************************************************** // OUTPUTS // *************************************************************************** else if (Address.Substring(0, 3).Contains("PQB")) { area = libnodave.daveOutputs; len = 1; p0 = 3; p1 = Address.Length - p0; str = Address.Substring(p0, p1); DataByte = Convert.ToInt32(str); myByte = Convert.ToByte(args[4]); } else if (Address.Substring(0, 3).Contains("PQW")) { area = libnodave.daveOutputs; len = 2; p0 = 3; p1 = Address.Length - p0; str = Address.Substring(p0, p1); DataByte = Convert.ToInt32(str); myWord = Convert.ToInt16(args[4]); } else if (Address.Substring(0, 3).Contains("PQD")) { area = libnodave.daveOutputs; len = 4; p0 = 3; p1 = Address.Length - p0; str = Address.Substring(p0, p1); DataByte = Convert.ToInt32(str); if (IO == OUTPUTS) { if (args[4].Contains(".")) { mySingle = Convert.ToSingle(args[4]); byteArray = BitConverter.GetBytes(mySingle); myDWord = BitConverter.ToInt32(byteArray, 0); } else { myDWord = Convert.ToInt32(args[4]); } } } else if (Address.Substring(0, 3).Contains("QB")) { area = libnodave.daveOutputs; len = 1; p0 = 2; p1 = Address.Length - p0; str = Address.Substring(p0, p1); DataByte = Convert.ToInt32(str); myByte = Convert.ToByte(args[4]); } else if (Address.Substring(0, 3).Contains("QW")) { area = libnodave.daveOutputs; len = 2; p0 = 2; p1 = Address.Length - p0; str = Address.Substring(p0, p1); DataByte = Convert.ToInt32(str); myWord = Convert.ToInt16(args[4]); } else if (Address.Substring(0, 3).Contains("QD")) { area = libnodave.daveOutputs; len = 4; p0 = 2; p1 = Address.Length - p0; str = Address.Substring(p0, p1); DataByte = Convert.ToInt32(str); if (IO == OUTPUTS) { if (args[4].Contains(".")) { mySingle = Convert.ToSingle(args[4]); byteArray = BitConverter.GetBytes(mySingle); myDWord = BitConverter.ToInt32(byteArray, 0); } else { myDWord = Convert.ToInt32(args[4]); } } } else if (Address.Substring(0, 1).Contains("Q")) { area = libnodave.daveOutputs; len = 1; p0 = 1; p1 = Address.IndexOf(".") - 1; str = Address.Substring(p0, p1); DataByte = Convert.ToInt32(str); p1 = p1 + 2; str = Address.Substring(p1, 1); bitnumber = Convert.ToInt32(str) + 1; myBit = Convert.ToBoolean(args[4]); } // *************************************************************************** // Merkers / Flags // *************************************************************************** else if (Address.Substring(0, 3).Contains("MB")) { area = libnodave.daveFlags; len = 1; p0 = 2; p1 = Address.Length - p0; str = Address.Substring(p0, p1); DataByte = Convert.ToInt32(str); if (IO == OUTPUTS) { myByte = Convert.ToByte(args[4]); } } else if (Address.Substring(0, 3).Contains("MW")) { area = libnodave.daveFlags; len = 2; p0 = 2; p1 = Address.Length - p0; str = Address.Substring(p0, p1); DataByte = Convert.ToInt32(str); if (IO == OUTPUTS) { myWord = Convert.ToInt16(args[4]); } } else if (Address.Substring(0, 3).Contains("MD")) { area = libnodave.daveFlags; len = 4; p0 = 2; p1 = Address.Length - p0; str = Address.Substring(p0, p1); DataByte = Convert.ToInt32(str); if (IO == OUTPUTS) { if (args[4].Contains(".")) { mySingle = Convert.ToSingle(args[4]); byteArray = BitConverter.GetBytes(mySingle); myDWord = BitConverter.ToInt32(byteArray, 0); } else { myDWord = Convert.ToInt32(args[4]); } } } else if (Address.Substring(0, 1).Contains("M")) { area = libnodave.daveFlags; len = 1; p0 = 1; p1 = Address.IndexOf(".") - 1; str = Address.Substring(p0, p1); DataByte = Convert.ToInt32(str); p1 = p1 + 2; str = Address.Substring(p1, 1); bitnumber = Convert.ToInt32(str) + 1; if (IO == OUTPUTS) { myBit = Convert.ToBoolean(args[4]); } } // *************************************************************************** else if (Address.Substring(0, 1).Contains("C")) { area = libnodave.daveCounter; } else if (Address.Substring(0, 1).Contains("T")) { area = libnodave.daveTimer; } fds.rfd = libnodave.openSocket(102, PLC); fds.wfd = fds.rfd; if (fds.rfd > 0) { di = new libnodave.daveInterface(fds, "IF1", 0, libnodave.daveProtoISOTCP, libnodave.daveSpeed187k); di.setTimeout(1000000); res = di.initAdapter(); if (res == 0) { dc = new libnodave.daveConnection(di, 0, rack, slot); if (0 == dc.connectPLC()) { if (IO == 0) { Console.WriteLine("Onbekende Input of Output modus."); return(-3); } if (IO == OUTPUTS) { if (area == libnodave.daveDB || area == libnodave.daveOutputs || area == libnodave.daveFlags) { if (bitnumber > 0) { res = dc.readBytes(area, DB, DataByte, len, null); if (res == 0) { // Eerst Byte inlezen myByte = (byte)dc.getU8(); // Daarna Bitje in Byte aanpassen. if (myBit) { myByte = Convert.ToByte(Convert.ToInt16(myByte) + Convert.ToInt16(System.Math.Pow(2, bitnumber - 1))); } else { myByte = Convert.ToByte(myByte & ~(1 << (bitnumber - 1))); } } } if (len == 1) { WriteBuffer[0] = myByte; res = dc.writeBytes(area, DB, DataByte, len, WriteBuffer); } else if (len == 2) { WriteBuffer[1] = Convert.ToByte(myWord & 0xff); WriteBuffer[0] = Convert.ToByte((myWord >> (8 * 1)) & 0xff); res = dc.writeBytes(area, DB, DataByte, len, WriteBuffer); } else if (len == 4) { WriteBuffer[3] = Convert.ToByte(myDWord & 0xff); WriteBuffer[2] = Convert.ToByte((myDWord >> (8 * 1)) & 0xff); WriteBuffer[1] = Convert.ToByte((myDWord >> (8 * 2)) & 0xff); WriteBuffer[0] = Convert.ToByte((myDWord >> (8 * 3)) & 0xff); res = dc.writeBytes(area, DB, DataByte, len, WriteBuffer); } } } else if (IO == INPUTS) { res = dc.readBytes(area, DB, DataByte, len, null); if (res == 0) { if (area == libnodave.daveDB || area == libnodave.daveInputs || area == libnodave.daveFlags) { if (bitnumber > 0) { myByte = (byte)dc.getU8(); myBit = (myByte & (1 << bitnumber - 1)) != 0; Console.WriteLine(myBit); } else if (len == 1) { myByte = (System.Byte)dc.getU8(); Console.WriteLine(myByte); } else if (len == 2) { myWord = (System.Int16)dc.getU16(); Console.WriteLine(myWord); } else if (len == 4) { myDWord = (System.Int32)dc.getU32(); Console.WriteLine(myDWord); } } if (area == libnodave.daveCounter) { Console.WriteLine(dc.getS8()); } if (area == libnodave.daveTimer) { Console.WriteLine(dc.getS8()); } } } else { Console.WriteLine("error " + res + " " + libnodave.daveStrerror(res)); } } //if (IO == INPUTS) // Console.ReadKey(); dc.disconnectPLC(); } di.disconnectAdapter(); libnodave.closeSocket(fds.rfd); } else { Console.WriteLine("Couldn't open TCP connaction to " + args[0]); return(-1); } return(0); } }
public static int Main(string[] args) { int i; int rack, slot; int res; int readDB, readstart, readnumberofbytes; int writeDB, writestart, writenumberofbytes; int area = 0; var writebuffer = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; var readbuffer = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; string PLC = "255.255.255.255"; libnodave.daveSetDebug(libnodave.daveDebugRawRead); if (args.Length != 9) { Console.WriteLine(""); Console.WriteLine("Usage:"); Console.WriteLine("./S7koppelvlak [ipaddress] [rack] [slot] [readDB] [startbyte] [numberofbytes] [writeDB] [startbyte] [numberofbytes]"); Console.WriteLine(""); Console.WriteLine("Examples:"); Console.WriteLine("./S7koppelvlak 172.24.40.191 0 3 DB10 0 16 DB20 17 16"); Console.WriteLine(""); Console.WriteLine("./S7koppelvlak 172.24.40.191 0 3 DB100 128 32 DB100 0 64"); Console.WriteLine(""); return(-4); } for (i = 0; i < 9; i++) { Console.WriteLine("" + args[i]); } PLC = args[0]; rack = Convert.ToInt32(args[1]); slot = Convert.ToInt32(args[2]); readDB = Convert.ToInt32(args[3]); readstart = Convert.ToInt32(args[4]); readnumberofbytes = Convert.ToInt32(args[5]); writeDB = Convert.ToInt32(args[6]); writestart = Convert.ToInt32(args[7]); writenumberofbytes = Convert.ToInt32(args[8]); Console.WriteLine("Connection PLC " + PLC + ", rack " + Convert.ToString(rack) + ", slot " + Convert.ToString(slot)); Console.WriteLine("Read block : DB" + Convert.ToString(readDB) + "[" + Convert.ToString(readstart) + ".." + Convert.ToString(readstart + readnumberofbytes - 1) + "]"); Console.WriteLine("Write block : DB" + Convert.ToString(writeDB) + "[" + Convert.ToString(writestart) + ".." + Convert.ToString(writestart + writenumberofbytes - 1) + "]"); area = libnodave.daveDB; fds.rfd = libnodave.openSocket(102, PLC); fds.wfd = fds.rfd; if (fds.rfd <= 0) { return(-5); } else { di = new libnodave.daveInterface(fds, "IF1", 0, libnodave.daveProtoISOTCP, libnodave.daveSpeed187k); di.setTimeout(1000000); res = di.initAdapter(); if (res != 0) { return(-4); } else { dc = new libnodave.daveConnection(di, 0, rack, slot); if (dc.connectPLC() != 0) { Console.WriteLine("Couldn't open TCP connaction to " + args[0]); return(-2); } else { while (true) { // Incoming data res = dc.readBytes(area, readDB, readstart, readnumberofbytes, null); if (res == 0) { for (i = 0; i < readnumberofbytes; i++) { readbuffer[i] = (System.Byte)dc.getU8(); } } // Copy incoming data to PiFace Digital board outputs... Console.WriteLine(readbuffer); // Copy inputs form PiFace Digital board to writebuffer... // Outgoing data for (i = 0; i < writenumberofbytes; i++) { writebuffer[i] = (byte)i; } res = dc.writeBytes(area, writeDB, writestart, writenumberofbytes, writebuffer); Thread.Sleep(1000); } dc.disconnectPLC(); } di.disconnectAdapter(); libnodave.closeSocket(fds.rfd); return(0); } } }