public void WriteBool( )
        {
            #region WriteBool

            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( );

            // 以下是简单的写入,没有仔细校验的方式
            melsecFx.Write("M100", true);

            // 如果需要判断是否读取成功
            OperateResult write1 = melsecFx.Write("M100", true);
            if (write1.IsSuccess)
            {
                // success
            }
            else
            {
                // failed
            }


            #endregion
        }
Пример #2
0
        public int WriteBit(DeviceAddress address, bool bit)
        {
            try
            {
                lock (_async)
                {
                    string addr = GetAddress(address);
                    var    ret  = _fxSerial.Write(addr, bit);
                    if (ret.IsSuccess)
                    {
                        return(0);
                    }
                    else
                    {
                        if (OnError != null)
                        {
                            OnError(this, new IOErrorEventArgs(ret.Message));
                        }

                        return(-1);
                    }
                }
            }
            catch (Exception e)
            {
                if (OnError != null)
                {
                    OnError(this, new IOErrorEventArgs(e.Message));
                }

                return(-1);
            }
        }
        public override bool WriteTagToRealDevice(Tag tag, object value)
        {
            lock (this)
            {
                OperateResult opres = new OperateResult();
                try
                {
                    if (value is bool)
                    {
                        opres = PLC.Write(tag.Address, (bool)value);
                    }
                    else
                    {
                        if (tag.TagType == "string")
                        {
                            if (tag.Address.Contains("."))
                            {
                                try
                                {
                                    string[]    adds    = tag.Address.Split('.');
                                    string      address = adds[0];
                                    ushort      len     = Convert.ToUInt16(adds[1]);
                                    List <byte> values  = new List <byte>();
                                    values.AddRange(ConvertUtils.GetBytes(tag, value));
                                    while (values.Count < len)
                                    {
                                        values.Add(0);
                                    }

                                    opres = PLC.Write(address, values.ToArray());
                                }
                                catch (Exception)
                                {
                                    LOG.Error($"Tag Address Error {tag.Address}");
                                }
                            }
                            else
                            {
                                LOG.Error($"Tag Address Error {tag.Address}");
                            }
                        }
                        else
                        {
                            opres = PLC.Write(tag.Address, ConvertUtils.GetBytes(tag, value));
                        }
                    }

                    LOG.Info($"数据源[{SourceName}]写入数据 Tag[{tag.TagName}] Address[{tag.Address}] Value[{value.ToString()}] IsSuccess[{opres.IsSuccess}]");
                }
                catch (Exception ex)
                {
                    LOG.Error($"数据源[{SourceName}]写入数据出错 Tag[{tag.TagName}] Address[{tag.Address}] Value[{value.ToString()}] Message[{ex.Message}]");
                }
                return(opres.IsSuccess);
            }
        }
 private void test4()
 {
     // These are the underlying operations that ignore validation of success
     melsecSerial.Write("D100", (short)5);
     melsecSerial.Write("D100", (ushort)5);
     melsecSerial.Write("D100", 5);
     melsecSerial.Write("D100", (uint)5);
     melsecSerial.Write("D100", (long)5);
     melsecSerial.Write("D100", (ulong)5);
     melsecSerial.Write("D100", 5f);
     melsecSerial.Write("D100", 5d);
     // length should Multiples of 2
     melsecSerial.Write("D100", "12345678");
 }
        public void WriteExample2( )
        {
            #region WriteExample2

            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( );

            // 拼凑数据,这样的话,一次通讯就完成数据的全部写入
            byte[] buffer = new byte[8];
            melsecFx.ByteTransform.TransByte((short)1234).CopyTo(buffer, 0);
            melsecFx.ByteTransform.TransByte((short)2100).CopyTo(buffer, 2);
            melsecFx.ByteTransform.TransByte(12353423).CopyTo(buffer, 4);

            OperateResult write = melsecFx.Write("D100", buffer);
            if (write.IsSuccess)
            {
                // success
            }
            else
            {
                // failed
            }

            // 上面的功能等同于三个数据分别写入,下面的性能更差点,因为进行了三次通讯,而且每次还要判断是否写入成功
            //melsec_net.Write( "D100", (short)1234 );
            //melsec_net.Write( "D100", (short)2100 );
            //melsec_net.Write( "D100", 12353423 );

            #endregion
        }
Пример #6
0
 private void button24_Click(object sender, EventArgs e)
 {
     // bool写入
     try
     {
         writeResultRender(melsecSerial.Write(textBox8.Text, bool.Parse(textBox7.Text)), textBox8.Text);
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
     }
 }
Пример #7
0
        public void MoveToPostion(en_Postion postion, int xPostion = 0, int yPostion = 0)
        {
            switch (postion)
            {
            case en_Postion._HomeXY:
                try
                {
                    writeResultRender(melsecSerial.Write(st_AddressX._Home, true), st_AddressX._Home);
                    writeResultRender(melsecSerial.Write(st_AddressY._Home, true), st_AddressY._Home);
                    System.Threading.Thread.Sleep(500);
                    writeResultRender(melsecSerial.Write(st_AddressX._Home, false), st_AddressX._Home);
                    writeResultRender(melsecSerial.Write(st_AddressY._Home, false), st_AddressY._Home);
                }
                catch (Exception ex)
                {
                    //MessageBox.Show(ex.Message);
                }
                break;

            case en_Postion._HomeX:
                try
                {
                    writeResultRender(melsecSerial.Write(st_AddressX._Home, true), st_AddressX._Home);
                    System.Threading.Thread.Sleep(500);
                    writeResultRender(melsecSerial.Write(st_AddressX._Home, false), st_AddressX._Home);
                }
                catch (Exception ex)
                {
                    //MessageBox.Show(ex.Message);
                }
                break;

            case en_Postion._HomeY:
                try
                {
                    writeResultRender(melsecSerial.Write(st_AddressY._Home, true), st_AddressY._Home);
                    System.Threading.Thread.Sleep(500);
                    writeResultRender(melsecSerial.Write(st_AddressY._Home, false), st_AddressY._Home);
                }
                catch (Exception ex)
                {
                    //MessageBox.Show(ex.Message);
                }
                break;

            case en_Postion._PostionXY:
                try
                {
                    writeResultRender(melsecSerial.Write(st_AddressX._LocSet, (int)xPostion), st_AddressX._LocSet);
                    writeResultRender(melsecSerial.Write(st_AddressY._LocSet, (int)yPostion), st_AddressY._LocSet);
                    System.Threading.Thread.Sleep(200);
                    writeResultRender(melsecSerial.Write(st_AddressX._EnLocMove, true), st_AddressX._Home);
                    writeResultRender(melsecSerial.Write(st_AddressY._EnLocMove, true), st_AddressY._Home);
                    System.Threading.Thread.Sleep(200);
                    writeResultRender(melsecSerial.Write(st_AddressX._EnLocMove, false), st_AddressX._Home);
                    writeResultRender(melsecSerial.Write(st_AddressY._EnLocMove, false), st_AddressY._Home);
                }
                catch (Exception ex)
                {
                    //MessageBox.Show(ex.Message);
                }
                break;

            case en_Postion._PostionX:
                try
                {
                    writeResultRender(melsecSerial.Write(st_AddressX._LocSet, xPostion), st_AddressX._LocSet);
                    System.Threading.Thread.Sleep(100);
                    writeResultRender(melsecSerial.Write(st_AddressX._EnLocMove, true), st_AddressX._EnLocMove);
                    System.Threading.Thread.Sleep(200);
                    writeResultRender(melsecSerial.Write(st_AddressX._EnLocMove, false), st_AddressX._EnLocMove);
                }
                catch (Exception ex)
                {
                    //MessageBox.Show(ex.Message);
                }
                break;

            case en_Postion._PostionY:
                try
                {
                    writeResultRender(melsecSerial.Write(st_AddressY._LocSet, yPostion), st_AddressY._LocSet);
                    System.Threading.Thread.Sleep(100);
                    writeResultRender(melsecSerial.Write(st_AddressY._EnLocMove, true), st_AddressY._EnLocMove);
                    System.Threading.Thread.Sleep(200);
                    writeResultRender(melsecSerial.Write(st_AddressY._EnLocMove, false), st_AddressY._EnLocMove);
                }
                catch (Exception ex)
                {
                    //MessageBox.Show(ex.Message);
                }
                break;

            default:
                break;
            }
        }
        public void WriteExample( )
        {
            #region WriteExample1

            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( );

            // 此处以D寄存器作为示例
            melsecFx.Write("D100", (short)1234);                  // 写入D1000  short值  ,W3C0,R3C0 效果是一样的
            melsecFx.Write("D100", (ushort)45678);                // 写入D1000  ushort值
            melsecFx.Write("D100", 1234566);                      // 写入D1000  int值
            melsecFx.Write("D100", (uint)1234566);                // 写入D1000  uint值
            melsecFx.Write("D100", 123.456f);                     // 写入D1000  float值
            melsecFx.Write("D100", 123.456d);                     // 写入D1000  double值
            melsecFx.Write("D100", 123456661235123534L);          // 写入D1000  long值
            melsecFx.Write("D100", 523456661235123534UL);         // 写入D1000  ulong值
            melsecFx.Write("D100", "K123456789");                 // 写入D1000  string值

            // 读取数组
            melsecFx.Write("D100", new short[] { 123, 3566, -123 });                                    // 写入D1000  short值  ,W3C0,R3C0 效果是一样的
            melsecFx.Write("D100", new ushort[] { 12242, 42321, 12323 });                               // 写入D1000  ushort值
            melsecFx.Write("D100", new int[] { 1234312312, 12312312, -1237213 });                       // 写入D1000  int值
            melsecFx.Write("D100", new uint[] { 523123212, 213, 13123 });                               // 写入D1000  uint值
            melsecFx.Write("D100", new float[] { 123.456f, 35.3f, -675.2f });                           // 写入D1000  float值
            melsecFx.Write("D100", new double[] { 12343.542312d, 213123.123d, -231232.53432d });        // 写入D1000  double值
            melsecFx.Write("D100", new long[] { 1231231242312, 34312312323214, -1283862312631823 });    // 写入D1000  long值
            melsecFx.Write("D100", new ulong[] { 1231231242312, 34312312323214, 9731283862312631823 }); // 写入D1000  ulong值

            #endregion
        }
Пример #9
0
 private void button24_Click(object sender, EventArgs e)
 {
     // bool写入
     DemoUtils.WriteResultRender(() => melsecSerial.Write(textBox8.Text, bool.Parse(textBox7.Text)), textBox8.Text);
 }