public void ReadBool( )
        {
            #region ReadBool

            MelsecFxSerial melsecFx = new MelsecFxSerial( );
            melsecFx.SerialPortInni(sp =>
            {
                sp.PortName = "COM1";
                sp.BaudRate = 9600;
                sp.DataBits = 7;
                sp.StopBits = System.IO.Ports.StopBits.One;
                sp.Parity   = System.IO.Ports.Parity.Even;
            });
            melsecFx.Open( );

            // 以下是简单的读取,没有仔细校验的方式
            bool   X1    = melsecFx.ReadBool("X1").Content;
            bool[] X1_10 = melsecFx.ReadBool("X1", 10).Content;

            // 如果需要判断是否读取成功
            OperateResult <bool> R_X1 = melsecFx.ReadBool("X1");
            if (R_X1.IsSuccess)
            {
                // success
                bool value = R_X1.Content;
            }
            else
            {
                // failed
            }


            OperateResult <bool[]> R_X1_10 = melsecFx.ReadBool("X1", 10);
            if (R_X1_10.IsSuccess)
            {
                // success
                bool x1 = R_X1_10.Content[0];
                bool x2 = R_X1_10.Content[1];
                bool x3 = R_X1_10.Content[2];
                bool x4 = R_X1_10.Content[3];
                bool x5 = R_X1_10.Content[4];
                bool x6 = R_X1_10.Content[5];
                bool x7 = R_X1_10.Content[6];
                bool x8 = R_X1_10.Content[7];
                bool x9 = R_X1_10.Content[8];
                bool xa = R_X1_10.Content[9];
            }
            else
            {
                // failed
            }


            #endregion
        }
 private void button_read_bool_Click(object sender, EventArgs e)
 {
     // 读取bool变量
     if (textBox12.Text == "1")
     {
         DemoUtils.ReadResultRender(melsecSerial.ReadBool(textBox3.Text), textBox3.Text, textBox4);
     }
     else
     {
         DemoUtils.ReadResultRender(melsecSerial.ReadBool(textBox3.Text, ushort.Parse(textBox12.Text)), textBox3.Text, textBox4);
     }
 }
Beispiel #3
0
        public byte[] ReadBytes(DeviceAddress address, ushort size)
        {
            string addr;

            addr = GetAddress(address);
            try
            {
                lock (_async)
                {
                    if (address.DBNumber < 3)
                    {
                        var read = _fxSerial.ReadBool(addr, (ushort)(size * 16));
                        if (read.IsSuccess)
                        {
                            byte[] retBytes = new byte[size * 2];
                            Buffer.BlockCopy(read.Content, 0, retBytes, 0, size * 2);
                            return(retBytes);
                        }
                        else
                        {
                            if (OnError != null)
                            {
                                OnError(this, new IOErrorEventArgs(read.Message));
                            }
                            return(null);
                        }
                    }
                    else
                    {
                        var read = _fxSerial.Read(addr, size);
                        if (read.IsSuccess)
                        {
                            return(read.Content);
                        }
                        else
                        {
                            if (OnError != null)
                            {
                                OnError(this, new IOErrorEventArgs(read.Message));
                            }
                            return(null);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                if (OnError != null)
                {
                    OnError(this, new IOErrorEventArgs(e.Message));
                }
                return(null);
            }
        }
        private void test1()
        {
            // 如果我们想要读取M100-M109,我们可以按照如下的代码进行操作

            // if we want read M100-M109, so we can do like this
            OperateResult <bool[]> read = melsecSerial.ReadBool("M100", 10);

            if (read.IsSuccess)
            {
                bool m100 = read.Content[0];
                // and so on
                // ...
                // then
                bool m109 = read.Content[9];
            }
            else
            {
                // failed, the follow operation is output the wrong msg
                Console.WriteLine("Read failed: " + read.ToMessageShowString( ));
            }
        }
 private void button_read_bool_Click(object sender, EventArgs e)
 {
     // 读取bool变量
     readResultRender(melsecSerial.ReadBool(textBox3.Text), textBox3.Text, textBox4);
 }
Beispiel #6
0
        private void ThreadReadServer()
        {
            if (melsecFxSerial != null)
            {
                while (isThreadRun)
                {
                    Thread.Sleep(timeSleep);
                    try
                    {
                        Task.Run(() => {
                            // Read X0-X7
                            var ReadXInput = melsecFxSerial.ReadBool("X0", 8);
                            if (ReadXInput.IsSuccess)
                            {
                                // Tampilkan Data
                                if (isThreadRun)
                                {
                                    //Invoke(new Action<short>(AddDataCurve), read.Content);
                                    this.Invoke((MethodInvoker) delegate
                                    {
                                        TSStatusPLC.Text = "Connected to PLC";

                                        // runs on UI thread
                                        var value = ReadXInput.Content;
                                        for (ushort i = 0; i <= LampXInput.Count - 1; i++)
                                        {
                                            if (value[i])
                                            {
                                                LampXInput[i].Image = Properties.Resources.lamp_green_on;
                                            }
                                            else
                                            {
                                                LampXInput[i].Image = Properties.Resources.lamp_green_off;
                                            }
                                        }

                                        //metroLabel4.Text = Convert.ToString(value[0]);
                                    });
                                }
                            }
                            else
                            {
                                this.Invoke((MethodInvoker) delegate
                                {
                                    // runs on UI thread
                                    TSStatusPLC.Text = "reconnecting...";
                                });
                            }
                        });

                        Task.Run(() => {
                            // Read Y0-Y7
                            var ReadYOutput = melsecFxSerial.ReadBool("Y0", 8);
                            if (ReadYOutput.IsSuccess)
                            {
                                // Tampilkan Data
                                if (isThreadRun)
                                {
                                    //Invoke(new Action<short>(AddDataCurve), read.Content);
                                    this.Invoke((MethodInvoker) delegate
                                    {
                                        // runs on UI thread
                                        var value = ReadYOutput.Content;
                                        for (ushort i = 0; i <= LampYOutput.Count - 1; i++)
                                        {
                                            if (value[i])
                                            {
                                                LampYOutput[i].Image = Properties.Resources.lamp_green_on;
                                            }
                                            else
                                            {
                                                LampYOutput[i].Image = Properties.Resources.lamp_green_off;
                                            }
                                        }
                                    });
                                }
                            }
                        });
                    }
                    catch (Exception ex)
                    {
                        //MessageBox.Show("Read failed:" + ex.Message);
                    }
                }
            }
        }
        public override object ReadTag(Tag tag)
        {
            if (tag.AccessType == TagAccessType.Read || tag.AccessType == TagAccessType.ReadWrite)
            {
                try
                {
                    if (tag.TagType == "bool")
                    {
                        var res = PLC.ReadBool(tag.Address);
                        if (res.IsSuccess)
                        {
                            tag.TagValue = res.Content;
                            tag.Quality  = Quality.Good;
                        }
                        else
                        {
                            tag.Quality = Quality.Bad;
                        }
                    }
                    else if (tag.TagType == "string")
                    {
                        if (tag.Address.Contains("."))
                        {
                            try
                            {
                                string address = tag.Address.Split('.')[0];
                                ushort len     = Convert.ToUInt16(tag.Address.Split('.')[1]);
                                var    res     = PLC.ReadString(tag.Address.Split('.')[0], len);
                                if (res.IsSuccess)
                                {
                                    tag.TagValue = res.Content.Replace("\0", "");
                                    tag.Quality  = Quality.Good;
                                }
                                else
                                {
                                    tag.Quality = Quality.Bad;
                                }
                            }
                            catch (Exception)
                            {
                                LOG.Error($"Tag Address Error {tag.Address}");
                            }
                        }
                        else
                        {
                            LOG.Error($"Tag Address Error {tag.Address}");
                        }
                    }
                    else
                    {
                        ushort len = ConvertUtils.GetLength(tag);

                        var res = PLC.Read(tag.Address, len);
                        ConvertUtils.DecodeTagValue(tag, res);
                    }
                    return(tag.TagValue);
                }
                catch (Exception ex)
                {
                    LOG.Error($"Datasource[{SourceName}] read error. Tag[{tag.TagName}] Address[{tag.Address}] Message[{ex.Message}]");
                    tag.TagValue = null;
                    tag.Quality  = Quality.Bad;
                    return(tag.TagValue);
                }
            }
            else
            {
                return(null);
            }
        }