コード例 #1
0
        //PLC写入
        private bool  WriteSieTcpValue(Config.PlcTypeItem plctype, Config.PlcDataItem plcdata, SiemensS7Net plc, object value)
        {
            HslCommunication.OperateResult rlt = new HslCommunication.OperateResult();
            rlt.IsSuccess = false;
            try
            {
                //string[] strAdrss = plcdata.Address.Split('.');
                //string Address = strAdrss[0] + "." + Regex.Replace(strAdrss[1], @"^[A-Za-z]+", string.Empty);

                if (plctype == null || !plctype.IsConnected || plcdata == null || plc == null)
                {
                    return(rlt.IsSuccess);
                }
                switch (plcdata.DataType)
                {
                case Common.DataTypes.Bool:    //Bool
                    rlt = plc.Write(plcdata.Address, Convert.ToBoolean(value));
                    break;

                case Common.DataTypes.Byte:    //Byte
                    rlt = plc.Write(plcdata.Address, Convert.ToChar(value));
                    break;

                case Common.DataTypes.Short:
                    rlt = plc.Write(plcdata.Address, Convert.ToInt16(value));
                    break;

                case Common.DataTypes.Ushort:
                    rlt = plc.Write(plcdata.Address, Convert.ToUInt16(value));
                    break;

                case Common.DataTypes.Int:
                    rlt = plc.Write(plcdata.Address, Convert.ToInt32(value));
                    break;

                case Common.DataTypes.UInt:
                    rlt = plc.Write(plcdata.Address, Convert.ToUInt32(value));
                    break;

                case Common.DataTypes.Long:
                    long lValue = 0;
                    if (long.TryParse(value.ToString(), out lValue))
                    {
                        long lValueNew = BpLong.SwapInt64(lValue);
                        rlt = plc.Write(plcdata.Address, lValueNew);
                    }
                    //plc.Write(plcdata.Address, Convert.ToInt64(value));
                    break;

                case Common.DataTypes.ULong:
                    rlt = plc.Write(plcdata.Address, Convert.ToUInt64(value));
                    break;

                case Common.DataTypes.Float:
                    rlt = plc.Write(plcdata.Address, float.Parse(value.ToString()));
                    break;

                case Common.DataTypes.Double:
                    rlt = plc.Write(plcdata.Address, Convert.ToDouble(value));
                    break;

                case Common.DataTypes.String:
                    if (value != null)
                    {
                        byte[] btValue = System.Text.Encoding.ASCII.GetBytes(value.ToString());
                        byte[] arrData = new byte[btValue.Length + 2];
                        arrData[0] = 50;
                        arrData[1] = (byte)btValue.Length;
                        btValue.CopyTo(arrData, 2);
                        rlt = plc.Write(plcdata.Address, arrData);
                    }
                    break;

                default: break;
                }
            }
            catch
            {
                //MessageBox.Show(ex.Message);
            }
            return(rlt.IsSuccess);
        }
コード例 #2
0
        //PLC读取
        private object ReadSieTcpValue(Config.PlcTypeItem plctype, Config.PlcDataItem plcdata, SiemensS7Net plc)
        {
            try
            {
                if (plctype == null || !plctype.IsConnected || plcdata == null || plc == null)
                {
                    return(null);
                }

                switch (plcdata.DataType)
                {
                case Common.DataTypes.Bool:    //Bool
                    plcdata.ValueNew = plc.ReadBool(plcdata.Address).Content;
                    break;

                case Common.DataTypes.Byte:    //Byte
                    plcdata.ValueNew = plc.ReadByte(plcdata.Address).Content;
                    break;

                case Common.DataTypes.Short:
                    plcdata.ValueNew = plc.ReadInt16(plcdata.Address).Content;
                    break;

                case Common.DataTypes.Ushort:
                    plcdata.ValueNew = plc.ReadUInt16(plcdata.Address).Content;
                    break;

                case Common.DataTypes.Int:
                    plcdata.ValueNew = plc.ReadInt32(plcdata.Address).Content;
                    break;

                case Common.DataTypes.UInt:
                    plcdata.ValueNew = plc.ReadUInt32(plcdata.Address).Content;
                    break;

                case Common.DataTypes.Long:
                    long lValueNew = 0;
                    if (long.TryParse(plc.ReadInt64(plcdata.Address).Content.ToString(), out lValueNew))
                    {
                        long temp = BpLong.SwapInt64(lValueNew);
                        plcdata.ValueNew = temp;
                    }
                    // plcdata.ValueNew = plc.ReadInt64(plcdata.Address).Content;
                    break;

                case Common.DataTypes.ULong:
                    plcdata.ValueNew = plc.ReadUInt64(plcdata.Address).Content;
                    break;

                case Common.DataTypes.Float:
                    plcdata.ValueNew = plc.ReadFloat(plcdata.Address).Content;
                    break;

                case Common.DataTypes.Double:
                    plcdata.ValueNew = plc.ReadDouble(plcdata.Address).Content;
                    break;

                case Common.DataTypes.String:
                    HslCommunication.OperateResult <byte[]> data = (HslCommunication.OperateResult <byte[]>)plc.Read(plcdata.Address, 50);
                    if (data != null && data.Content != null && data.Content.Length > 2)
                    {
                        List <byte> lstData = new List <byte>();
                        int         nLen    = data.Content[1];
                        for (int i = 2; i < nLen + 2; i++)
                        {
                            lstData.Add(data.Content[i]);
                        }
                        plcdata.ValueNew = System.Text.Encoding.ASCII.GetString(lstData.ToArray());
                    }
                    break;

                default:
                    break;
                }
            }
            catch
            {
                //MessageBox.Show(ex.Message);
            }

            return(plcdata.ValueNew);
        }