C# Modbus-TCP协议的实现

1、模拟测试界面
C# Modbus-TCP协议的实现_第1张图片

2、编程实现

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            cbbWriteFunction.SelectedIndex = 0;

        }
        /// 
        /// TextBox只允许16进制输入
        /// 
        /// 
        /// 
        private void HexInput_KeyPress(object sender, KeyPressEventArgs e)
        {
            // 0-9 a-z A-Z Backsppace按键可输入
            if ((e.KeyChar >= '0' && e.KeyChar <= '9') || (e.KeyChar >= 'a' && e.KeyChar <= 'f') || (e.KeyChar >= 'A' && e.KeyChar <= 'F') || e.KeyChar == '\b')
            {
                if (e.KeyChar >= 'a' && e.KeyChar <= 'f')
                {
                    e.KeyChar = Convert.ToChar(e.KeyChar.ToString().ToUpper());
                }
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }
        private void WriteFunctionChanged(int index)
        {
            switch (index)
            {
                case 0:
                case 1:
                    this.lbWriteAddress.Text = "起始地址";
                    this.tbWriteAdress.Text = "0";
                    this.lbWriteQuantity.Text = "线圈数量";
                    this.tbWriteQuantity.Text = "0";
                    this.lbWriteBytes.Visible = false;
                    this.tbWriteBytes.Visible = false;
                    break;
                case 2:
                case 3:
                    this.lbWriteAddress.Text = "起始地址";
                    this.tbWriteAdress.Text = "0";
                    this.lbWriteQuantity.Text = "寄存器数量";
                    this.tbWriteQuantity.Text = "0";
                    this.lbWriteBytes.Visible = false;
                    this.tbWriteBytes.Visible = false;
                    break;
                case 4:
                    this.lbWriteAddress.Text = "线圈地址";
                    this.tbWriteAdress.Text = "0";
                    this.lbWriteQuantity.Text = "线圈值";
                    this.tbWriteQuantity.Text = "0";
                    this.lbWriteBytes.Visible = false;
                    this.tbWriteBytes.Visible = false;
                    break;
                case 5:
                    this.lbWriteAddress.Text = "寄存器地址";
                    this.tbWriteAdress.Text = "0";
                    this.lbWriteQuantity.Text = "寄存器值";
                    this.tbWriteQuantity.Text = "0";
                    this.lbWriteBytes.Visible = false;
                    this.tbWriteBytes.Visible = false;
                    break;
                case 6:
                    this.lbWriteAddress.Text = "起始地址";
                    this.tbWriteAdress.Text = "0";
                    this.lbWriteQuantity.Text = "输出数量";
                    this.tbWriteQuantity.Text = "0";
                    this.lbWriteBytes.Visible = true;
                    this.lbWriteBytes.Text = "输出值";
                    this.tbWriteBytes.Visible = true;
                    this.tbWriteBytes.Text = "01 00";
                    break;
                case 7:
                    this.lbWriteAddress.Text = "起始地址";
                    this.tbWriteAdress.Text = "0";
                    this.lbWriteQuantity.Text = "寄存器数量";
                    this.tbWriteQuantity.Text = "0";
                    this.lbWriteBytes.Visible = true;
                    this.lbWriteBytes.Text = "寄存器值";
                    this.tbWriteBytes.Visible = true;
                    this.tbWriteBytes.Text = "01 00 00";
                    break;
            }
        }

        private void cbbWriteFunction_SelectedIndexChanged(object sender, EventArgs e)
        {
            WriteFunctionChanged(this.cbbWriteFunction.SelectedIndex);
        }
        private ushort serialNumber = 0;
        private void btWriteOK_Click(object sender, EventArgs e)
        {
            int index = this.cbbWriteFunction.SelectedIndex;
            ushort address0 = Convert.ToUInt16(this.tbWriteAdress.Text.Trim(), 16);
            byte[] address = new byte[2];
            address[0] = (byte)((address0 >> 8) & (0xFF));
            address[1] = (byte)(address0 & 0xFF);
            ushort quantity0 = Convert.ToUInt16(this.tbWriteQuantity.Text.Trim(), 16);
            byte[] quantity = new byte[2];
            quantity[0] = (byte)((quantity0 >> 8) & (0xFF));
            quantity[1] = (byte)(quantity0 & 0xFF);
            byte[] data;
            if (this.tbWriteBytes.Visible)
            {
                string[] arr = this.tbWriteBytes.Text.Split(' ');
                if (arr.Length > 0)
                {
                    data = new byte[arr.Length];
                    for (int i = 0; i < arr.Length; i++)
                    {
                        byte b = byte.Parse(arr[i]);
                        data[i] = b;
                    }
                }
            }
            switch (index)
            {
                case 0:
                    byte[] pdu = new byte[5];
                    pdu[0] = 0x01;
                    Buffer.BlockCopy(address, 0, pdu, 1, 2);
                    Buffer.BlockCopy(quantity, 0, pdu, 3, 2);
                    byte[] sendData = GetProtocolHead(0x00, pdu);
                    lbWriteData.Text = BitConverter.ToString(sendData).Replace('-', ' ');
                    break;
            }
        }
        private static byte[] MODBUS_PROTOCOL_MARK = new byte[2] { 0x00, 0x00 };
        private byte[] GetProtocolHead(byte device, byte[] data)
        {
            ushort len = (ushort)data.Length;
            byte[] b = new byte[7 + len];
            byte[] b1 = ushort2Byte(serialNumber);
            serialNumber++;
            Buffer.BlockCopy(b1, 0, b, 0, 2);
            Buffer.BlockCopy(MODBUS_PROTOCOL_MARK, 0, b, 2, 2);
            b1 = ushort2Byte(len);
            Buffer.BlockCopy(b1, 0, b, 4, 2);
            b[6] = device;
            Buffer.BlockCopy(data, 0, b, 7, len);
            return b;
        }
        public static byte[] ushort2Byte(ushort s)
        {
            byte[] b = BitConverter.GetBytes(s);
            Array.Reverse(b);
            return b;
        }
    }

未完

你可能感兴趣的:(C#,c#,开发语言)