예제 #1
0
 private void btnReadReal_Click(object sender, EventArgs e)
 {
     try
     {
         if (errCode != ExceptionCode.ExceptionNo)
         {
             return;
         }
         //double value = Types.Double.FromDWord((uint)plc.Read("DB90.DBD248"));
         string[] tempStrg = txtRealOutput.Text.Split('.');
         int      size     = tempStrg.Length;
         double   value    = 0;
         db            = 0;
         startByteAddr = 0;
         if (size >= 2)
         {
             db            = int.Parse(tempStrg[0].Substring(2, tempStrg[0].Length - 2));
             startByteAddr = int.Parse(tempStrg[1].Substring(3, tempStrg[1].Length - 3));
         }
         value             = Types.Double.FromDWord((uint)plc.Read(txtRealOutput.Text));
         txtRealInput.Text = value.ToString("0.#");
     }
     catch (Exception ex)
     {
         MessageBox.Show(this, ex.Message, "Notification", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }
        //struct in datablock to read .


        private void timer1_Tick_1(object sender, EventArgs e)
        { //call funtion read data
            try
            {
                if (plcstation.IsConnected)
                {
                    double m1 = (double)plcstation.Read("DB10.DBD0", VarType.Real);
                    double m2 = (double)plcstation.Read("DB10.DBD4", VarType.Real);
                    double m3 = (double)plcstation.Read("DB10.DBD8", VarType.Real);
                    double m4 = (double)plcstation.Read("DB10.DBD12", VarType.Real);
                    double m5 = (double)plcstation.Read("DB10.DBD16", VarType.Real);
                    //insert sql ;
                    //////////////////////////


                    StringBuilder sbSql = new StringBuilder();
                    sbSql.Append(@"INSERT INTO MachineRuning(TagName,Valuee,TimeStamp) VALUES");
                    double[] data = { m1, m2, m3, m4, m5 };
                    for (int i = 0; i < data.Length; i++)
                    {
                        string values = string.Format("({0},{1},{2}),",
                                                      new string[] { "@TagName" + i.ToString(), "@Valuee" + i.ToString(), "@TimeStamp" + i.ToString() });
                        sbSql.Append(values);
                    }
                    string sql = sbSql.ToString();
                    sql = sql.TrimEnd(','); // remove the trailing comma ','
                    string        conectionstring = @" Data Source = DESKTOP-TF7PT8I\WINCC; Initial Catalog = S71200; User ID = sa; Password = gcsvn";
                    SqlConnection conect          = new SqlConnection(conectionstring);
                    SqlCommand    cmd             = new SqlCommand(sql, conect);//create command
                    conect.Open();
                    for (int j = 0; j < data.Length; j++)
                    {
                        cmd.Parameters.Add("@TagName" + j.ToString(), System.Data.SqlDbType.VarChar).Value    = "ToTalTimeM_" + (j + 1).ToString();
                        cmd.Parameters.Add("@Valuee" + j.ToString(), System.Data.SqlDbType.Float).Value       = data[j];
                        cmd.Parameters.Add("@TimeStamp" + j.ToString(), System.Data.SqlDbType.DateTime).Value = DateTime.Now;
                    }
                    //insert into bang vlues(register1,3,datetime.now),(register2,5,datetime.now),()
                    cmd.ExecuteNonQuery();
                    conect.Close();
                    ///////////////display //////////////////////
                    textBox1.Text = string.Format("{0:N3}", m1);
                    textBox2.Text = string.Format("{0:N3}", m2);
                    textBox3.Text = string.Format("{0:N3}", m3);
                    textBox4.Text = string.Format("{0:N3}", m4);
                    textBox5.Text = string.Format("{0:N3}", m5);
                }
                else
                {
                    textBox6.Text = "Disconnect with PLC";
                }
            }
            catch (Exception ess)
            {
                MessageBox.Show(this, ess.Message);
                plcstation.Open();
            }
        }
예제 #3
0
 private void btnRead_Click(object sender, EventArgs e)
 {
     try
     {
         txtOutput.Text = plc.Read(txtInput.Text).ToString();
     }
     catch (Exception ex)
     {
         MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }
예제 #4
0
        public async Task TestIntWithRead()
        {
            var myPLC = new PLC(Configuration.ipAddress, Configuration.slot);
            await myPLC.Write("BaseINT", TagType.Int, (short)8012);

            var result = await myPLC.Read("BaseINT", TagType.Int);

            Assert.AreEqual(result.Value, "8012");
            await myPLC.Write("BaseINT", TagType.Int, (short)8009);

            var result2 = await myPLC.Read("BaseINT", TagType.Int);

            Assert.AreEqual(result2.Value, "8009");
        }
예제 #5
0
        public async Task TestStringWithRead()
        {
            var myPLC = new PLC(Configuration.ipAddress, Configuration.slot);
            await myPLC.Write("BaseSTRING", TagType.String, "yXbDjcoUjcYjRQDxnZTeDCdZuOTXrJMHJIBeZPgUEPsTjqLNNk");

            var result = await myPLC.Read("BaseSTRING", TagType.String);

            Assert.AreEqual(result.Value, "yXbDjcoUjcYjRQDxnZTeDCdZuOTXrJMHJIBeZPgUEPsTjqLNNk");
            await myPLC.Write("BaseSTRING", TagType.String, "yFbDjcoUjcYjRQDxnZTeDCdZuOTXrJMHJIBeZPgUEPsTjqLNNk");

            var result2 = await myPLC.Read("BaseSTRING", TagType.String);

            Assert.AreEqual(result2.Value, "yFbDjcoUjcYjRQDxnZTeDCdZuOTXrJMHJIBeZPgUEPsTjqLNNk");
        }
예제 #6
0
        public async Task TestRealWithRead()
        {
            var myPLC = new PLC(Configuration.ipAddress, Configuration.slot);
            await myPLC.Write("BaseREAL", TagType.Real, (float)2);

            var result = await myPLC.Read("BaseREAL", TagType.Real);

            Assert.AreEqual(result.Value, "2");
            await myPLC.Write("BaseREAL", TagType.Real, (float)2.7);

            var result2 = await myPLC.Read("BaseREAL", TagType.Real);

            Assert.AreEqual(result2.Value, "2.7");
        }
예제 #7
0
        public async Task TestSintWithRead()
        {
            var myPLC = new PLC(Configuration.ipAddress, Configuration.slot);
            await myPLC.Write("BaseSINT", TagType.Sint, (sbyte)-62);

            var result = await myPLC.Read("BaseSINT", TagType.Sint);

            Assert.AreEqual(result.Value, "-62");
            await myPLC.Write("BaseSINT", TagType.Sint, (sbyte)64);

            var result2 = await myPLC.Read("BaseSINT", TagType.Sint);

            Assert.AreEqual(result2.Value, "64");
        }
예제 #8
0
        public async Task TestLintWithRead()
        {
            var myPLC = new PLC(Configuration.ipAddress, Configuration.slot);
            await myPLC.Write("BaseLINT", TagType.Lint, (long)-576406435);

            var result = await myPLC.Read("BaseLINT", TagType.Lint);

            Assert.AreEqual(result.Value, "-576406435");
            await myPLC.Write("BaseLINT", TagType.Lint, (long)566406435);

            var result2 = await myPLC.Read("BaseLINT", TagType.Lint);

            Assert.AreEqual(result2.Value, "566406435");
        }
예제 #9
0
        public async Task TestDintWithRead()
        {
            var myPLC = new PLC(Configuration.ipAddress, Configuration.slot);
            await myPLC.Write("BaseDINT", TagType.Dint, -545437491);

            var result = await myPLC.Read("BaseDINT", TagType.Dint);

            Assert.AreEqual(result.Value, "-545437491");
            await myPLC.Write("BaseDINT", TagType.Dint, -545437485);

            var result2 = await myPLC.Read("BaseDINT", TagType.Dint);

            Assert.AreEqual(result2.Value, "-545437485");
        }
예제 #10
0
        public async Task TestBoolWithRead()
        {
            var myPLC = new PLC(Configuration.ipAddress, Configuration.slot);
            await myPLC.Write("BaseBOOL", TagType.Bool, false);

            var result = await myPLC.Read("BaseBOOL", TagType.Bool);

            Assert.AreEqual("False", result.Value);

            await myPLC.Write("BaseBOOL", TagType.Bool, true);

            var result2 = await myPLC.Read("BaseBOOL", TagType.Bool);

            Assert.AreEqual("True", result2.Value);
        }
예제 #11
0
        public bool ReadBool(string variable)
        {
            PLC.Open();
            bool value = (bool)(PLC.Read(variable));

            PLC.Close();
            return(value);
        }
예제 #12
0
        public async Task TestBoolWithReadNull()
        {
            var myPLC = new PLC(Configuration.ipAddress, Configuration.slot);
            //await myPLC.Write("BaseBOOL", TagType.Bool, false);
            var result = await myPLC.Read("BaseBOOLsdfsdf", TagType.Bool);

            Assert.AreEqual(null, result.Value);
        }
예제 #13
0
        public double ReadDouble(string variable, int decimalNumbers)
        {
            PLC.Open();
            double value = Math.Round(Convert.ToDouble((uint)PLC.Read(variable)), decimalNumbers);

            PLC.Close();
            return(value);
        }
예제 #14
0
        public int ReadDIntDelete(string variable)
        {
            var value = PLC.Read(variable);

            if (Convert.ToInt32(value) == 0)
            {
                return(0);
            }
            return((ushort)value);
        }
예제 #15
0
 public bool this[int index]
 {
     get
     {
         return(PLC.Read(Offset + index * ElmSize, ElmSize).ReadByte() != 0);
     }
     set
     {
         PLC.Write(Offset + index * ElmSize, new byte[] { (byte)(value ? 1 : 0) });
     }
 }
예제 #16
0
 public UInt16 this[int index]
 {
     get
     {
         return(PLC.Read(Offset + index * ElmSize, ElmSize).ReadUInt16());
     }
     set
     {
         PLC.Write(Offset + index * ElmSize, value);
     }
 }
예제 #17
0
 public int this[int index]
 {
     get
     {
         return(PLC.Read(Offset + index * ElmSize, ElmSize).ReadInt32());
     }
     set
     {
         PLC.Write(Offset + index * ElmSize, value);
     }
 }
예제 #18
0
 public float this[int index]
 {
     get
     {
         return(PLC.Read(Offset + index * ElmSize, ElmSize).ReadSingle());
     }
     set
     {
         PLC.Write(Offset + index * ElmSize, value);
     }
 }
예제 #19
0
        public int ReadInt(string variable)
        {
            //int test = Int.FromByteArray((byte[])PLC.Read(variable));
            PLC.Open();
            var value = PLC.Read(variable);

            PLC.Close();
            if (Convert.ToInt32(value) == 0)
            {
                return(0);
            }
            return((ushort)value);
        }
        public async Task TestBoolArrayWithReadWrite()
        {
            var myPLC = new PLC(Configuration.ipAddress, Configuration.slot);
            var alist = new List <bool>(Randomizer.GenRandBoolList(128)); // Randomize first to ensure new values

            var result = await myPLC.Write("BaseBOOLArray", TagType.Bool, alist.ToArray(), 128);

            Assert.AreEqual("Success", result.Status);

            var result2 = await myPLC.Read("BaseBOOLArray", TagType.Bool, 128);

            string[] arrString = Array.ConvertAll(alist.ToArray(), Convert.ToString);
            Assert.IsTrue(result2.Value.SequenceEqual(arrString));
        }
예제 #21
0
        public async Task TestStringArrayRange04()
        {
            var myPLC = new PLC(Configuration.ipAddress, Configuration.slot);
            var alist = new List <string>(Randomizer.GenRandStringList(128)); // Randomize first to ensure new values
            await myPLC.Write("BaseSTRINGArray", TagType.String, alist.ToArray(), 128);

            var updateValues = new List <string>(Randomizer.GenRandStringList(10));

            var result = await myPLC.Write("BaseSTRINGArray", TagType.String, updateValues.ToArray(), 128, 118, 10);

            Assert.AreEqual("Success", result.Status);

            var result2 = await myPLC.Read("BaseSTRINGArray", TagType.String, 128, 118, 10);

            Assert.IsTrue(result2.Value.SequenceEqual(updateValues.ToArray()));
        }
        public async Task TestBoolArrayRange04()
        {
            var myPLC = new PLC(Configuration.ipAddress, Configuration.slot);
            var alist = new List <bool>(Randomizer.GenRandBoolList(128)); // Randomize first to ensure new values
            await myPLC.Write("BaseBOOLArray", TagType.Bool, alist.ToArray(), 128);

            var updateValues = new List <bool>(Randomizer.GenRandBoolList(10));

            var result = await myPLC.Write("BaseBOOLArray", TagType.Bool, updateValues.ToArray(), 128, 118, 10);

            Assert.AreEqual("Success", result.Status);

            var result2 = await myPLC.Read("BaseBOOLArray", TagType.Bool, 128, 118, 10);

            bool[] arrBool = Array.ConvertAll(result2.Value, Convert.ToBoolean);
            Assert.IsTrue(arrBool.SequenceEqual(updateValues.ToArray()));
        }
예제 #23
0
        private void readB_Click(object sender, EventArgs e)
        {
            PLC p = SelectedPLC;

            if (p == null)
            {
                return;
            }
            int          offset = (int)rwOffsetNUD.Value;
            int          len    = (int)rwLenNUD.Value;
            BinaryReader br     = p.Read(offset, len);

            rwHB.ByteProvider = new DynamicByteProvider(new byte[len]);
            rwHB.ByteProvider.LengthChanged += new EventHandler(writeRawLengthChangedHandler);
            for (int i = 0; i < len; i++)
            {
                rwHB.ByteProvider.WriteByte(i, br.ReadByte());
            }
        }
예제 #24
0
파일: Form1.cs 프로젝트: Hoan96/S7ToConnect
 private void btnOn1_Click(object sender, EventArgs e)
 {
     try
     {
         if (plc != null)
         {
             // plc.Write("M0.0", 1);
             plc.Read("Q0.0");
         }
         else
         {
             throw new Exception("Chưa kết nối với PLC");
         }
     }
     catch (Exception)
     {
         MessageBox.Show(this, "Chưa kết nối với PLC", "Information", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
     }
 }
예제 #25
0
        public async Task WriteReadBoolRandom()
        {
            var myPLC         = new PLC(Configuration.ipAddress, Configuration.slot);
            var rand          = new Random();
            var numberOfTests = rand.Next(0, MAX_NUMBER_TESTS);
            var alist         = new List <bool>(Randomizer.GenRandBoolList(numberOfTests));

            Console.WriteLine("Number of random tests: " + numberOfTests);

            for (int i = 0; i < alist.Count; i++)
            {
                await myPLC.Write("BaseBOOL", TagType.Bool, alist[i]);

                var result = await myPLC.Read("BaseBOOL", TagType.Bool);

                Assert.AreEqual(alist[i].ToString(), result.Value);
                await Task.Delay(500);
            }
        }
예제 #26
0
 public object Read(string var_name)
 {
     return(plc.Read(var_name));
 }
예제 #27
0
파일: IO.cs 프로젝트: cobrce/Elevator
 private int?ReadBoolAsInt(IPoint output) => PLC.Read(output);
예제 #28
0
 public int ReadDInt(string variable)
 {
     return((ushort)PLC.Read(variable));
 }
예제 #29
0
        public int ReadDWordInteger(string variable)
        {
            UInt32 value = (UInt32)PLC.Read(variable);

            return((int)value);
        }
예제 #30
0
 public int ReadWordInteger(string variable)
 {
     return((ushort)PLC.Read(variable));
 }