Beispiel #1
0
        private void userButton2_Click(object sender, EventArgs e)
        {
            // D100-D104读取
            OperateResult <byte[]> read = melsec_net.ReadFromPLC(MelsecDataType.D, 100, 5);

            if (read.IsSuccess)
            {
                // 成功读取,提取各自的值,此处的值有个前提假设,假设PLC上的数据是有符号的数据,表示-32768-32767
                short D100 = melsec_net.GetShortFromBytes(read.Content, 0);
                short D101 = melsec_net.GetShortFromBytes(read.Content, 1);
                short D102 = melsec_net.GetShortFromBytes(read.Content, 2);
                short D103 = melsec_net.GetShortFromBytes(read.Content, 3);
                short D104 = melsec_net.GetShortFromBytes(read.Content, 4);
                TextBoxAppendStringLine("D100:" + D100);
                TextBoxAppendStringLine("D101:" + D101);
                TextBoxAppendStringLine("D102:" + D102);
                TextBoxAppendStringLine("D103:" + D103);
                TextBoxAppendStringLine("D104:" + D104);
                //================================================================================
                //这两种方式一样的,如果是无符号的,则使用 ushort D100 = BitConverter.ToUInt16(read.Content, 0);//0-65535
                //short D100 = BitConverter.ToInt16(read.Content, 0);
                //short D101 = BitConverter.ToInt16(read.Content, 2);
                //short D102 = BitConverter.ToInt16(read.Content, 4);
                //short D103 = BitConverter.ToInt16(read.Content, 6);
                //short D104 = BitConverter.ToInt16(read.Content, 8);
            }
            else
            {
                //失败读取
                MessageBox.Show(read.ToMessageShowString());
            }
        }