private bool Connect() { int iConnected = 0; try { iConnected = CS7TcpClient.ConnectPlc( plcHandle, Encoding.Default.GetBytes(IPAddress), RackNumber, SlotNumber, false, 0, 0); if (iConnected == 0) { plc_Connected = true; return(true); } else { plc_Connected = false; return(false); } } catch (Exception error) { plc_Connected = false; Console.WriteLine(error.Message); return(false); } }
/// <summary> /// 根据错误代码获取错误提示文本 /// </summary> /// <param name="errorCode">错误代码</param> public string GetErrorMsg(int errorCode) { byte[] errText = new byte[1024]; int resNo = CS7TcpClient.GetErrorMsg(errorCode, errText, errText.Length); if (resNo == 0) { return(Encoding.Default.GetString(errText)); } else { return($"ResNo={resNo}"); } }
protected string ReadString(TCustomTKDevice device, ArrayCharOfTag tag) { byte[] buffer = new byte[tag.Length]; int resNo = CS7TcpClient.ReadString( plcHandle, (int)tag.DB_Type, device.DBNumber, tag.DB_Offset, (short)tag.Length, buffer); return(Encoding.Default.GetString(buffer)); }
public SiemensPLC_Test() { plcHandle = CS7TcpClient.CreatePlc(); }
private void Do() { if (!Connect()) { return; } while (!thread_terminated_single) { int resNo = 0; for (int i = 0; i < devices.Count; i++) { buffer = new byte[devices[i].BufferSize]; try { resNo = CS7TcpClient.ReadBlockAsByte( plcHandle, 0x84, devices[i].DBNumber, 0, buffer.Length, buffer); if (resNo != 0) { Console.WriteLine($"{devices[i].DBNumber}.ResNo={resNo}"); } devices[i].DoSomething(buffer); } catch (Exception error) { Console.WriteLine(error.Message); } if (devices[i].COMM.MES_Heart_Beat.Used) { DateTime now = DateTime.Now; DateTime lastCallTime = devices[i].LastMESHeartBeatTime; if ((now - lastCallTime).TotalMilliseconds >= 2000) { devices[i].LastMESHeartBeatTime = now; //devices[i].COMM.MES_Heart_Beat.Value = // !(devices[i].COMM.MES_Heart_Beat.Value); CS7TcpClient.WriteBool( plcHandle, 0x84, devices[i].DBNumber, devices[i].COMM.MES_Heart_Beat.DB_Offset, devices[i].COMM.MES_Heart_Beat.Position, Convert.ToUInt16(!devices[i].COMM.MES_Heart_Beat.Value)); } } } Thread.Sleep(10); } }
/// <summary> /// 回写到 PLC 中 /// </summary> private void WritebackToPLC(TCustomTKDevice device, CustomTagTest tag) { if (tag is BoolOfTag) { BoolOfTag ltag = tag as BoolOfTag; CS7TcpClient.WriteBool( plcHandle, 0x84, device.DBNumber, ltag.DB_Offset, ltag.Position, Convert.ToUInt16(ltag.Value)); } else if (tag is ByteOfTag) { ByteOfTag ltag = tag as ByteOfTag; CS7TcpClient.WriteByte( plcHandle, 0x84, device.DBNumber, ltag.DB_Offset, ltag.Value); } else if (tag is WordOfTag) { WordOfTag ltag = tag as WordOfTag; CS7TcpClient.WriteWord( plcHandle, 0x84, device.DBNumber, ltag.DB_Offset, (uint)ltag.Value); } else if (tag is DWordOfTag) { DWordOfTag ltag = tag as DWordOfTag; CS7TcpClient.WriteDWord( plcHandle, 0x84, device.DBNumber, ltag.DB_Offset, (uint)ltag.Value); } else if (tag is ArrayCharOfTag) { ArrayCharOfTag ltag = tag as ArrayCharOfTag; string temp = ltag.Value.PadRight(ltag.Length, ' ').Substring(0, ltag.Length); CS7TcpClient.WriteBlockAsByte( plcHandle, 0x84, device.DBNumber, ltag.DB_Offset, ltag.Length, Encoding.Default.GetBytes(temp)); } }
private void Disconnect() { CS7TcpClient.DisconnectPlc(plcHandle); }