ModbusTcp调试用例

namespace Modbus_TCP_Client
{
    public partial class Form1 : Form
    {
        public Socket newclient;
        public bool Connected;
        public Thread myThread;
        public delegate void MyInvoke(string str);
        public Form1()
        {
            InitializeComponent();
        }

        private void exit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        public void Connect()
        {
            byte[] data = new byte[1024];

            string ipadd = serverIP.Text.Trim();//将服务器IP地址存放在字符串 ipadd中
            int port = Convert.ToInt32(serverPort.Text.Trim());//将端口号强制为32位整型,存放在port中

            //创建一个套接字

            IPEndPoint ie = new IPEndPoint(IPAddress.Parse(ipadd), port);
            newclient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);


            //将套接字与远程服务器地址相连
            try
            {
                newclient.Connect(ie);
                connect.Enabled = false;//使连接按钮变成虚的,无法点击
                Connected = true;

            }
            catch (SocketException e)
            {
                MessageBox.Show("连接服务器失败  " + e.Message);
                return;
            }

            ThreadStart myThreaddelegate = new ThreadStart(ReceiveMsg);
            myThread = new Thread(myThreaddelegate);
            myThread.Start();
            timersend.Enabled = true;

        }

        private void connect_Click_1(object sender, EventArgs e)
        {
            Connect();
        }

        private void timersend_Tick(object sender, EventArgs e)
        {
            int isecond = 5000;//以毫秒为单位
            timersend.Interval = isecond;//5秒触发一次
            byte[] data = new byte[] { 0x00, 0x0f, 0x00, 0x00, 0x00, 0x06, 0x01, 0x04, 0x00, 0x00, 0x00, 0x01 };//这里我们不讲解04功能码,所以这里用04功能码发送,防止对其他功能码演示的干扰
            newclient.Send(data);
        }

        public void ReceiveMsg()
        {
            while (true)
            {
                byte[] data = new byte[1024];
                newclient.Receive(data);
                int length = data[5];
                Byte[] datashow = new byte[length + 6];
                for (int i = 0; i <= length + 5; i++)
                    datashow[i] = data[i];
                string stringdata = BitConverter.ToString(datashow);//把数组转换成16进制字符串
                if (data[7] == 0x01) { showMsg01(stringdata + "\r\n"); };
                if (data[7] == 0x02) { showMsg02(stringdata + "\r\n"); };
                if (data[7] == 0x03) { showMsg03(stringdata + "\r\n"); };
                if (data[7] == 0x05) { showMsg05(stringdata + "\r\n"); };
                if (data[7] == 0x06) { showMsg06(stringdata + "\r\n"); };
                if (data[7] == 0x0F) { showMsg0F(stringdata + "\r\n"); };
                if (data[7] == 0x10) { showMsg10(stringdata + "\r\n"); };
            }
        }

        private void send01_Click(object sender, EventArgs e)
        {
            //0x00 0x14 起始地址
            //0x00 0x13 输出数量
            byte[] data = new byte[] { 0x00, 0x01, 0x00, 0x00, 0x00, 0x06, 0x01, 0x01, 0x00, 0x14, 0x00, 0x13 };
            newclient.Send(data);
        }

        private void send01_21_Click(object sender, EventArgs e)
        {
            byte[] data = new byte[] { 0x00, 0x01, 0x00, 0x00, 0x00, 0x06, 0x01, 0x01, 0x00, 0x15, 0x00, 0x13 };
            newclient.Send(data);
        }

        private void send01_4151_Click(object sender, EventArgs e)
        {
            byte[] data = new byte[] { 0x00, 0x01, 0x00, 0x00, 0x00, 0x06, 0x01, 0x01, 0x10, 0x37, 0x00, 0x13 };
            newclient.Send(data);
        }

        public void showMsg01(string msg)
        {

            //在线程里以安全方式调用控件
            if (receive0x01.InvokeRequired)
            {
                MyInvoke _myinvoke = new MyInvoke(showMsg01);
                receive0x01.Invoke(_myinvoke, new object[] { msg });
            }
            else
            {
                //接收数据格式
                //0x03      字节数
                //0x??      接收状态(按位读取)
                receive0x01.AppendText(msg);
            }

        }

        private void send02_Click(object sender, EventArgs e)
        {
            byte[] data = new byte[] { 0x00, 0x01, 0x00, 0x00, 0x00, 0x06, 0x01, 0x02, 0x00, 0xC5, 0x00, 0x16 };
            newclient.Send(data);
        }

        private void send02_21_Click(object sender, EventArgs e)
        {
            byte[] data = new byte[] { 0x00, 0x01, 0x00, 0x00, 0x00, 0x06, 0x01, 0x02, 0x00, 0x15, 0x00, 0x16 };
            newclient.Send(data);

        }

        private void send02_4151_Click(object sender, EventArgs e)
        {
            byte[] data = new byte[] { 0x00, 0x01, 0x00, 0x00, 0x00, 0x06, 0x01, 0x02, 0x10, 0x37, 0x00, 0x16 };
            newclient.Send(data);

        }

        public void showMsg02(string msg)
        {

            //在线程里以安全方式调用控件
            if (receive0x01.InvokeRequired)
            {
                MyInvoke _myinvoke = new MyInvoke(showMsg02);
                receive0x02.Invoke(_myinvoke, new object[] { msg });
            }
            else
            {
                receive0x02.AppendText(msg);
            }

        }

        private void send03_Click(object sender, EventArgs e)
        {
            byte[] data = new byte[] { 0x00, 0x01, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x00, 0x6C, 0x00, 0x03 };
            newclient.Send(data);
        }

        private void send03_21_Click(object sender, EventArgs e)
        {
            byte[] data = new byte[] { 0x00, 0x01, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x00, 0x15, 0x00, 0x03 };
            newclient.Send(data);
        }

        private void send03_4151_Click(object sender, EventArgs e)
        {
            byte[] data = new byte[] { 0x00, 0x01, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x10, 0x37, 0x00, 0x03 };
            newclient.Send(data);

        }

        public void showMsg03(string msg)
        {

            //在线程里以安全方式调用控件
            if (receive0x01.InvokeRequired)
            {
                MyInvoke _myinvoke = new MyInvoke(showMsg03);
                receive0x03.Invoke(_myinvoke, new object[] { msg });
            }
            else
            {
                receive0x03.AppendText(msg);
            }

        }

        private void send05_Click(object sender, EventArgs e)
        {
            byte[] data = new byte[] { 0x00, 0x01, 0x00, 0x00, 0x00, 0x06, 0x01, 0x05, 0x00, 0xAD, 0xFF, 0x00 };
            newclient.Send(data);
        }

        public void showMsg05(string msg)
        {

            //在线程里以安全方式调用控件
            if (receive0x05.InvokeRequired)
            {
                MyInvoke _myinvoke = new MyInvoke(showMsg05);
                receive0x05.Invoke(_myinvoke, new object[] { msg });
            }
            else
            {
                receive0x05.AppendText(msg);
            }

        }

        private void send06_Click(object sender, EventArgs e)
        {
            byte[] data = new byte[] { 0x00, 0x01, 0x00, 0x00, 0x00, 0x06, 0x01, 0x06, 0x00, 0x01, 0x00, 0x03 };
            newclient.Send(data);
        }

        public void showMsg06(string msg)
        {

            //在线程里以安全方式调用控件
            if (receive0x06.InvokeRequired)
            {
                MyInvoke _myinvoke = new MyInvoke(showMsg06);
                receive0x06.Invoke(_myinvoke, new object[] { msg });
            }
            else
            {
                receive0x06.AppendText(msg);
            }

        }

        private void send0F_Click(object sender, EventArgs e)
        {
            byte[] data = new byte[] { 0x00, 0x01, 0x00, 0x00, 0x00, 0x09, 0x01, 0x0F, 0x00, 0x14, 0x00, 0x0A, 0x02, 0xCD, 0x01 };
            newclient.Send(data);
        }

        public void showMsg0F(string msg)
        {

            //在线程里以安全方式调用控件
            if (receive0x0F.InvokeRequired)
            {
                MyInvoke _myinvoke = new MyInvoke(showMsg0F);
                receive0x0F.Invoke(_myinvoke, new object[] { msg });
            }
            else
            {
                receive0x0F.AppendText(msg);
            }

        }

        private void send10_Click(object sender, EventArgs e)
        {
            byte[] data = new byte[] { 0x00, 0x01, 0x00, 0x00, 0x00, 0x0B, 0x01, 0x10, 0x00, 0x02, 0x00, 0x02, 0x04, 0x00,0x0A,0x01, 0x02 };
            newclient.Send(data);
        }

        public void showMsg10(string msg)
        {

            //在线程里以安全方式调用控件
            if (receive0x10.InvokeRequired)
            {
                MyInvoke _myinvoke = new MyInvoke(showMsg10);
                receive0x10.Invoke(_myinvoke, new object[] { msg });
            }
            else
            {
                receive0x10.AppendText(msg);
            }

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            RegistryKey key = Registry.CurrentUser.OpenSubKey("Software\\ModbusTcpClient", false);
            if (key == null)
            {
                key = Registry.CurrentUser.CreateSubKey("Software\\ModbusTcpClient");
            }
            if (key != null)
            {
                string strKeyValue;
                strKeyValue = key.GetValue("Server", "192.168.1.10").ToString();
                if (strKeyValue.Length > 0)
                {
                    serverIP.Text = strKeyValue;
                }

                strKeyValue = key.GetValue("Port", "502").ToString();
                if (strKeyValue.Length > 0)
                {
                    serverPort.Text = strKeyValue;
                }

                key.Close();
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            RegistryKey key = Registry.CurrentUser.OpenSubKey("Software\\ModbusTcpClient", true);
            if (key != null)
            {
                string strKeyValue = serverIP.Text.Trim();
                key.SetValue("Server", strKeyValue);

                strKeyValue = serverPort.Text.Trim();
                key.SetValue("Port", strKeyValue);

                key.Close();
            }
        }
    }
}

展开阅读全文

没有更多推荐了,返回首页