📄 formgprsdata.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Threading;
using System.Data;
using CommCLB.Comm;
using CommCLB.asswctrl_ws;
using System.Text;
using System.IO;
using System.Data.SqlTypes;
using System.Runtime.InteropServices;
using System.Net.Sockets;
using System.Net;
namespace GPRSDrvWinApp.gprs
{
//RTU物理通道配置信息
public struct RtuPackage
{
public uint m_DeviceID; //设备号
public uint m_ChannelCount; //通道个数
public uint[] m_ChannelWidth; //通道字节数
public string[] m_useFlag; //使用标志
public uint[] m_ValidChannelIndex; //有效通道索引号
public uint[] m_ValidChannelWidth; //有效通道字节数
}
/// <summary>
/// FormGprsData 的摘要说明。
/// </summary>
public class FormGprsData : System.Windows.Forms.Form
{
[DllImport("kernel32", SetLastError=true)] private static extern int GetPrivateProfileString(string section,string key,string def, StringBuilder retVal,ref int size,string filePath);
public const bool DEBUG = true;
protected string ModuleNO; //模块编号
public GprsComm gprsComm = new GprsComm();
//RTU物理通道基本信息Hashtable
public Hashtable htRtuPackage=new Hashtable();
public String DebugFileName;
public String DriverConfigureFileName;
public uint ExceptionCount = 0;
//线程:定义从GPRS接收实时数据并发送到中央控制程序
Thread tRecvFromGprs = null;
public DriverConfig driverCfg;
public delegate void showInfo_sub_delegate(string strInfo);
public delegate void showStatistic_sub_delegate(uint nTotal, uint nValid, uint nInvalid);
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.StatusBarPanel statusBarPanel1;
private System.Windows.Forms.StatusBarPanel statusBarPanel2;
private System.Windows.Forms.StatusBarPanel statusBarPanel3;
private System.Windows.Forms.ListBox lbData;
private System.Windows.Forms.StatusBar sbStatistic;
private System.Windows.Forms.Panel panel2;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
#region 初始化RTU物理通道基本信息的Hashtable
public bool InitHtRtuPackage()
{
//返回已配置有效通道的RTU记录
DataSet ds=Class_Comm.GetServiceAsswConf().GetRtuIDByPackage();
DataTable dt=ds.Tables["tb_nwRtuPackage"];
if (dt == null)
return false;
//返回所有RTU的通道配置信息
DataSet dsInfo=Class_Comm.GetServiceAsswConf().GetRtuPackageInfo();
DataTable dtInfo=dsInfo.Tables["tb_nwRtuPackageInfo"];
if (dtInfo == null)
return false;
if (dt.Rows.Count > 0)
{
for(int i=0; i<dt.Rows.Count; i++)
{
RtuPackage rtuPackage = new RtuPackage();
rtuPackage.m_DeviceID = Convert.ToUInt32(dt.Rows[i]["fd_id"].ToString());
string filter = "fd_id = " + dt.Rows[i]["fd_id"].ToString();
//根据RTU的号码,检索对应的通道配置信息
DataRow[] drs = dtInfo.Select(filter);
rtuPackage.m_ChannelCount = (uint)drs.Length;
rtuPackage.m_ChannelWidth = new uint[drs.Length];
rtuPackage.m_useFlag = new string[drs.Length];
//用来保存已用通道的索引号、字节数
StringBuilder sbIndex = new StringBuilder("");
StringBuilder sbWidth = new StringBuilder("");
for(int j=0; j<drs.Length; j++)
{
rtuPackage.m_ChannelWidth[j] = Convert.ToUInt32(drs[j]["fd_nums"].ToString());
rtuPackage.m_useFlag[j] =drs[j]["fd_useFlag"].ToString();
if (rtuPackage.m_useFlag[j].CompareTo("1") == 0)
{
uint tmpIndex=0;
for(int k=0; k<j; k++)
{
tmpIndex += rtuPackage.m_ChannelWidth[k];
}
sbIndex.Append(",").Append(tmpIndex.ToString());
sbWidth.Append(",").Append(rtuPackage.m_ChannelWidth[j].ToString());
}
}
if (sbIndex.ToString()!="")
{
sbIndex.Remove(0,1);
sbWidth.Remove(0,1);
string[] tmpIndex = sbIndex.ToString().Split(new char[]{','});
string[] tmpWidth = sbWidth.ToString().Split(new char[]{','});
rtuPackage.m_ValidChannelIndex = new uint[tmpIndex.Length];
rtuPackage.m_ValidChannelWidth = new uint[tmpWidth.Length];
for(int m=0; m<tmpIndex.Length; m++)
{
rtuPackage.m_ValidChannelIndex[m] = Convert.ToUInt32(tmpIndex[m]);
rtuPackage.m_ValidChannelWidth[m] = Convert.ToUInt32(tmpWidth[m]);
}
}
htRtuPackage.Add(rtuPackage.m_DeviceID, rtuPackage);
}
}
else
{
return false;
}
return true;
}
#endregion
#region 构造函数
public FormGprsData()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
public FormGprsData(string moduleNo)
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
this.ModuleNO = moduleNo;
DebugFileName = Application.StartupPath + "\\DriverDebug.txt";
// 初始化UDP通讯所需要的IP和Port
if (!this.InitDriverConfig())
{
MessageBox.Show("初始化udp通讯所需的ip和port出错");
return;
}
//初始化RTU的通道配置信息
if (!this.InitHtRtuPackage())
{
MessageBox.Show("监测点初始化信息失败!");
return;
}
// 判断是否可以打开到中央控制系统的UDP端口和与GPRS通讯所需要的本机UDP端口
if (!this.InitSocketLink())
{
MessageBox.Show("打开与中央控制系统和GPRS通讯udp端口出错");
return;
}
// 开启通讯线程
if (!this.OpenThread())
{
MessageBox.Show("开启通讯线程出错");
return;
}
}
#endregion
#region 清理所有正在使用的资源
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#endregion
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.sbStatistic = new System.Windows.Forms.StatusBar();
this.statusBarPanel1 = new System.Windows.Forms.StatusBarPanel();
this.statusBarPanel2 = new System.Windows.Forms.StatusBarPanel();
this.statusBarPanel3 = new System.Windows.Forms.StatusBarPanel();
this.panel1 = new System.Windows.Forms.Panel();
this.panel2 = new System.Windows.Forms.Panel();
this.lbData = new System.Windows.Forms.ListBox();
((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.statusBarPanel2)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.statusBarPanel3)).BeginInit();
this.panel1.SuspendLayout();
this.panel2.SuspendLayout();
this.SuspendLayout();
//
// sbStatistic
//
this.sbStatistic.Location = new System.Drawing.Point(0, 441);
this.sbStatistic.Name = "sbStatistic";
this.sbStatistic.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] {
this.statusBarPanel1,
this.statusBarPanel2,
this.statusBarPanel3});
this.sbStatistic.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.sbStatistic.ShowPanels = true;
this.sbStatistic.Size = new System.Drawing.Size(706, 22);
this.sbStatistic.TabIndex = 0;
//
// statusBarPanel1
//
this.statusBarPanel1.AutoSize = System.Windows.Forms.StatusBarPanelAutoSize.Spring;
this.statusBarPanel1.Width = 230;
//
// statusBarPanel2
//
this.statusBarPanel2.AutoSize = System.Windows.Forms.StatusBarPanelAutoSize.Spring;
this.statusBarPanel2.Width = 230;
//
// statusBarPanel3
//
this.statusBarPanel3.AutoSize = System.Windows.Forms.StatusBarPanelAutoSize.Spring;
this.statusBarPanel3.Width = 230;
//
// panel1
//
this.panel1.AutoScroll = true;
this.panel1.Controls.Add(this.panel2);
this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
this.panel1.Location = new System.Drawing.Point(0, 0);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(706, 441);
this.panel1.TabIndex = 1;
//
// panel2
//
this.panel2.AutoScroll = true;
this.panel2.Controls.Add(this.lbData);
this.panel2.Dock = System.Windows.Forms.DockStyle.Fill;
this.panel2.Location = new System.Drawing.Point(0, 0);
this.panel2.Name = "panel2";
this.panel2.Size = new System.Drawing.Size(706, 441);
this.panel2.TabIndex = 1;
//
// lbData
//
this.lbData.Dock = System.Windows.Forms.DockStyle.Fill;
this.lbData.HorizontalScrollbar = true;
this.lbData.ItemHeight = 12;
this.lbData.Location = new System.Drawing.Point(0, 0);
this.lbData.Name = "lbData";
this.lbData.Size = new System.Drawing.Size(706, 436);
this.lbData.TabIndex = 0;
//
// FormGprsData
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(706, 463);
this.Controls.Add(this.panel1);
this.Controls.Add(this.sbStatistic);
this.Name = "FormGprsData";
this.Text = "FormGprsData";
this.Closing += new System.ComponentModel.CancelEventHandler(this.FormGprsData_Closing);
this.Load += new System.EventHandler(this.FormGprsData_Load);
this.Closed += new System.EventHandler(this.FormGprsData_Closed);
((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.statusBarPanel2)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.statusBarPanel3)).EndInit();
this.panel1.ResumeLayout(false);
this.panel2.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
#region 初始化DriverConfg参数
/// <summary>
/// 初始化DriverConfg参数
/// </summary>
/// <returns></returns>
public bool InitDriverConfig()
{
DriverConfigureFileName = Application.StartupPath + "\\DriverConfigure.ini";
driverCfg = new DriverConfig();
strDrvSvc DrvSvc = null;
int nSize = 0;
StringBuilder stringBuilder = new StringBuilder();
IPHostEntry ipHost=Dns.Resolve(Dns.GetHostName());
IPAddress ipAddr=ipHost.AddressList[0];
String DebugFileNameBuffer=DebugFileName;
uint ExceptionCountBuffer=ExceptionCount;
StreamWriter swDebugFileBuffer=null;
try
{
GetPrivateProfileString("DRIVERTYPE","DriverID","GPRS", stringBuilder, ref nSize,DriverConfigureFileName);
driverCfg.DriverID = stringBuilder.ToString();
// 设置从GPRS接收实时数据的ip和port
GetPrivateProfileString("GPRS","IpRecvFromGprs","172.18.18.71", stringBuilder, ref nSize,DriverConfigureFileName);
driverCfg.IpRecvFromGprs = stringBuilder.ToString();
GetPrivateProfileString("GPRS","PortRecvFromGprs","4000", stringBuilder, ref nSize,DriverConfigureFileName);
driverCfg.PortRecvFromGprs = Int32.Parse(stringBuilder.ToString());
}
catch(Exception err)
{
GprsComm.WriteToText(ExceptionCountBuffer,DebugFileNameBuffer,swDebugFileBuffer,err);
return false;
}
//获取驱动配置信息
try
{
DrvSvc = Class_Comm.GetServiceAsswCtrl().getDrvSvcByID(driverCfg.DriverID);
// 设置发送到中控的ip和port
driverCfg.IpSendToCenter = DrvSvc.fd_server.Trim();
driverCfg.PortSendToCenter = DrvSvc.fd_port1;
}
catch (Exception err)
{
DrvSvc = null;
GprsComm.WriteToText(ExceptionCountBuffer,DebugFileNameBuffer,swDebugFileBuffer,err);
return false;
}
return true;
}
#endregion
#region 初始化SOCKET连接
/// <summary>
/// 初始化SOCKET连接
/// </summary>
/// <returns></returns>
public bool InitSocketLink()
{
String DebugFileNameBuffer = DebugFileName;
uint ExceptionCountBuffer = ExceptionCount;
StreamWriter swDebugFileBuffer = null;
try
{
gprsComm=new GprsComm(driverCfg, DebugFileNameBuffer, lbData, sbStatistic, htRtuPackage);
}
catch (Exception err)
{
GprsComm.WriteToText(ExceptionCountBuffer, DebugFileNameBuffer, swDebugFileBuffer, err);
return false;
}
return true;
}
#endregion
#region 开启所有接收和发送线程
/// <summary>
/// 开启所有接收和发送线程
/// </summary>
/// <returns></returns>
public bool OpenThread()
{
String DebugFileNameBuffer = DebugFileName;
uint ExceptionCountBuffer = ExceptionCount;
StreamWriter swDebugFileBuffer = null;
try
{
//接收实时数据并发送到中控线程
tRecvFromGprs = new Thread(new ThreadStart(gprsComm.StartRecvGPRSSendCenter));
tRecvFromGprs.IsBackground = true;
tRecvFromGprs.Start();
}
catch (Exception err)
{
if (tRecvFromGprs != null && tRecvFromGprs.IsAlive)
{
tRecvFromGprs.Abort();
tRecvFromGprs = null;
}
GprsComm.WriteToText(ExceptionCountBuffer, DebugFileNameBuffer, swDebugFileBuffer, err);
return false;
}
return true;
}
#endregion
#region 关闭所有发送和接收线程
/// <summary>
/// 关闭所有发送和接收线程
/// </summary>
/// <returns></returns>
public bool closeThread()
{
String DebugFileNameBuffer = DebugFileName;
uint ExceptionCountBuffer = ExceptionCount;
if (tRecvFromGprs != null && tRecvFromGprs.IsAlive)
{
gprsComm.SocketRecvFromGprs.Shutdown(SocketShutdown.Receive);
tRecvFromGprs.Abort();
tRecvFromGprs = null;
}
return true;
}
#endregion
#region 正准备关闭窗体
private void FormGprsData_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
bool isok=Class_Comm.ChkPwd();
e.Cancel=!isok;
}
#endregion
#region 窗体关闭
private void FormGprsData_Closed(object sender, System.EventArgs e)
{
this.closeThread();
gprsComm.CloseSocketLink();
}
#endregion
#region 窗体初始化方法
private void FormGprsData_Load(object sender, System.EventArgs e)
{
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -