TCP读写器介绍:https://item.taobao.com/item.htm?spm=a1z10.5-c.w4002-17663462238.13.45d75b43xb6FYD&id=601009585329https://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;}}    }
}

C# TCP通讯客户端源码相关推荐

  1. 【微信小程序控制硬件②】 开始微信小程序之旅,导入小程序Mqtt客户端源码,实现简单的验证和通讯于服务器.(附带源码)

    文章目录 一.前言: 二.注册微信小程序: 三.本博文连接和微信物联有何区别: 四.微信小程序`MQTT`客户端源码导入注意事项: 五.下载: 微信物联网生态主要分在微信硬件开发平台与腾讯物联开发平台 ...

  2. boost::asio异步模式的C/S客户端源码实现

    异步模式的服务器源码 //g++ -g async_tcp_server.cpp -o async_tcp_server -lboost_system //#include <iostream& ...

  3. zookeeper 客户端_zookeeper进阶-客户端源码详解

    流程图 先看一下客户端源码的流程图 总体流程 总体流程 开启SendThread线程 开启EventThread 总结 下面根据源码讲解,大家整合源码和流程图一起看最好,本篇内容比较多建议收藏起来看. ...

  4. IOCP 网络通讯模型源码解读

    From: http://hi.baidu.com/tsingsing/item/1aa5062fa27791fa50fd87b7 以前写服务器的时候用的是iocp,最近偶然发现windows的 网络 ...

  5. 2021最新4合1即时通讯IM源码-服务端+PC+WEB+安卓+IOS完整原生源码

    介绍: 20214合1即时通讯IM源码 服务端+PC+WEB+安卓+IOS完整原生源码 附完整开发文档+视频搭建教程. 注意:此源码亲测可用,他处有小问题,我们已经修复.任何源码,难免有瑕疵,但不影响 ...

  6. grpc-go客户端源码分析

    grpc-go客户端源码分析 代码讲解基于v1.37.0版本. 和grpc-go服务端源码分析一样,我们先看一段示例代码, const (address = "localhost:50051 ...

  7. Libcurl的编译_HTTP/HTTPS客户端源码示例

    HTTP/HTTPS客户端源码示例 环境:  zlib-1.2.8  openssl-1.0.1g  curl-7.36 Author:  Kagula LastUpdateDate: 2016-05 ...

  8. mosquitto客户端对象“struct mosquitto *mosq”管理下篇(mosquitto2.0.15客户端源码分析之四)

    文章目录 前言 5 设置网络参数 5.1 客户端连接服务器使用的端口号 `mosq->port` 5.2 指定绑定的网络地址 `mosq->bind_address` 5.3 客户端连接服 ...

  9. PHP开发的H5即时通讯聊天系统源码 带群聊 可封装APP

    H5即时通讯聊天系统源码,源码可用于聊天.交友.客服等平台,可创建群聊,可添加好友,可发朋友圈,运行稳定,支持封装APP. 源码带有详细文档搭建教程,直接使用宝塔搭建即可,前端需要使用工具运行和编译, ...

最新文章

  1. 分布式锁之三:Redlock实现分布式锁
  2. 查看mysql版本的四种方法
  3. 系统服务器端口怎么关闭了,如何关闭云服务器端口号
  4. Vue——prop双向绑定解决方案
  5. Nexys4DDR的DDR2读写操作例程详解
  6. Mongodb带验证的主从复制架构
  7. 深度解析单例与序列化之间的爱恨情仇
  8. Euler 今日问世!国内首个工业级的图深度学习开源框架,阿里妈妈造
  9. (9)FPGA复位设计(第2天)
  10. Vue父组件监听子组件调用删除模块(个性化页面设置会使用到)
  11. [转]nodejs npm常用命令
  12. mac上远程连接windows
  13. 改善代码设计 —— 处理概括关系(Dealing with Generalization)
  14. 多线程-线程间通信-多生产者多消费者示例
  15. 自定义控件2.第一个自定义view
  16. 【python基础】h5py库的基本使用
  17. ua获取手机型号_JavaScript获取移动设备型号的实现代码(JS获取手机型号和系统)...
  18. PS一键磨皮插件磨皮DR4.5中文增强版(PS CC 2014-2019),人像修图调色软件
  19. matlab求dfa指数,关于使用MF-DFA方法计算广义Hurst指数的MATLAB操作问题
  20. NLP 之 jieba (结巴)制作词云

热门文章

  1. 如何实现get接口与post接口转换,get接口与post接口如何转换
  2. 服务器自检后显示scsi,SCSI RAID卡常见故障分析及BIOS升级指导
  3. 2010年程序员工资
  4. 官网教程:什么是Apache Hadoop?
  5. 水利水电之水利工程测量第二章
  6. Excel求解最优化问题(有具体步骤)
  7. FPS游戏初开发--逻辑分析总结
  8. PSPNet Caffe 编译安装
  9. 一个C语言面试的经典例题
  10. js随机生成彩票号码 双色球