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 void AddConnection(CallDave.daveInterface di) { if (di != null) { CallDave.daveConnection dc = new CallDave.daveConnection(di, 2, 0, 2); Connections.Add(dc); interfaces.Add(di); } }
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); } }
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); } }
/// <summary> /// 读取PLC /// </summary> /// <param name="_dVC"></param> /// <returns></returns> public bool LoadFromPLC(CallDave.daveConnection _dVC) { return(true); }
public EArgOnHandle_Process(CallDave.daveConnection conn) { PLCConn = conn; }