MelsecFxSerialOverTcpWriteAsync 方法 (String, Byte) |
命名空间: HslCommunication.Profinet.Melsec
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 );
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值