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); } } } }
public bool WritePara(CallDave.daveConnection dvc, string key, string value, string ParaType, string ParaScope, string plcAddress, out string outMsg) { int res = 0; outMsg = ""; bool result = false; if (value == null || value == string.Empty || value == "") { value = "0"; } byte[] b_data = { 0, 0 }; try { result = CheckValueScope(ParaScope, value); if (!result) { outMsg = string.Format("[{0}]验证范围失败", key); throw new ApplicationException(outMsg); } int db = 0; int start = 0; result = SplitplcAddress(plcAddress, out db, out start, out outMsg); if (!result) { outMsg = string.Format("[{0}]PLC地址不合法", key); throw new ApplicationException(outMsg); } b_data = BitConverter.GetBytes(ushort.Parse(value)); b_data = b_data.Reverse().ToArray(); res = dvc.writeBytes(CallDave.daveDB, db, start, b_data.Length, b_data); if (res != 0) { outMsg = string.Format("[{0}]写入PLC失败-{1}", key, res); throw new ApplicationException(outMsg); } result = ServiceREF.SetParameterByCode(key, value, out outMsg); if (!result) { outMsg = string.Format("[{0}]保存数据至服务器失败-{1}", key, outMsg); throw new ApplicationException(outMsg); } return(result); } catch (Exception ex) { outMsg = ex.Message; return(false); } }