IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 网络协议 -> C# TCP通讯客户端源码 -> 正文阅读

[网络协议]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;
            }
        }    
    }
}

  网络协议 最新文章
使用Easyswoole 搭建简单的Websoket服务
常见的数据通信方式有哪些?
Openssl 1024bit RSA算法---公私钥获取和处
HTTPS协议的密钥交换流程
《小白WEB安全入门》03. 漏洞篇
HttpRunner4.x 安装与使用
2021-07-04
手写RPC学习笔记
K8S高可用版本部署
mySQL计算IP地址范围
上一篇文章      下一篇文章      查看所有文章
加:2022-03-15 23:04:40  更:2022-03-15 23:05:12 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/26 6:44:21-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码