342 lines
14 KiB
C#
342 lines
14 KiB
C#
|
|
using Modbus.Device;
|
|||
|
|
using System;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.Data;
|
|||
|
|
using System.Linq;
|
|||
|
|
using System.Net.Sockets;
|
|||
|
|
using System.Text;
|
|||
|
|
using System.Threading;
|
|||
|
|
|
|||
|
|
namespace ConsoleGetPLCData.CS
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
class GetDataNet
|
|||
|
|
{
|
|||
|
|
stStation Station = new stStation();
|
|||
|
|
private ModbusIpMaster master;
|
|||
|
|
private TcpClient client;
|
|||
|
|
private bool WriteDateTimeFlag;
|
|||
|
|
public static ManualResetEvent allDone = new ManualResetEvent(false);
|
|||
|
|
|
|||
|
|
///构造函数 初始化
|
|||
|
|
public GetDataNet(stStation strZhan)
|
|||
|
|
{
|
|||
|
|
Station = strZhan;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 线程入口函数
|
|||
|
|
/// </summary>
|
|||
|
|
public void GetPLCData()
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
ProgramGetData.strAppend(DateTime.Now.ToString() + "------" + "开始采集" + Station.ZCName + "数据");
|
|||
|
|
|
|||
|
|
GetDataByNet();
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
#region 局域网采集功能
|
|||
|
|
/// <summary>
|
|||
|
|
/// 局域网获取PLC的数据
|
|||
|
|
/// </summary>
|
|||
|
|
private void GetDataByNet()
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
while (true)
|
|||
|
|
{
|
|||
|
|
using (client = new TcpClient())
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
client.Connect(Station.ip, Station.port);
|
|||
|
|
master = ModbusIpMaster.CreateIp(client);
|
|||
|
|
int JldNum = ProgramGetData.dsJLDataRealTime.Tables[Station.ZCID].Rows.Count;
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
catch (SystemException err)
|
|||
|
|
{
|
|||
|
|
if (err.Message != null)
|
|||
|
|
{
|
|||
|
|
// string strZCIDtemp[]=Station.ZCID.Split ('_');
|
|||
|
|
// if (strZCIDtemp .Length >1)
|
|||
|
|
//{
|
|||
|
|
|
|||
|
|
//}
|
|||
|
|
// else
|
|||
|
|
// {
|
|||
|
|
int JldNum = ProgramGetData.dsJLDataRealTime.Tables[Station.ZCID].Rows.Count;
|
|||
|
|
|
|||
|
|
//}
|
|||
|
|
|
|||
|
|
|
|||
|
|
for (int i = 0; i < ProgramGetData.dsJLDataRealTime.Tables[Station.ZCID].Rows.Count; i++) //PLC计量点读取循环 循环数由主程序建立该站的实时数据表中的记录数决定
|
|||
|
|
{
|
|||
|
|
//故障赋值
|
|||
|
|
//TranData.netError(ProgramGetData.dsJLDataRealTime.Tables[Station.ZCID].Rows[i]);
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
foreach (DataRow dr in ProgramGetData.dsJLDataRealTime.Tables[Station.ZCID].Rows)
|
|||
|
|
{
|
|||
|
|
TranData.netError(dr);
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
ProgramGetData.strAppend(DateTime.Now.ToString() + "------" + Station.ZCName + Station.ZCID + "网络连接故障!");
|
|||
|
|
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
int JldNum = ProgramGetData.dsJLDataRealTime.Tables[Station.ZCID].Rows.Count;
|
|||
|
|
|
|||
|
|
ProgramGetData.dsZhanDateTime.Tables[Station.ZCID].Rows[0]["PLCTime"] = getPLCDateTime(); //获取PLC时间
|
|||
|
|
SetPLCDateTime(); //同步PLC时间
|
|||
|
|
|
|||
|
|
//PLC计量点读取循环 循环数由主程序建立该站的实时数据表中的记录数决定
|
|||
|
|
foreach (DataRow dr in ProgramGetData.dsJLDataRealTime.Tables[Station.ZCID].Rows)
|
|||
|
|
{
|
|||
|
|
int _jldID = 0;
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
_jldID = int.Parse(dr["jld_id"].ToString());
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
DataRow[] drPars = ProgramGetData.dsPparameter.Tables[Station.ZCID].Select("jld_id='" + _jldID.ToString() + "'");
|
|||
|
|
//判断是否读取计量点参数
|
|||
|
|
if (dr["readparflag"].ToString() == "1")
|
|||
|
|
{
|
|||
|
|
ushort[] ParWord = master.ReadHoldingRegisters(Convert.ToUInt16((int)(24360 + (240 * _jldID))), 60);
|
|||
|
|
drPars[0].ItemArray = TranData.GetPLCPar_Net(drPars[0], ParWord, Station.MeterTYPE).ItemArray;
|
|||
|
|
dr["readparflag"] = "0";
|
|||
|
|
drPars[0]["ReadTime"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
|||
|
|
}
|
|||
|
|
//采集实时计量数据
|
|||
|
|
ushort[] Word = master.ReadHoldingRegisters(Convert.ToUInt16((int)(400 + (20 * _jldID))), 20);
|
|||
|
|
//解析数据 根据设备类型解析
|
|||
|
|
dr.ItemArray = TranData.GetDataTran_Net(dr, Word, Station.MeterTYPE).ItemArray;
|
|||
|
|
//读取计算脉冲仪表的工况流量或雷诺数
|
|||
|
|
string strPlusNum = "";
|
|||
|
|
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
|
|||
|
|
if (dr["MeterTYPE"].ToString() == "脉冲")
|
|||
|
|
{
|
|||
|
|
//采集脉冲数
|
|||
|
|
ushort[] PlusNum = master.ReadHoldingRegisters(Convert.ToUInt16((int)(5000 + (80 * _jldID))), 4);
|
|||
|
|
//采集K系数
|
|||
|
|
ushort[] PlusK = master.ReadHoldingRegisters(Convert.ToUInt16((int)(2600 + (30 * _jldID))), 4);
|
|||
|
|
string ssgkll = TranData.GetFlowGK(PlusNum, PlusK, out strPlusNum);
|
|||
|
|
dr["SSGKLL"] = ssgkll;
|
|||
|
|
string strCxXx = drPars[0]["CLXX"].ToString();
|
|||
|
|
string strCxSx = drPars[0]["CLSX"].ToString();
|
|||
|
|
dr["CXFlag"] = TranData.GetCxFlag(ssgkll, strCxXx, strCxSx);
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
dr["JLD_NAME"] = drPars[0]["JLD_NAME"];
|
|||
|
|
if (dr["MeterTYPE"].ToString() == "脉冲") dr["CY"] = strPlusNum;
|
|||
|
|
//差压式流量计则计算雷诺数
|
|||
|
|
if (dr["MeterTYPE"].ToString() != "脉冲")
|
|||
|
|
{
|
|||
|
|
string strXdmd = drPars[0]["Gr"].ToString();
|
|||
|
|
string strGj = drPars[0]["gj"].ToString();
|
|||
|
|
float sngDlnd = 19768.8f;
|
|||
|
|
string strQn = dr["SSL"].ToString();
|
|||
|
|
if (strQn == "") strQn = "0";
|
|||
|
|
if (strXdmd == "") strXdmd = "0";
|
|||
|
|
|
|||
|
|
if (Station.ZCID == "4708")
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (strGj != "")
|
|||
|
|
{
|
|||
|
|
dr["SSGKLL"] = (1615.7238 * Convert.ToSingle(strQn) * Convert.ToSingle(strXdmd) / Convert.ToSingle(strGj)).ToString("#.#");
|
|||
|
|
}
|
|||
|
|
dr["CXFlag"] = TranData.GetCxFlag(dr["CY"].ToString());
|
|||
|
|
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
if (ex.Message != null)
|
|||
|
|
{
|
|||
|
|
ProgramGetData.strAppend(DateTime.Now.ToString() + "------" + Station.ZCName + Station.ZCID + "计量点 " + "读取寄存器错误!");
|
|||
|
|
continue;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
Thread.Sleep(Station.readDiffTime);
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// 将服务器的时间同步到PLC
|
|||
|
|
/// </summary>
|
|||
|
|
private void SetPLCDateTime()
|
|||
|
|
{
|
|||
|
|
DateTime SAVETIME = DateTime.Now;
|
|||
|
|
|
|||
|
|
if ((SAVETIME.Hour.ToString() == "16" && SAVETIME.Minute == 00 && WriteDateTimeFlag == false) || (ProgramGetData.dsZhanDateTime.Tables[Station.ZCID].Rows[0]["TongBuFlag"].ToString() == "同步")) //到每日23:40 同步时间
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
ushort[] DateData = getTimeData();
|
|||
|
|
master.WriteSingleRegister(380, DateData[0]);
|
|||
|
|
master.WriteSingleRegister(381, DateData[1]);
|
|||
|
|
master.WriteSingleRegister(382, DateData[2]);
|
|||
|
|
master.WriteSingleRegister(383, DateData[3]);
|
|||
|
|
|
|||
|
|
master.WriteSingleRegister(384, 1);
|
|||
|
|
master.WriteSingleRegister(386, 1);
|
|||
|
|
|
|||
|
|
ProgramGetData.dsZhanDateTime.Tables[Station.ZCID].Rows[0]["TongBuTime"] = SAVETIME;
|
|||
|
|
ProgramGetData.dsZhanDateTime.Tables[Station.ZCID].Rows[0]["TongBuFlag"] = "成功";
|
|||
|
|
WriteDateTimeFlag = true;
|
|||
|
|
}
|
|||
|
|
catch (SystemException error)
|
|||
|
|
{
|
|||
|
|
if (error.Message != null)
|
|||
|
|
{
|
|||
|
|
ProgramGetData.strAppend(DateTime.Now.ToString() + "------" + Station.ZCName + Station.ZCID + "时间同步失败!");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
if (SAVETIME.Hour.ToString() != "16" && SAVETIME.Minute != 00) // 同步时间标志位false
|
|||
|
|
{
|
|||
|
|
WriteDateTimeFlag = false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// 读取局域网站场PLC的时间
|
|||
|
|
/// </summary>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
private string getPLCDateTime()
|
|||
|
|
{
|
|||
|
|
//读取站场PLC的系统时间
|
|||
|
|
ushort[] plc_DateTime = master.ReadHoldingRegisters(16, 4);
|
|||
|
|
|
|||
|
|
string plc_Year = plc_DateTime[0].ToString("x4");
|
|||
|
|
string plc_Month = plc_DateTime[1].ToString("x4");
|
|||
|
|
string plc_Minute = plc_DateTime[2].ToString("x4");
|
|||
|
|
string plc_Second = plc_DateTime[3].ToString("x4");
|
|||
|
|
string plcDatetime = plc_Year + "-" + plc_Month.Substring(0, 2) + "-" + plc_Month.Substring(2, 2) + " " + plc_Minute.Substring(0, 2) + ":" + plc_Minute.Substring(2, 2) + ":" + plc_Second;//.Substring(0, 2);// +plc_Second.Substring(2, 2);
|
|||
|
|
return plcDatetime;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 将日期时间转换为Ushort类型数组
|
|||
|
|
/// </summary>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
private ushort[] getTimeData()
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
ushort[] data = new ushort[4];
|
|||
|
|
DateTime now = new DateTime();
|
|||
|
|
now = DateTime.Now;
|
|||
|
|
string str = "";
|
|||
|
|
str = now.Year.ToString();
|
|||
|
|
int num4 = Convert.ToInt16(str.Substring(0, 1));
|
|||
|
|
int num5 = Convert.ToInt16(str.Substring(1, 1));
|
|||
|
|
int num6 = Convert.ToInt16(str.Substring(2, 1));
|
|||
|
|
int num7 = Convert.ToInt16(str.Substring(3, 1));
|
|||
|
|
data[0] = (ushort)((((((num4 * 0x10) * 0x10) * 0x10) + ((num5 * 0x10) * 0x10)) + (num6 * 0x10)) + num7);
|
|||
|
|
string str3 = "";
|
|||
|
|
if (now.Month < 10)
|
|||
|
|
{
|
|||
|
|
str3 = str3 + "0" + now.Month.ToString();
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
str3 = str3 + now.Month.ToString();
|
|||
|
|
}
|
|||
|
|
if (now.Day < 10)
|
|||
|
|
{
|
|||
|
|
str3 = str3 + "0" + now.Day.ToString();
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
str3 = str3 + now.Day.ToString();
|
|||
|
|
}
|
|||
|
|
str = str3;
|
|||
|
|
num4 = Convert.ToInt16(str.Substring(0, 1));
|
|||
|
|
num5 = Convert.ToInt16(str.Substring(1, 1));
|
|||
|
|
num6 = Convert.ToInt16(str.Substring(2, 1));
|
|||
|
|
num7 = Convert.ToInt16(str.Substring(3, 1));
|
|||
|
|
data[1] = (ushort)((((((num4 * 0x10) * 0x10) * 0x10) + ((num5 * 0x10) * 0x10)) + (num6 * 0x10)) + num7);
|
|||
|
|
string str4 = "";
|
|||
|
|
if (now.Hour < 10)
|
|||
|
|
{
|
|||
|
|
str4 = str4 + "0" + now.Hour.ToString();
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
str4 = str4 + now.Hour.ToString();
|
|||
|
|
}
|
|||
|
|
if (now.Minute < 10)
|
|||
|
|
{
|
|||
|
|
str4 = str4 + "0" + now.Minute.ToString();
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
str4 = str4 + now.Minute.ToString();
|
|||
|
|
}
|
|||
|
|
str = str4;
|
|||
|
|
num4 = Convert.ToInt16(str.Substring(0, 1));
|
|||
|
|
num5 = Convert.ToInt16(str.Substring(1, 1));
|
|||
|
|
num6 = Convert.ToInt16(str.Substring(2, 1));
|
|||
|
|
num7 = Convert.ToInt16(str.Substring(3, 1));
|
|||
|
|
data[2] = (ushort)((((((num4 * 0x10) * 0x10) * 0x10) + ((num5 * 0x10) * 0x10)) + (num6 * 0x10)) + num7);
|
|||
|
|
string str5 = "";
|
|||
|
|
if (now.Second < 10)
|
|||
|
|
{
|
|||
|
|
str5 = str5 + "000" + now.Second.ToString();
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
str5 = str5 + "00" + now.Second.ToString();
|
|||
|
|
}
|
|||
|
|
str = str5;
|
|||
|
|
num4 = Convert.ToInt16(str.Substring(0, 1));
|
|||
|
|
num5 = Convert.ToInt16(str.Substring(1, 1));
|
|||
|
|
num6 = Convert.ToInt16(str.Substring(2, 1));
|
|||
|
|
num7 = Convert.ToInt16(str.Substring(3, 1));
|
|||
|
|
data[3] = (ushort)((((((num4 * 0x10) * 0x10) * 0x10) + ((num5 * 0x10) * 0x10)) + (num6 * 0x10)) + num7);
|
|||
|
|
|
|||
|
|
return data;
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
}
|