Beispiel #1
0
        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);
            }
        }
Beispiel #2
0
        /// <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}");
            }
        }
Beispiel #3
0
        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));
        }
Beispiel #4
0
 public SiemensPLC_Test()
 {
     plcHandle = CS7TcpClient.CreatePlc();
 }
Beispiel #5
0
        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);
            }
        }
Beispiel #6
0
        /// <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));
            }
        }
Beispiel #7
0
 private void Disconnect()
 {
     CS7TcpClient.DisconnectPlc(plcHandle);
 }