提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
前言
开发语言:c#,DTU为宏电7710DTU,数据来源为压力和流量数据,压力监测设备为山东力创压力变送器,流量监测设备为E+H流量计。
DTU客户端设置内容为:自定义注册包,自定义心跳包。
一、软件架构
软件为C#控制台程序,通过TCP/IP协议与DTU模块建立通讯,监听DTU数据并进行处理。
二、使用步骤
1.引入库
代码如下(示例):
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
2.示例代码
代码如下(示例):
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
namespace SocketServerDTU
{
class Program
{
//和客户端通信的套接字
static Socket client_socket = null;
//集合:存储客户端信息
static Dictionary<string, Socket> clientConnectionItems = new Dictionary<string, Socket> {
};
//数据库连接字符串
private static string ConServerStr = "Data Source='xxx.xxx.0.52';Initial Catalog=Sensor;User ID=xxxxxx;Password=xxxxxxxxxx"; //设置数据库IP地址,数据库名称,数据库帐号,密码
private static SqlConnection sqlCon = null;
static void Main(string[] args)
{
//和客户端通信的套接字:监听客户端发来的消息,三个参数:IP4寻址协议,流式连接,tcp协议
client_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//服务端发送信息需要一个IP地址和端口号
IPAddress address = IPAddress.Parse("***.***.0.***");
//将IP地址和端口号绑定到网络节点point上
IPEndPoint point = new IPEndPoint(address,5000 );//5000端口门用来监听,5000为本机未占用的端口,可自定义
//监听绑定的网络节点
client_socket.Bind(point);
//将套接字的监听队列长度限制为20;
client_socket.Listen(20);
//负责监听客户端的线程
Thread client_thread = new Thread(watchconnecting);
//将窗体线程设置为与后台同步,跟随主线程结束而结束
client_thread.IsBackground = true;
//启动线程
client_thread.Start();
Console.BackgroundColor = ConsoleColor.Black;
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("开启监听......");
Console.WriteLine("点击输入任意数据回车退出程序......");
Console.ReadKey();
Console.WriteLine("退出监听,并关闭程序");
}
//监听客户端发来的请求
static void watchconnecting()
{
Socket connection = null;
//持续不断监听客户端发来的请求
while (true)
{
try
{
connection = client_socket.Accept();
Console.WriteLine("连接时间:" + DateTime.Now.ToString());
}
catch (Exception e)
{
Console.WriteLine(e.Message);
break;
}
//获取客户端的IP和端口号
IPAddress clientIP = (connection.RemoteEndPoint as IPEndPoint).Address;
int clientPort = (connection.RemoteEndPoint as IPEndPoint).Port;
//让客户显示“连接成功的信息”
string sendMsg = "连接服务端成功!\r\n" + "本地IP:" + clientIP + ",本地端口:" + clientPort.ToString();
//byte[] arrSendMsg = Encoding.UTF8.GetBytes(sendMsg);
//connection.Send(arrSendMsg);
//客户端网络结点号
string remoteEndPoint = connection.RemoteEndPoint.ToString();
//显示与客户端连接情况:
Console.WriteLine("成功与" + remoteEndPoint + "客户端建立连接!\t\n");
//添加客户端信息
clientConnectionItems.Add(remoteEndPoint, connection);
IPEndPoint netpoint = connection.RemoteEndPoint as IPEndPoint;
//创建一个线程通信
ParameterizedThreadStart pts = new ParameterizedThreadStart(recv);
Thread thread = new Thread(pts);
//设置后台进程,跟随主线程退出而退出
thread.IsBackground = true;
//启动线程
thread.Start(connection);
}
}
//接收客户端发来的消息,客户端套接字对象
static void recv(object socketclientpara)
{
Socket socketServer = socketclientpara as Socket;
//创建一个内存缓冲区,其大小为1024*1024字节,及1M
byte[] arrServiceRecMsg = new byte[40];
//判断数据表中是否存在该设备
bool haveDevice = false;
//判断是否为十六进制数据,是:设备类型为流量计,否:设备类型为压力计
bool ifHex = false;
//监测点属性
sensorAttribute att = new sensorAttribute();
//用于连接数据库
SqlCommand cmd = null;
SqlDataReader reader = null;
if (sqlCon == null)
{