private void btn_Connect_Click(object sender, EventArgs e) { CallDave.daveOSserialType fds; CallDave.daveInterface di; CallDave.daveConnection dc; int localMPI = 0; int plcMPI = 2; int i, a = 0, j, res, b = 0, c = 0; float d = 0; fds.rfd = CallDave.openSocket(102, "192.168.1.4"); //fds.rfd = CallDave.setPort("80001", "38400", 'O'); fds.wfd = fds.rfd; if (fds.rfd > 0) { di = new CallDave.daveInterface(fds, "IF1", localMPI, CallDave.daveProtoISOTCP, CallDave.daveSpeed187k); di.setTimeout(10000); res = di.initAdapter(); if (res == 0) { dc = new CallDave.daveConnection(di, plcMPI, 0, 2); if (0 == dc.connectPLC()) { byte[] buffer = { 0, 1 }; //int res = e.PLCConn.writeBits(CallDave.daveDB, 1354, 114, 1, buffer); res = dc.writeBytes(CallDave.daveDB, 1354, 114, 2, buffer); //buffer[0] = 2; //res = dc.writeBytes(CallDave.daveDB, 101, 0, 1, buffer); //res = dc.readBytes(CallDave.daveDB, 101, 0, 28, buffer); //CallDave.PDU pdu = dc.prepareReadRequest(); //pdu.addBitVarToReadRequest(CallDave.daveDB, 101, 0, 1); //pdu.addBitVarToReadRequest(CallDave.daveDB, 101, 0, 1); //pdu.addBitVarToReadRequest(CallDave.daveDB, 101, 0, 1); //if (res == 0) //{ // //a = dc.getS32(); // //b = dc.getS32(); // //c = dc.getS32(); // //d = dc.getFloat(); // int b1 = dc.getU8(); // int b2 = dc.getU8(); // int b3 = dc.getU8(); // int b4 = dc.getU8(); // int B5 = dc.getS32(); // int B6 = dc.getS32(); // int b7 = dc.getU8(); // int b8 = dc.getU8(); // int B9 = dc.getU8(); //} res = dc.disconnectPLC(); //res = dc.writeBytes(CallDave.daveDB, 0, 1, 16, null); } } } }
protected void Run() { try { int plcMPI = 2; CallDave.daveInterface di = CreateDaveInterface_Define(IP, Port); while (IsBeginProcess && di != null) { int res = di.initAdapter(); if (res == 0) { _dVC = new CallDave.daveConnection(di, plcMPI, rack, slot); if (0 == _dVC.connectPLC()) { Status = ePLCStatus.Connected;; int times = 1; if (LoopWrite != null) { do { EArgOnHandle_Process e = new EArgOnHandle_Process(_dVC); LoopWrite(this, e); if (e.result) { times = 0; } else { times++; } Thread.Sleep(this.ProecssSleepTime); } while (times <= MaxReConnTime); } //Log PLCProcesser.ReConnSleep 后重新连接 WLog.WriteLog(Enum_LogType.LogType_Communication, Enum_LogGrade.LogGrade_Nin, Enum_LogMessageType.LogMsgType_Event, "Run", "ReConnSleep", ""); Thread.Sleep(this.ReConnSleep); } Thread.Sleep(this.ReConnSleep); } } if (di != null) { di.disconnectAdapter(); } Status = ePLCStatus.Closed; WLog.WriteLog(Enum_LogType.LogType_Error, Enum_LogGrade.LogGrade_Fiv, Enum_LogMessageType.LogMsgType_Exception, "Run", "Closed", string.Format("IP:{0},Port:{1},Flag:{2}", IP, Port, IsBeginProcess)); } catch (Exception ex) { Status = ePLCStatus.Failed; WLog.WriteLog(Enum_LogType.LogType_Error, Enum_LogGrade.LogGrade_Nin, Enum_LogMessageType.LogMsgType_Exception, "Run", ex.Message, ex.StackTrace); } }