C# TCP通讯客户端源码

TCP读写器介绍:https://item.taobao.com/item.htm?spm=a1z10.5-c.w4002-17663462238.13.45d75b43xb6FYD&id=601009585329icon-default.png?t=M276https://item.taobao.com/item.htm?spm=a1z10.5-c.w4002-17663462238.13.45d75b43xb6FYD&id=601009585329 

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Management;

namespace TCPIP接口读卡器C井2010例子代码
{
    public partial class Form8 : Form
    {
        private TcpClient tcpClient = null;
        private IPAddress ipaddress;
        int remortprot;
        public Boolean connok;
        private Thread thread;   

        Form1 f1;
        
        public Form8()
        {
            InitializeComponent();
            
        }

        private void Form8_Load(object sender, EventArgs e)
        {
            f1 = (Form1)this.Owner;
            f1.Refresh();

            string connip = f1.textBox2.Text + "." + f1.textBox3.Text + "." + f1.textBox4.Text + "." + f1.textBox5.Text;
            ipaddress = IPAddress.Parse(connip);
            remortprot = Convert.ToInt32(f1.textBox8.Text);
        }


        private void button1_Click(object sender, EventArgs e)
        {
            if (button1.Text == "建立与读写器的TCP连接")
            {
                try
                {
                    tcpClient = new TcpClient();
                    tcpClient.Connect(ipaddress, remortprot);       //阻塞式同步通讯工作方式        
                    if (tcpClient.Connected)
                    {
                        connok = true;
                        button1.Text = "断开与读写器的TCP连接";
                        panel1.Enabled = true;
                    
                        thread = new Thread(new ThreadStart(this.ReceiveHandle));
                        thread.Start();
                    }
                }
                catch
                {
                    MessageBox.Show("TCP连接失败,请确定MODBUS TCP读写器已经开启!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                connok = false;                
                button1.Text = "建立与读写器的TCP连接";
                panel1.Enabled = false;
                tcpClient.Close();
            }
        }

        private void ReceiveHandle()
        {
            string recestr;
            int i;
            try
            {
                while (connok)
                {
                    byte[] bytes = new byte[100];
                    NetworkStream stream;
                    stream = tcpClient.GetStream();                       //获取网络流
                    lock (stream) ;                                       //为了保证数据的完整性以及安全性 锁定数据流
                    int bytesRead = stream.Read(bytes, 0, bytes.Length);  //读取返回信息

                    recestr = "FromIP:" + (ipaddress + ":" + remortprot.ToString().Trim() + "                        ").Substring(0, 22) + "电脑接收:";
                    for (i = 0; i < bytesRead; i++)
                    {
                        recestr = recestr + bytes[i].ToString("X2") + " ";
                    }
                    f1.listBox1.Items.Add(recestr);
                    f1.listBox1.SelectedIndex = f1.listBox1.Items.Count - 1;

                    if (bytes[0] == 0x55 && bytes.Length>11)
                    {
                        switch (bytes[1])
                        {
                            case 0x05: ;
                                 DialogResult dr = MessageBox.Show("读写器已执行一次读卡操作,是否要读出寄存器内数!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                                 if (dr == DialogResult.OK)
                                 {
                                     beginbuf.Value = 7;
                                     button4_Click(button4, EventArgs.Empty);
                                 }
                                 break;
                            case 0x06: ;
                                 recestr = "";
                                 for (i = 9; i <= 9 + bytes[8];i++ )
                                 {
                                     recestr = recestr + bytes[i].ToString("X2") + " ";
                                 }
                                 textBox1.Text = recestr;
 
                                 break;                               
                            case 0x08:;
                                 if (beginbuf.Value >= 10)
                                 {
                                     DialogResult dr1 = MessageBox.Show("    存放IC卡扇区数据的寄存器数据更改成功,是否要驱动读卡器执行写卡操作?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                                     if (dr1 == DialogResult.OK)
                                     {
                                         beginbuf.Value = 7;
                                         button5_Click(button5, EventArgs.Empty);
                                     }
                                 }
                                  break;                               
                        }

                    }
                }
            }
            catch
            {

            }
        }
        private void button32_Click(object sender, EventArgs e)
        {
            byte[] sendbuf = new byte[12];
            sendbuf[0] = 0x55;
            sendbuf[1] = 0x01;
            sendbuf[2] = 0x00;    //MODBUS TCP协议标识
            sendbuf[3] = 0x00;
            sendbuf[4] = 0x00;
            sendbuf[5] = 0x06;    //指令长度
            sendbuf[6] = 0x00;
            sendbuf[7] = 0x06;    //功能码
            sendbuf[8] = 0x00;
            sendbuf[9] = 0x02;    //寄存器地址
            sendbuf[10] = 0x00;
            sendbuf[11] = (byte)(comboBox7.SelectedIndex + 1);

            try
            {
                NetworkStream stream;
                stream = tcpClient.GetStream();          //获取网络流
                //lock (stream) ;                           //为了保证数据的完整性以及安全性 锁定数据流
                stream.Write(sendbuf, 0, sendbuf.Length);//将数据写入网络流
                string recestr = "";
                recestr = "SendTo:" + (ipaddress + ":" + remortprot.ToString() .Trim() + "                        ").Substring(0, 22) + "电脑发送:";
                for (int i = 0; i < 12; i++)
                {
                    recestr = recestr + sendbuf[i].ToString("X2") + " ";
                }
                f1.listBox1.Items.Add(recestr);
                f1.listBox1.SelectedIndex = f1.listBox1.Items.Count - 1;
            }
            catch
            {
                connok = false;
                tcpClient.Close();
                button1.Text = "建立与读写器的TCP连接";
                panel1.Enabled = false;
            }
        }

        private void Form8_Load()
        {
            f1 = (Form1)this.Owner;
            f1.Refresh();

            string connip = f1.textBox2.Text + "." + f1.textBox3.Text + "." + f1.textBox4.Text + "." + f1.textBox5.Text;
            ipaddress = IPAddress.Parse(connip);
            remortprot = Convert.ToInt32(f1.textBox8.Text);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            byte[] sendbuf = new byte[12];
            sendbuf[0] = 0x55;
            sendbuf[1] = 0x02;
            sendbuf[2] = 0x00;    //MODBUS TCP协议标识
            sendbuf[3] = 0x00;
            sendbuf[4] = 0x00;
            sendbuf[5] = 0x06;    //指令长度
            sendbuf[6] = 0x00;
            sendbuf[7] = 0x06;    //功能码
            sendbuf[8] = 0x00;
            sendbuf[9] = (byte)(0x46 + comboBox8.SelectedIndex);

            sendbuf[10] = (byte)(swithdely.Value / 256);  //延时
            sendbuf[11] = (byte)(swithdely.Value % 256);

            try
            {
                NetworkStream stream;
                stream = tcpClient.GetStream();          //获取网络流
                lock (stream) ;                           //为了保证数据的完整性以及安全性 锁定数据流
                stream.Write(sendbuf, 0, sendbuf.Length);//将数据写入网络流
                string recestr = "";
                recestr = "SendTo:" + (ipaddress + ":" + remortprot.ToString().Trim() + "                        ").Substring(0, 22) + "电脑发送:";
                for (int i = 0; i < 12; i++)
                {
                    recestr = recestr + sendbuf[i].ToString("X2") + " ";
                }
                f1.listBox1.Items.Add(recestr);
                f1.listBox1.SelectedIndex = f1.listBox1.Items.Count - 1;
            }
            catch
            {
                connok = false;
                tcpClient.Close();
                button1.Text = "建立与读写器的TCP连接";
                panel1.Enabled = false;
            }

        }

        private void button34_Click(object sender, EventArgs e)
        {
            byte[] sendbuf = new byte[12];
            sendbuf[0] = 0x55;
            sendbuf[1] = 0x03;
            sendbuf[2] = 0x00;    //MODBUS TCP协议标识
            sendbuf[3] = 0x00;
            sendbuf[4] = 0x00;
            sendbuf[5] = 0x06;    //指令长度
            sendbuf[6] = 0x00;
            sendbuf[7] = 0x06;    //功能码
            sendbuf[8] = 0x00;
            sendbuf[9] = (byte)(0x46 + comboBox8.SelectedIndex);

            sendbuf[10] = 0x00;   //延时
            sendbuf[11] = 0x00;

            try
            {
                NetworkStream stream;
                stream = tcpClient.GetStream();           //获取网络流
                lock (stream) ;                           //为了保证数据的完整性以及安全性 锁定数据流
                stream.Write(sendbuf, 0, sendbuf.Length); //将数据写入网络流
                string recestr = "";
                recestr = "SendTo:" + (ipaddress + ":" + remortprot.ToString().Trim() + "                        ").Substring(0, 22) + "电脑发送:";
                for (int i = 0; i < 12; i++)
                {
                    recestr = recestr + sendbuf[i].ToString("X2") + " ";
                }
                f1.listBox1.Items.Add(recestr);
                f1.listBox1.SelectedIndex = f1.listBox1.Items.Count - 1;
            }
            catch
            {
                connok = false;
                tcpClient.Close();
                button1.Text = "建立与读写器的TCP连接";
                panel1.Enabled = false;
            }
        }

        private void button35_Click(object sender, EventArgs e)
        {
            byte[] sendbuf = new byte[12];
            sendbuf[0] = 0x55;
            sendbuf[1] = 0x04;
            sendbuf[2] = 0x00;    //MODBUS TCP协议标识
            sendbuf[3] = 0x00;
            sendbuf[4] = 0x00;
            sendbuf[5] = 0x06;    //指令长度
            sendbuf[6] = 0x00;
            sendbuf[7] = 0x06;    //功能码
            sendbuf[8] = 0x00;
            sendbuf[9] = 0x01;

            if(checkBox1.Checked )
            {
                sendbuf[10] = 0x80;
                sendbuf[11] = (byte)(comboBox9.SelectedIndex + 2 * comboBox10.SelectedIndex);
            }
            else
            {
                sendbuf[10] = 0x00;
                sendbuf[11] = (byte)(comboBox9.SelectedIndex + 2 * comboBox10.SelectedIndex);
            }
              

            try
            {
                NetworkStream stream;
                stream = tcpClient.GetStream();           //获取网络流
                lock (stream) ;                           //为了保证数据的完整性以及安全性 锁定数据流
                stream.Write(sendbuf, 0, sendbuf.Length); //将数据写入网络流
                string recestr = "";
                recestr = "SendTo:" + (ipaddress + ":" + remortprot.ToString().Trim() + "                        ").Substring(0, 22) + "电脑发送:";
                for (int i = 0; i < 12; i++)
                {
                    recestr = recestr + sendbuf[i].ToString("X2") + " ";
                }
                f1.listBox1.Items.Add(recestr);
                f1.listBox1.SelectedIndex = f1.listBox1.Items.Count - 1;
            }
            catch
            {
                connok = false;
                tcpClient.Close();
                button1.Text = "建立与读写器的TCP连接";
                panel1.Enabled = false;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            byte[] sendbuf = new byte[12];
            sendbuf[0] = 0x55;
            sendbuf[1] = 0x05;
            sendbuf[2] = 0x00;    //MODBUS TCP协议标识
            sendbuf[3] = 0x00;
            sendbuf[4] = 0x00;
            sendbuf[5] = 0x06;    //指令长度
            sendbuf[6] = 0x00;
            sendbuf[7] = 0x06;    //功能码
            sendbuf[8] = 0x00;
            sendbuf[9] = 0x07;
            sendbuf[10] = 0x00;
            sendbuf[11] = 0x08;

            try
            {
                NetworkStream stream;
                stream = tcpClient.GetStream();          //获取网络流
                lock (stream) ;                           //为了保证数据的完整性以及安全性 锁定数据流
                stream.Write(sendbuf, 0, sendbuf.Length);//将数据写入网络流
                string recestr = "";
                recestr = "SendTo:" + (ipaddress + ":" + remortprot.ToString().Trim() + "                        ").Substring(0, 22) + "电脑发送:";
                for (int i = 0; i < 12; i++)
                {
                    recestr = recestr + sendbuf[i].ToString("X2") + " ";
                }
                f1.listBox1.Items.Add(recestr);
                f1.listBox1.SelectedIndex = f1.listBox1.Items.Count - 1;
            }
            catch
            {
                connok = false;
                tcpClient.Close();
                button1.Text = "建立与读写器的TCP连接";
                panel1.Enabled = false;
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            byte[] sendbuf = new byte[12];
            sendbuf[0] = 0x55;
            sendbuf[1] = 0x06;
            sendbuf[2] = 0x00;    //MODBUS TCP协议标识
            sendbuf[3] = 0x00;
            sendbuf[4] = 0x00;
            sendbuf[5] = 0x06;    //指令长度
            sendbuf[6] = 0x00;
            sendbuf[7] = 0x03;    //功能码
            sendbuf[8] = 0x00;
            sendbuf[9] = (byte)beginbuf.Value ;
            sendbuf[10] = 0x00;
            sendbuf[11] = (byte)bufnum.Value ;

            try
            {
                NetworkStream stream;
                stream = tcpClient.GetStream();          //获取网络流
                lock (stream) ;                           //为了保证数据的完整性以及安全性 锁定数据流
                stream.Write(sendbuf, 0, sendbuf.Length);//将数据写入网络流
                string recestr = "";
                recestr = "SendTo:" + (ipaddress + ":" + remortprot.ToString().Trim() + "                        ").Substring(0, 22) + "电脑发送:";
                for (int i = 0; i < 12; i++)
                {
                    recestr = recestr + sendbuf[i].ToString("X2") + " ";
                }
                f1.listBox1.Items.Add(recestr);
                f1.listBox1.SelectedIndex = f1.listBox1.Items.Count - 1;
            }
            catch
            {
                connok = false;
                tcpClient.Close();
                button1.Text = "建立与读写器的TCP连接";
                panel1.Enabled = false;
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            byte wrib1;
            byte wrib2;
            string wreitstr = textBox1.Text.Trim();

            try
            {
                wrib1 = Convert.ToByte(Convert.ToInt32(wreitstr.Substring(0, 2), 16));
                wrib2 = Convert.ToByte(Convert.ToInt32(wreitstr.Substring(3, 2), 16));
            }
            catch (Exception ex)
            {
                MessageBox.Show("请在右边栏中输入要写入的16进制数据!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            
            byte[] sendbuf = new byte[12];
            sendbuf[0] = 0x55;
            sendbuf[1] = 0x07;
            sendbuf[2] = 0x00;    //MODBUS TCP协议标识
            sendbuf[3] = 0x00;
            sendbuf[4] = 0x00;
            sendbuf[5] = 0x06;    //指令长度
            sendbuf[6] = 0x00;
            sendbuf[7] = 0x06;    //功能码
            sendbuf[8] = 0x00;
            sendbuf[9] = (byte)beginbuf.Value;
            sendbuf[10] = wrib1;
            sendbuf[11] = wrib2;

            try
            {
                NetworkStream stream;
                stream = tcpClient.GetStream();          //获取网络流
                lock (stream) ;                           //为了保证数据的完整性以及安全性 锁定数据流
                stream.Write(sendbuf, 0, sendbuf.Length);//将数据写入网络流
                string recestr = "";
                recestr = "SendTo:" + (ipaddress + ":" + remortprot.ToString().Trim() + "                        ").Substring(0, 22) + "电脑发送:";
                for (int i = 0; i < 12; i++)
                {
                    recestr = recestr + sendbuf[i].ToString("X2") + " ";
                }
                f1.listBox1.Items.Add(recestr);
                f1.listBox1.SelectedIndex = f1.listBox1.Items.Count - 1;
            }
            catch
            {
                connok = false;
                tcpClient.Close();
                button1.Text = "建立与读写器的TCP连接";
                panel1.Enabled = false;
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            byte[] Writbuf = new byte[48];
            int WritNum =(int)(bufnum.Value * 2);
            int i;

            string wreitstr = textBox1.Text.Trim();

            try
            {
                for (i = 0; i < WritNum;i++ )
                {
                    Writbuf[i] = Convert.ToByte(Convert.ToInt32(wreitstr.Substring(i*3, 2), 16));
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("请在右边栏中输入要写入的16进制数据,每个寄存器要写入2个字节数据!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            byte[] sendbuf = new byte[WritNum+13];
            sendbuf[0] = 0x55;
            sendbuf[1] = 0x08;
            sendbuf[2] = 0x00;    //MODBUS TCP协议标识
            sendbuf[3] = 0x00;
            sendbuf[4] = 0x00;
            sendbuf[5] = 0x06;    //指令长度
            sendbuf[6] = 0x00;
            sendbuf[7] = 0x10;    //功能码
            sendbuf[8] = 0x00;
            sendbuf[9] = (byte)beginbuf.Value;
            sendbuf[10] = 0x00;
            sendbuf[11] = (byte)bufnum.Value;
            sendbuf[12] = (byte)WritNum;

            for (i = 1; i <= WritNum; i++)
            {
                sendbuf[12 + i] = Writbuf[i - 1];
            }

            try
            {
                NetworkStream stream;
                stream = tcpClient.GetStream();          //获取网络流
                lock (stream) ;                           //为了保证数据的完整性以及安全性 锁定数据流
                stream.Write(sendbuf, 0, sendbuf.Length);//将数据写入网络流
                string recestr = "";
                recestr = "SendTo:" + (ipaddress + ":" + remortprot.ToString().Trim() + "                        ").Substring(0, 22) + "电脑发送:";
                for (i = 0; i <= 12 + WritNum; i++)
                {
                    recestr = recestr + sendbuf[i].ToString("X2") + " ";
                }
                f1.listBox1.Items.Add(recestr);
                f1.listBox1.SelectedIndex = f1.listBox1.Items.Count - 1;
            }
            catch
            {
                connok = false;
                tcpClient.Close();
                button1.Text = "建立与读写器的TCP连接";
                panel1.Enabled = false;
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            byte[] sendbuf = new byte[12];
            sendbuf[0] = 0x55;
            sendbuf[1] = 0x09;
            sendbuf[2] = 0x00;    //MODBUS TCP协议标识
            sendbuf[3] = 0x00;
            sendbuf[4] = 0x00;
            sendbuf[5] = 0x06;    //指令长度
            sendbuf[6] = 0x00;
            sendbuf[7] = 0x06;    //功能码
            sendbuf[8] = 0x00;
            sendbuf[9] = 0x07;
            sendbuf[10] = 0x00;
            sendbuf[11] = 0x04;

            try
            {
                NetworkStream stream;
                stream = tcpClient.GetStream();          //获取网络流
                lock (stream) ;                           //为了保证数据的完整性以及安全性 锁定数据流
                stream.Write(sendbuf, 0, sendbuf.Length);//将数据写入网络流
                string recestr = "";
                recestr = "SendTo:" + (ipaddress + ":" + remortprot.ToString().Trim() + "                        ").Substring(0, 22) + "电脑发送:";
                for (int i = 0; i < 12; i++)
                {
                    recestr = recestr + sendbuf[i].ToString("X2") + " ";
                }
                f1.listBox1.Items.Add(recestr);
                f1.listBox1.SelectedIndex = f1.listBox1.Items.Count - 1;
            }
            catch
            {
                connok = false;
                tcpClient.Close();
                button1.Text = "建立与读写器的TCP连接";
                panel1.Enabled = false;
            }
        }    
    }
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

vx_13822155058

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值