using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SqlClient; using System.Collections; using RemoteObjects; using UPPLibs; using EMS.Lib; using System.Threading; using System.Globalization; using Entity; namespace CommService { /* *结构名称:struAutoTrans *创建人:韩荣伟 *创建时间:2010-10-30 *功能描述:保存电力对象的解析数据 */ struct struAutoTrans { //A相电流 public string IA; //B相电流 public string IB; //C相电流 public string IC; //A相电压 public string UA; //B相电压 public string UB; //C相电压 public string UC; //功率因数 public string GP; //正向有功总 public string YGZ; //采集时间 public string TTime; //终端编号 public string TID; //仪表编号 public int MID; //尖峰 public string JF; //峰 public string F; //平 public string P; //谷 public string G; //正向无功 public string ZXWG; public string monitorID; } /* *结构名称: struTempTrans *创建人:姜志浩 *创建时间:2018-04-16 *功能描述:保存温湿度对象的解析数据 */ struct struTempTrans { public string monitorID; public string illuminance; public string noise; public string concentration; /// /// 温度 /// public string TEM; /// /// 湿度 /// public string HUM; /// /// 采集时间 /// public string TTime; /// /// 终端编号 /// public string TID; /// /// 仪表编号 /// public int MID; } /* *结构名称:struAlarmDataTrans *创建人:韩荣伟 *创建时间:2010-10-30 *功能描述:保存告警消息的解析数据 */ struct struAlarmDataTrans { public object objUserdata; //采集时间 public string TTime; //终端编号 public string TID; //仪表编号 public int MID; } /* *类名称:E_MeterParams *创建人:韩荣伟 *创建时间:2010-10-30 *功能描述:保存电表PT,CT */ class E_MeterParams { public int nPT; public int nCT; } /* *结构名称:struSDataTrans *创建人:韩荣伟 *创建时间:2010-10-30 *功能描述:保存蒸汽消息的解析数据 */ struct struSDataTrans { public string monitorID; //蒸汽仪表压力值 public string YL; //蒸汽仪表温度值 public string WD; //蒸汽仪表瞬时流量值 public string SSLL; //蒸汽仪表累积流量值 public string LJLL; //蒸汽仪表瞬时热量 public string SSRL; //蒸汽仪表累积热量 public string LJRL; //采集时间 public string TTime; //终端编号 public string TID; //仪表编号 public int MID; //密度 public string MIDU; //差压 public string CHAYA; //频率 public string PL; //停电次数 public string Num; //听了多长时间电 public string Time; } /* *类名称:MessageHandler *创建人:韩荣伟 *创建时间:2010-10-30 *功能描述:消息分类处理类 */ class MessageHandler { private TCPHandler tcpHandler = null; private Hashtable htCollectPoint = null; public static Hashtable htEParams; private string connectString = Common.configReader.sConnectString; private string SYEMS_ZDSqlMaskString = " INSERT INTO dbo.T_VibrationSensor_Data ( sensor_id ,collectTime ,speed,displacement,acceleration,temperature) values ( '{0}',{1},{2} ,{3},{4},{5}) "; private string SYEMS_HWSqlMaskString = " INSERT INTO dbo.T_ArrayTemperatureData ( monitorId ,probeAddress ,collectTime ,maxTemperature,minTemperature,avgTemperature,Site1, Site2, Site3, Site4, Site5, Site6, Site7, Site8, Site9, Site10, Site11, Site12, Site13, Site14, Site15, Site16, Site17, Site18, Site19, Site20, Site21, Site22, Site23, Site24, Site25, Site26, Site27, Site28, Site29, Site30, Site31, Site32, Site33, Site34, Site35, Site36, Site37, Site38, Site39, Site40, Site41, Site42, Site43, Site44, Site45, Site46, Site47, Site48, Site49, Site50, Site51, Site52, Site53, Site54, Site55, Site56, Site57, Site58, Site59, Site60, Site61, Site62, Site63, Site64, Site65, Site66, Site67, Site68, Site69, Site70, Site71, Site72, Site73, Site74, Site75, Site76, Site77, Site78, Site79, Site80, Site81, Site82, Site83, Site84, Site85, Site86, Site87, Site88, Site89, Site90, Site91, Site92, Site93, Site94, Site95, Site96, Site97, Site98, Site99, Site100, Site101, Site102, Site103, Site104, Site105, Site106, Site107, Site108, Site109, Site110, Site111, Site112, Site113, Site114, Site115, Site116, Site117, Site118, Site119, Site120, Site121, Site122, Site123, Site124, Site125, Site126, Site127, Site128, Site129, Site130, Site131, Site132, Site133, Site134, Site135, Site136, Site137, Site138, Site139, Site140, Site141, Site142, Site143, Site144, Site145, Site146, Site147, Site148, Site149, Site150, Site151, Site152, Site153, Site154, Site155, Site156, Site157, Site158, Site159, Site160, Site161, Site162, Site163, Site164, Site165, Site166, Site167, Site168, Site169, Site170, Site171, Site172, Site173, Site174, Site175, Site176, Site177, Site178, Site179, Site180, Site181, Site182, Site183, Site184, Site185, Site186, Site187, Site188, Site189, Site190, Site191, Site192, Site193, Site194, Site195, Site196, Site197, Site198, Site199, Site200, Site201, Site202, Site203, Site204, Site205, Site206, Site207, Site208, Site209, Site210, Site211, Site212, Site213, Site214, Site215, Site216, Site217, Site218, Site219, Site220, Site221, Site222, Site223, Site224, Site225, Site226, Site227, Site228, Site229, Site230, Site231, Site232, Site233, Site234, Site235, Site236, Site237, Site238, Site239, Site240, Site241, Site242, Site243, Site244, Site245, Site246, Site247, Site248, Site249, Site250, Site251, Site252, Site253, Site254, Site255, Site256, Site257, Site258, Site259, Site260, Site261, Site262, Site263, Site264, Site265, Site266, Site267, Site268, Site269, Site270, Site271, Site272, Site273, Site274, Site275, Site276, Site277, Site278, Site279, Site280, Site281, Site282, Site283, Site284, Site285, Site286, Site287, Site288, Site289, Site290, Site291, Site292, Site293, Site294, Site295, Site296, Site297, Site298, Site299, Site300, Site301, Site302, Site303, Site304, Site305, Site306, Site307, Site308, Site309, Site310, Site311, Site312, Site313, Site314, Site315, Site316, Site317, Site318, Site319, Site320, Site321, Site322, Site323, Site324, Site325, Site326, Site327, Site328, Site329, Site330, Site331, Site332, Site333, Site334, Site335, Site336, Site337, Site338, Site339, Site340, Site341, Site342, Site343, Site344, Site345, Site346, Site347, Site348, Site349, Site350, Site351, Site352, Site353, Site354, Site355, Site356, Site357, Site358, Site359, Site360, Site361, Site362, Site363, Site364, Site365, Site366, Site367, Site368, Site369, Site370, Site371, Site372, Site373, Site374, Site375, Site376, Site377, Site378, Site379, Site380, Site381, Site382, Site383, Site384, Site385, Site386, Site387, Site388, Site389, Site390, Site391, Site392, Site393, Site394, Site395, Site396, Site397, Site398, Site399, Site400, Site401, Site402, Site403, Site404, Site405, Site406, Site407, Site408, Site409, Site410, Site411, Site412, Site413, Site414, Site415, Site416, Site417, Site418, Site419, Site420, Site421, Site422, Site423, Site424, Site425, Site426, Site427, Site428, Site429, Site430, Site431, Site432, Site433, Site434, Site435, Site436, Site437, Site438, Site439, Site440, Site441, Site442, Site443, Site444, Site445, Site446, Site447, Site448, Site449, Site450, Site451, Site452, Site453, Site454, Site455, Site456, Site457, Site458, Site459, Site460, Site461, Site462, Site463, Site464, Site465, Site466, Site467, Site468, Site469, Site470, Site471, Site472, Site473, Site474, Site475, Site476, Site477, Site478, Site479, Site480, Site481, Site482, Site483, Site484, Site485, Site486, Site487, Site488, Site489, Site490, Site491, Site492, Site493, Site494, Site495, Site496, Site497, Site498, Site499, Site500, Site501, Site502, Site503, Site504, Site505, Site506, Site507, Site508, Site509, Site510, Site511, Site512, Site513, Site514, Site515, Site516, Site517, Site518, Site519, Site520, Site521, Site522, Site523, Site524, Site525, Site526, Site527, Site528, Site529, Site530, Site531, Site532, Site533, Site534, Site535, Site536, Site537, Site538, Site539, Site540, Site541, Site542, Site543, Site544, Site545, Site546, Site547, Site548, Site549, Site550, Site551, Site552, Site553, Site554, Site555, Site556, Site557, Site558, Site559, Site560, Site561, Site562, Site563, Site564, Site565, Site566, Site567, Site568, Site569, Site570, Site571, Site572, Site573, Site574, Site575, Site576, Site577, Site578, Site579, Site580, Site581, Site582, Site583, Site584, Site585, Site586, Site587, Site588, Site589, Site590, Site591, Site592, Site593, Site594, Site595, Site596, Site597, Site598, Site599, Site600, Site601, Site602, Site603, Site604, Site605, Site606, Site607, Site608, Site609, Site610, Site611, Site612, Site613, Site614, Site615, Site616, Site617, Site618, Site619, Site620, Site621, Site622, Site623, Site624, Site625, Site626, Site627, Site628, Site629, Site630, Site631, Site632, Site633, Site634, Site635, Site636, Site637, Site638, Site639, Site640, Site641, Site642, Site643, Site644, Site645, Site646, Site647, Site648, Site649, Site650, Site651, Site652, Site653, Site654, Site655, Site656, Site657, Site658, Site659, Site660, Site661, Site662, Site663, Site664, Site665, Site666, Site667, Site668, Site669, Site670, Site671, Site672, Site673, Site674, Site675, Site676, Site677, Site678, Site679, Site680, Site681, Site682, Site683, Site684, Site685, Site686, Site687, Site688, Site689, Site690, Site691, Site692, Site693, Site694, Site695, Site696, Site697, Site698, Site699, Site700, Site701, Site702, Site703, Site704, Site705, Site706, Site707, Site708, Site709, Site710, Site711, Site712, Site713, Site714, Site715, Site716, Site717, Site718, Site719, Site720, Site721, Site722, Site723, Site724, Site725, Site726, Site727, Site728, Site729, Site730, Site731, Site732, Site733, Site734, Site735, Site736, Site737, Site738, Site739, Site740, Site741, Site742, Site743, Site744, Site745, Site746, Site747, Site748, Site749, Site750, Site751, Site752, Site753, Site754, Site755, Site756, Site757, Site758, Site759, Site760, Site761, Site762, Site763, Site764, Site765, Site766, Site767, Site768 ) values ( '{0}',{1},{2} ,{3},{4},{5},{6},{7},{8},{9},{10},{11},{12},{13},{14},{15},{16},{17},{18},{19},{20},{21},{22},{23},{24},{25},{26},{27},{28},{29},{30},{31},{32},{33},{34},{35},{36},{37},{38},{39},{40},{41},{42},{43},{44},{45},{46},{47},{48},{49},{50},{51},{52},{53},{54},{55},{56},{57},{58},{59},{60},{61},{62},{63},{64},{65},{66},{67},{68},{69},{70},{71},{72},{73},{74},{75},{76},{77},{78},{79},{80},{81},{82},{83},{84},{85},{86},{87},{88},{89},{90},{91},{92},{93},{94},{95},{96},{97},{98},{99},{100},{101},{102},{103},{104},{105},{106},{107},{108},{109},{110},{111},{112},{113},{114},{115},{116},{117},{118},{119},{120},{121},{122},{123},{124},{125},{126},{127},{128},{129},{130},{131},{132},{133},{134},{135},{136},{137},{138},{139},{140},{141},{142},{143},{144},{145},{146},{147},{148},{149},{150},{151},{152},{153},{154},{155},{156},{157},{158},{159},{160},{161},{162},{163},{164},{165},{166},{167},{168},{169},{170},{171},{172},{173},{174},{175},{176},{177},{178},{179},{180},{181},{182},{183},{184},{185},{186},{187},{188},{189},{190},{191},{192},{193},{194},{195},{196},{197},{198},{199},{200},{201},{202},{203},{204},{205},{206},{207},{208},{209},{210},{211},{212},{213},{214},{215},{216},{217},{218},{219},{220},{221},{222},{223},{224},{225},{226},{227},{228},{229},{230},{231},{232},{233},{234},{235},{236},{237},{238},{239},{240},{241},{242},{243},{244},{245},{246},{247},{248},{249},{250},{251},{252},{253},{254},{255},{256},{257},{258},{259},{260},{261},{262},{263},{264},{265},{266},{267},{268},{269},{270},{271},{272},{273},{274},{275},{276},{277},{278},{279},{280},{281},{282},{283},{284},{285},{286},{287},{288},{289},{290},{291},{292},{293},{294},{295},{296},{297},{298},{299},{300},{301},{302},{303},{304},{305},{306},{307},{308},{309},{310},{311},{312},{313},{314},{315},{316},{317},{318},{319},{320},{321},{322},{323},{324},{325},{326},{327},{328},{329},{330},{331},{332},{333},{334},{335},{336},{337},{338},{339},{340},{341},{342},{343},{344},{345},{346},{347},{348},{349},{350},{351},{352},{353},{354},{355},{356},{357},{358},{359},{360},{361},{362},{363},{364},{365},{366},{367},{368},{369},{370},{371},{372},{373},{374},{375},{376},{377},{378},{379},{380},{381},{382},{383},{384},{385},{386},{387},{388},{389},{390},{391},{392},{393},{394},{395},{396},{397},{398},{399},{400},{401},{402},{403},{404},{405},{406},{407},{408},{409},{410},{411},{412},{413},{414},{415},{416},{417},{418},{419},{420},{421},{422},{423},{424},{425},{426},{427},{428},{429},{430},{431},{432},{433},{434},{435},{436},{437},{438},{439},{440},{441},{442},{443},{444},{445},{446},{447},{448},{449},{450},{451},{452},{453},{454},{455},{456},{457},{458},{459},{460},{461},{462},{463},{464},{465},{466},{467},{468},{469},{470},{471},{472},{473},{474},{475},{476},{477},{478},{479},{480},{481},{482},{483},{484},{485},{486},{487},{488},{489},{490},{491},{492},{493},{494},{495},{496},{497},{498},{499},{500},{501},{502},{503},{504},{505},{506},{507},{508},{509},{510},{511},{512},{513},{514},{515},{516},{517},{518},{519},{520},{521},{522},{523},{524},{525},{526},{527},{528},{529},{530},{531},{532},{533},{534},{535},{536},{537},{538},{539},{540},{541},{542},{543},{544},{545},{546},{547},{548},{549},{550},{551},{552},{553},{554},{555},{556},{557},{558},{559},{560},{561},{562},{563},{564},{565},{566},{567},{568},{569},{570},{571},{572},{573},{574},{575},{576},{577},{578},{579},{580},{581},{582},{583},{584},{585},{586},{587},{588},{589},{590},{591},{592},{593},{594},{595},{596},{597},{598},{599},{600},{601},{602},{603},{604},{605},{606},{607},{608},{609},{610},{611},{612},{613},{614},{615},{616},{617},{618},{619},{620},{621},{622},{623},{624},{625},{626},{627},{628},{629},{630},{631},{632},{633},{634},{635},{636},{637},{638},{639},{640},{641},{642},{643},{644},{645},{646},{647},{648},{649},{650},{651},{652},{653},{654},{655},{656},{657},{658},{659},{660},{661},{662},{663},{664},{665},{666},{667},{668},{669},{670},{671},{672},{673},{674},{675},{676},{677},{678},{679},{680},{681},{682},{683},{684},{685},{686},{687},{688},{689},{690},{691},{692},{693},{694},{695},{696},{697},{698},{699},{700},{701},{702},{703},{704},{705},{706},{707},{708},{709},{710},{711},{712},{713},{714},{715},{716},{717},{718},{719},{720},{721},{722},{723},{724},{725},{726},{727},{728},{729},{730},{731},{732},{733},{734},{735},{736},{737},{738},{739},{740},{741},{742},{743},{744},{745},{746},{747},{748},{749},{750},{751},{752},{753},{754},{755},{756},{757},{758},{759},{760},{761},{762},{763},{764},{765},{766},{767},{768},{769},{770},{771},{772},{773}) "; private string XJEMS_THSqlMaskString = " INSERT INTO dbo.T_W_TempertureData ( monitorId ,collectTime ,tempreture,humidity,illuminance,noise,concentration ) values ( '{0}',{1},{2} ,{3},{4},{5},{6}) "; private string sT_SQLMaskString = " exec dbo.P_Uploadterminal {0},{1},{2} "; private string XJEMS_ESqlMaskString = " INSERT INTO dbo.T_DNB_Instant ( monitorId ,collectTime ,VA ,VB ,VC ,IA ,IB ,IC ,ZXYG ,GLYS ) VALUES ( '{0}',{1},{2} ,{3},{4},{5},{6},{7},{8} ,{9} )"; private string sE_SQLMaskString = " INSERT INTO dbo.T_DNB_Instant ( DnbID ,CJSJ ,VA ,VB ,VC ,IA ,IB ,IC ,ZXYG ,GLYS )SELECT MonitorID ,{0},{1},{2} ,{3},{4},{5},{6},{7},{8} FROM dbo.T_Monitor WHERE CollectDeviceID='{9}' AND MeterCommunicationID ='{10}';DELETE FROM dbo.T_E_Monitor_Instant WHERE DnbID=(SELECT TOP 1 MonitorID FROM dbo.T_Monitor WHERE CollectDeviceID='{9}' AND MeterCommunicationID='{10}') ;INSERT INTO dbo.T_E_Monitor_Instant ( DnbID ,CJSJ ,VA ,VB ,VC ,IA ,IB ,IC ,ZXYG ,GLYS )SELECT MonitorID ,{0},{1},{2} ,{3},{4},{5},{6},{7},{8} FROM dbo.T_Monitor WHERE CollectDeviceID='{9}' AND MeterCommunicationID ='{10}'"; private string sE_SQLMaskStringForPoint = " INSERT INTO dbo.T_E_PointData ( MonitorID ,CJSJ ,VA ,VB ,VC ,IA ,IB ,IC ,ZXYG ,GLYS )SELECT MonitorID ,{0},{1},{2} ,{3},{4},{5},{6},{7},{8} FROM dbo.T_Monitor WHERE CollectDeviceID='{9}' AND MeterCommunicationID ='{10}'"; private string sInsertAlarmDataSQLMask = " INSERT INTO dbo.T_Sys_AlarmData ( MonitorID ,AlarmType ,AlarmValue ,AlarmTime)SELECT MonitorID ,'{0}','{1}', '{2}' FROM dbo.T_Monitor WHERE CollectDeviceID='{3}' AND MeterCommunicationID ='{4}'"; private string XJEMS_SSqlMasjString = "INSERT INTO T_Steam_Data ( MonitorID ,collectTime ,FluxInstantValue ,FluxEyeableTotalValue ,HeatInstantValue ,HeatToftalValue ,Temperature,Press,Density,Frequency,PowerCutNum,PowerCutTime) VALUES ( '{0}','{1}',{2} ,{3},{4},{5},{6},{7},{8} ,{9},{10},{11})"; private string sS_SQLMaskString = " INSERT INTO dbo.T_Steam_Data ( MonitorID ,CollectDatetime ,FluxInstantValue ,FluxEyeableTotalValue ,HeatInstantValue ,HeatToftalValue ,Temperature,Press,Density,DifferencePress,Frequency,PowerCutNum,PowerCutTime)SELECT MonitorID ,'{0}','{1}','{2}' ,'{3}','{4}','{5}','{6}','{7}','{8}' ,{9},{10},{11}FROM dbo.T_Monitor WHERE CollectDeviceID='{12}' AND MeterCommunicationID ='{13}'"; private string sE_InsertSQLMaskString = " INSERT INTO dbo.T_E_AlarmValueSetting ([MonitorID] ,[ILL_Value] ,[IL_Value] ,[IH_Value] ,[IHH_Value] ,[VLL_Value] ,[VL_Value] ,[VH_Value] ,[VHH_Value]) SELECT MonitorID ,'{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}' FROM dbo.T_Monitor WHERE CollectDeviceID='{8}' AND MeterCommunicationID ='{9}'"; private string sE_UpdateSQLMaskString = "UPDATE [dbo].[T_E_AlarmValueSetting]SET [ILL_Value] = '{0}',[IL_Value] = '{1}',[IH_Value] = '{2}',[IHH_Value] = '{3}',[VLL_Value] = '{4}',[VL_Value] = '{5}',[VH_Value] = '{6}',[VHH_Value] = '{7}' FROM (SELECT MonitorID FROM dbo.T_Monitor WHERE CollectDeviceID='{8}' AND MeterCommunicationID ='{9}') AS t1 WHERE dbo.T_E_AlarmValueSetting.MonitorID = t1.MonitorID "; private string sS_InsertSQLMaskString = " INSERT INTO dbo.T_Steam_AlarmSettings ( MonitorID ,AlarmType ,AlarmLimitValue)SELECT MonitorID ,'{0}','{1}' FROM dbo.T_Monitor WHERE CollectDeviceID='{2}' AND MeterCommunicationID ='{3}'"; private string sS_UpdateSQLMaskString = " UPDATE dbo.T_Steam_AlarmSettings set AlarmLimitValue = '{0}' FROM (SELECT MonitorID FROM dbo.T_Monitor WHERE CollectDeviceID='{1}' AND MeterCommunicationID ='{2}') AS t1 WHERE dbo.T_Steam_AlarmSettings.MonitorID = t1.MonitorID and dbo.T_Steam_AlarmSettings.AlarmType = '{3}'"; private string sS_InsertDataSQLMaskString = " INSERT INTO dbo.T_Steam_AlarmData ( MonitorID ,AlarmType ,AlarmValue ,AlarmTime)SELECT MonitorID ,'{0}','{1}', '{2}' FROM dbo.T_Monitor WHERE CollectDeviceID='{3}' AND MeterCommunicationID ='{4}'"; private int nFlagDataStore; private List lstDataStore = null; public MessageHandler() { } public MessageHandler(TCPHandler tcpHandler) { this.tcpHandler = tcpHandler; lstDataStore = new List(); } public void Init() { if (htEParams.Count == 0) { E_Init(); } } private void E_Init() { string queryString = "SELECT [CollectDeviceID] ,[MeterCommunicationID] ,[PT] ,[CT] FROM [dbo].[T_Monitor] where [MonitorType] = 2 and [CollectDeviceID] is not null and [MeterCommunicationID] is not null and [PT] is not null and [CT] is not null"; SqlConnection connection = new SqlConnection(connectString); SqlCommand command = new SqlCommand(queryString, connection); try { connection.Open(); try { SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { Console.WriteLine($"{reader[0]}, {reader[1]}"); E_MeterParams e_MeterParams = new E_MeterParams(); e_MeterParams.nPT = Convert.ToInt32(reader[2]); e_MeterParams.nCT = Convert.ToInt32(reader[3]); htEParams.Add(reader[0].ToString().Trim() + reader[1].ToString().PadLeft(2, '0'), e_MeterParams); } Common.logFile.write("htEParams size:" + htEParams.Count, LogFile.LOGLVL_DEBUG); reader.Close(); } catch (SqlException e2) { Common.logErrFile.write("原SQL语句:" + queryString + ",SQL语句错误:" + e2.ToString(), LogFile.LOGLVL_ERROR); } } catch (Exception e) { Common.logErrFile.write("数据库打开错误:" + e.ToString(), LogFile.LOGLVL_ERROR); Common.logErrFile.write("数据库打开错误:详见err.txt", LogFile.LOGLVL_NORMAL); } } private bool loginMessageHandling(ref struFrame sFrame, ref struFrame sRespFrame) { sRespFrame.flagCtrlF = sFrame.flagCtrlF; sRespFrame.flagCtrlD = 1; string loginPWD = "\u00124V"; bool pwdOK = true; if (loginPWD.Length == sFrame.strData.Length) { for (int i = 0; i < loginPWD.Length; i++) { if (loginPWD[i] != sFrame.strData[i]) { pwdOK = false; break; } } } else { pwdOK = false; } if (pwdOK) { sRespFrame.flagCtrlE = 0; sRespFrame.lenData = 0; sRespFrame.strData = new byte[0]; byte[] tmp = new byte[1] { sFrame.flagDTN }; string newTitle = Encoding.ASCII.GetString(tmp, 0, 1); newTitle = (sFrame.tName = newTitle + (sFrame.addrDNL[0] * 256 + sFrame.addrDNL[1]).ToString("X4")); } else { sRespFrame.flagCtrlE = 1; sRespFrame.lenData = 1; sRespFrame.strData = new byte[1]; sRespFrame.strData[0] = 21; } return true; } private void respMessageSending(ref struFrame sRespFrame, ref byte[] resPack) { string info2Display = ""; UPPHandler.frameCommenting(ref sRespFrame, ref info2Display); Common.logFile.writeHexString("[" + tcpHandler.tName + "] 发送 " + info2Display, resPack, LogFile.LOGLVL_DEBUG); try { tcpHandler.networkStream.Write(resPack, 0, resPack.Length); } catch (Exception e1) { Common.logErrFile.write("Exception: " + e1.ToString(), LogFile.LOGLVL_ERROR); } } private bool heartbeatMessageHandling(ref struFrame sFrame, ref struFrame sRespFrame) { sRespFrame.flagCtrlF = sFrame.flagCtrlF; sRespFrame.flagCtrlD = 1; sRespFrame.flagCtrlE = 0; sRespFrame.lenData = 0; sRespFrame.strData = new byte[0]; return true; } public bool messageHandling(ref struFrame sFrame) { bool res = false; byte[] resPack = null; sFrame.tName = tcpHandler.tName; nFlagDataStore = -1; if (!UPPHandler.frameChecking(ref sFrame)) { return res; } struFrame sRespFrame = default(struFrame); UPPHandler.frameCopy(ref sFrame, ref sRespFrame); lstDataStore.Clear(); bool bLive = true; switch (sFrame.flagCtrl) { case 129: if (tcpHandler.messageMatching(ref sFrame)) { res = true; } break; case 131: case 187: res = DataProceeding(ref sFrame, ref sRespFrame); break; case 132: res = DataProceeding(ref sFrame, ref sRespFrame); break; case 133: res = DataProceeding(ref sFrame, ref sRespFrame); break; case 134: res = DataProceeding(ref sFrame, ref sRespFrame); break; case 135: res = DataProceeding(ref sFrame, ref sRespFrame); break; case 136: res = DataProceeding(ref sFrame, ref sRespFrame); break; case 137: res = AlarmDataProceeding(ref sFrame, ref sRespFrame); break; case 33: res = loginMessageHandling(ref sFrame, ref sRespFrame); break; case 36: res = heartbeatMessageHandling(ref sFrame, ref sRespFrame); if (res) { tcpHandler.SetTimeOut(Common.configReader.nTerminalTimeOut); if (sFrame.tName.CompareTo("未知终端") != 0) { Common.platForm.tcpServer.RefreshHeartBeat(sFrame.tName); } } break; } if (res) { if (sFrame.flagCtrlD != sRespFrame.flagCtrlD) { UPPHandler.framePacking(ref sRespFrame, ref resPack); if (resPack != null && resPack.Length != 0) { respMessageSending(ref sRespFrame, ref resPack); } } if (nFlagDataStore > 0) { SaveCollectionData(nFlagDataStore, bLive); } if (tcpHandler.tName.Length != 5 && sFrame.tName.Length > 0) { Common.platForm.tcpServer.ReplaceSameClient(sFrame.tName); tcpHandler.dtLogin = DateTime.Now; tcpHandler.sLoginTime = tcpHandler.dtLogin.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture); tcpHandler.tName = sFrame.tName; SaveLoginInfo(tcpHandler.tName, tcpHandler.dtLogin); if (res && Common.configReader.bConnectTiming) { Thread.Sleep(3000); tcpHandler.ExecuteTiming(); } } tcpHandler.SetTimeOut(Common.configReader.nTerminalTimeOut); if (sFrame.tName.CompareTo("未知终端") != 0) { Common.platForm.tcpServer.RefreshHeartBeat(sFrame.tName); } } return res; } private void SaveCollectionData(int nFlagDataStore, bool bLive) { switch (nFlagDataStore) { case 131: foreach (struAutoTrans sAutoTrans5 in lstDataStore) { string insertSQLString7 = string.Format(XJEMS_ESqlMaskString, sAutoTrans5.monitorID, sAutoTrans5.TTime, sAutoTrans5.UA, sAutoTrans5.UB, sAutoTrans5.UC, sAutoTrans5.IA, sAutoTrans5.IB, sAutoTrans5.IC, sAutoTrans5.YGZ, sAutoTrans5.GP); SqlConnection sqlConnection2 = new SqlConnection(connectString); SqlCommand command7 = new SqlCommand(insertSQLString7, sqlConnection2); try { sqlConnection2.Open(); try { int i2 = command7.ExecuteNonQuery(); int mID; if (i2 > 0) { LogFile logFile = Common.logFile; string[] obj = new string[6] { "采集:", sAutoTrans5.TTime, " ", sAutoTrans5.TID, " ", null }; mID = sAutoTrans5.MID; obj[5] = mID.ToString("D2"); logFile.write(string.Concat(obj), LogFile.LOGLVL_DEBUG); } else { LogFile logFile2 = Common.logFile; string[] obj2 = new string[6] { "采集失败!!!:", sAutoTrans5.TTime, " ", sAutoTrans5.TID, " ", null }; mID = sAutoTrans5.MID; obj2[5] = mID.ToString("D2"); logFile2.write(string.Concat(obj2), LogFile.LOGLVL_DEBUG); } if (htCollectPoint == null) { htCollectPoint = new Hashtable(); } string tID = sAutoTrans5.TID; mID = sAutoTrans5.MID; string sMeterID3 = tID + mID.ToString("X2"); if (bLive) { if (Common.platForm.tcpServer.bPointCollect) { if (!htCollectPoint.ContainsKey(sMeterID3)) { insertSQLString7 = string.Format(sE_SQLMaskStringForPoint, sAutoTrans5.TTime, sAutoTrans5.UA, sAutoTrans5.UB, sAutoTrans5.UC, sAutoTrans5.IA, sAutoTrans5.IB, sAutoTrans5.IC, sAutoTrans5.YGZ, sAutoTrans5.GP, sAutoTrans5.TID, sAutoTrans5.MID); command7 = new SqlCommand(insertSQLString7, sqlConnection2); i2 = command7.ExecuteNonQuery(); if (i2 > 0) { Common.logFile.write("整点采集:" + sAutoTrans5.TTime + " " + sMeterID3, LogFile.LOGLVL_NORMAL); } else { Common.logFile.write("整点采集失败!!!:" + sAutoTrans5.TTime + " " + sMeterID3, LogFile.LOGLVL_NORMAL); } htCollectPoint.Add(sMeterID3, null); } } else if (htCollectPoint.Count > 0) { htCollectPoint.Clear(); } } else if (Common.platForm.tcpServer.QueryTimePointForHistoryData(sAutoTrans5.TTime, sMeterID3)) { insertSQLString7 = string.Format(sE_SQLMaskStringForPoint, sAutoTrans5.TTime, sAutoTrans5.UA, sAutoTrans5.UB, sAutoTrans5.UC, sAutoTrans5.IA, sAutoTrans5.IB, sAutoTrans5.IC, sAutoTrans5.YGZ, sAutoTrans5.GP, sAutoTrans5.TID, sAutoTrans5.MID); command7 = new SqlCommand(insertSQLString7, sqlConnection2); i2 = command7.ExecuteNonQuery(); if (i2 > 0) { Common.logFile.write("历史数据整点采集:" + sAutoTrans5.TTime + " " + sMeterID3, LogFile.LOGLVL_NORMAL); } else { Common.logFile.write("历史数据整点采集失败!!!:" + sAutoTrans5.TTime + " " + sMeterID3, LogFile.LOGLVL_NORMAL); } } } catch (SqlException e14) { Common.logErrFile.write("原SQL语句:" + insertSQLString7 + ",SQL语句错误:" + e14.ToString(), LogFile.LOGLVL_ERROR); } } catch (Exception e13) { Common.logErrFile.write("数据库打开错误:" + e13.ToString(), LogFile.LOGLVL_ERROR); Common.logErrFile.write("数据库打开错误:详见err.txt", LogFile.LOGLVL_NORMAL); } } break; case 325: { using (SqlConnection sqlConnection = new SqlConnection(connectString)) { try { sqlConnection.Open(); foreach (struAlarmDataTrans alarmDataTrans in lstDataStore) { foreach (DictionaryEntry de in (Hashtable)alarmDataTrans.objUserdata) { string insertSQLString6 = string.Format(sInsertAlarmDataSQLMask, de.Key.ToString(), de.Value.ToString(), alarmDataTrans.TTime, alarmDataTrans.TID, alarmDataTrans.MID); SqlCommand command6 = new SqlCommand(insertSQLString6, sqlConnection); try { int n = command6.ExecuteNonQuery(); } catch (SqlException e12) { Common.logErrFile.write("原SQL语句:" + insertSQLString6 + ",SQL语句错误:" + e12.ToString(), LogFile.LOGLVL_ERROR); } } } } catch (Exception e11) { Common.logErrFile.write("数据库打开错误:" + e11.ToString(), LogFile.LOGLVL_ERROR); Common.logErrFile.write("数据库打开错误:详见err.txt", LogFile.LOGLVL_NORMAL); } } break; } } switch (nFlagDataStore) { case 87: foreach (struTempTrans sAutoTrans4 in lstDataStore) { string MonitorAddr4 = sAutoTrans4.TID; string insertSQLString5 = string.Format(sT_SQLMaskString, sAutoTrans4.TID, sAutoTrans4.TEM, sAutoTrans4.TTime); SqlConnection sqlConnection3 = new SqlConnection(connectString); SqlCommand command5 = new SqlCommand(insertSQLString5, sqlConnection3); try { sqlConnection3.Open(); try { int m = command5.ExecuteNonQuery(); int mID; if (m > 0) { LogFile logFile3 = Common.logFile; string[] obj3 = new string[6] { "采集:", sAutoTrans4.TTime, " ", sAutoTrans4.TID, " ", null }; mID = sAutoTrans4.MID; obj3[5] = mID.ToString("D2"); logFile3.write(string.Concat(obj3), LogFile.LOGLVL_DEBUG); } else { LogFile logFile4 = Common.logFile; string[] obj4 = new string[6] { "采集失败!!!:", sAutoTrans4.TTime, " ", sAutoTrans4.TID, " ", null }; mID = sAutoTrans4.MID; obj4[5] = mID.ToString("D2"); logFile4.write(string.Concat(obj4), LogFile.LOGLVL_DEBUG); } if (htCollectPoint == null) { htCollectPoint = new Hashtable(); } string tID2 = sAutoTrans4.TID; mID = sAutoTrans4.MID; string sMeterID2 = tID2 + mID.ToString("X2"); } catch (SqlException e10) { Common.logErrFile.write("原SQL语句:" + insertSQLString5 + ",SQL语句错误:" + e10.ToString(), LogFile.LOGLVL_ERROR); } } catch (Exception e9) { Common.logErrFile.write("数据库打开错误:" + e9.ToString(), LogFile.LOGLVL_ERROR); Common.logErrFile.write("数据库打开错误:详见err.txt", LogFile.LOGLVL_NORMAL); } } break; } if (nFlagDataStore == 132) { SqlConnection sqlConnection4 = new SqlConnection(connectString); sqlConnection4.Open(); foreach (struSDataTrans sDataTrans in lstDataStore) { string insertSQLString4 = string.Format(XJEMS_SSqlMasjString, sDataTrans.monitorID, sDataTrans.TTime, sDataTrans.SSLL, sDataTrans.LJLL, sDataTrans.SSRL, sDataTrans.LJRL, sDataTrans.WD, sDataTrans.YL, sDataTrans.MIDU, sDataTrans.PL, sDataTrans.Num, sDataTrans.Time); SqlCommand command4 = new SqlCommand(insertSQLString4, sqlConnection4); try { try { int l = command4.ExecuteNonQuery(); } catch (SqlException e8) { Common.logErrFile.write("原SQL语句:" + insertSQLString4 + ",SQL语句错误:" + e8.ToString(), LogFile.LOGLVL_ERROR); } } catch (Exception e7) { Common.logErrFile.write("数据库打开错误:" + e7.ToString(), LogFile.LOGLVL_ERROR); Common.logErrFile.write("数据库打开错误:详见err.txt", LogFile.LOGLVL_NORMAL); } } } if (nFlagDataStore == 133) { foreach (struTempTrans sAutoTrans3 in lstDataStore) { string MonitorAddr3 = sAutoTrans3.TID; string insertSQLString3 = string.Format(XJEMS_THSqlMaskString, sAutoTrans3.monitorID, sAutoTrans3.TTime, sAutoTrans3.TEM, sAutoTrans3.HUM, sAutoTrans3.illuminance, sAutoTrans3.noise, sAutoTrans3.concentration); SqlConnection sqlConnection5 = new SqlConnection(connectString); SqlCommand command3 = new SqlCommand(insertSQLString3, sqlConnection5); try { sqlConnection5.Open(); try { int k = command3.ExecuteNonQuery(); if (htCollectPoint == null) { htCollectPoint = new Hashtable(); } string tID3 = sAutoTrans3.TID; int mID = sAutoTrans3.MID; string sMeterID = tID3 + mID.ToString("X2"); } catch (SqlException e6) { Common.logErrFile.write("原SQL语句:" + insertSQLString3 + ",SQL语句错误:" + e6.ToString(), LogFile.LOGLVL_ERROR); } } catch (Exception e5) { Common.logErrFile.write("数据库打开错误:" + e5.ToString(), LogFile.LOGLVL_ERROR); Common.logErrFile.write("数据库打开错误:详见err.txt", LogFile.LOGLVL_NORMAL); } } } if (nFlagDataStore == 135) { foreach (TVibrationSensorData sAutoTrans2 in lstDataStore) { string MonitorAddr2 = sAutoTrans2.SensorId; string insertSQLString2 = string.Format(SYEMS_ZDSqlMaskString, sAutoTrans2.SensorId, sAutoTrans2.CollectTime, sAutoTrans2.Speed, sAutoTrans2.Displacement, sAutoTrans2.Acceleration, sAutoTrans2.Temperature); SqlConnection sqlConnection6 = new SqlConnection(connectString); SqlCommand command2 = new SqlCommand(insertSQLString2, sqlConnection6); try { sqlConnection6.Open(); try { int j = command2.ExecuteNonQuery(); } catch (SqlException e4) { Common.logErrFile.write("原SQL语句:" + insertSQLString2 + ",SQL语句错误:" + e4.ToString(), LogFile.LOGLVL_ERROR); } } catch (Exception e3) { Common.logErrFile.write("数据库打开错误:" + e3.ToString(), LogFile.LOGLVL_ERROR); Common.logErrFile.write("数据库打开错误:详见err.txt", LogFile.LOGLVL_NORMAL); } } } if (nFlagDataStore == 136) { foreach (TemperatureData sAutoTrans in lstDataStore) { string MonitorAddr = sAutoTrans.monitorId; #region 优化SQL StringBuilder columnBuilder = new StringBuilder(); StringBuilder paramBuilder = new StringBuilder(); columnBuilder.Append("monitorId,probeAddress,collectTime,maxTemperature,minTemperature,avgTemperature"); paramBuilder.Append("@monitorId,@probeAddress,@collectTime,@maxTemperature,@minTemperature,@avgTemperature"); // 动态生成768个Site字段 for (int i = 1; i <= 768; i++) { columnBuilder.Append($",Site{i}"); paramBuilder.Append($",@Site{i}"); } #endregion string sql = $@"INSERT INTO dbo.T_ArrayTemperatureData ({columnBuilder}) VALUES ({paramBuilder})"; SqlConnection connection = new SqlConnection(connectString); using (SqlCommand cmd = new SqlCommand(sql, connection)) { // 添加固定参数 cmd.Parameters.AddWithValue("@monitorId", sAutoTrans.monitorId); cmd.Parameters.AddWithValue("@probeAddress", sAutoTrans.ProbeAddress); cmd.Parameters.AddWithValue("@collectTime", sAutoTrans.CollectTime); cmd.Parameters.AddWithValue("@maxTemperature", sAutoTrans.MaxTemperature); cmd.Parameters.AddWithValue("@minTemperature", sAutoTrans.MinTemperature); cmd.Parameters.AddWithValue("@avgTemperature", sAutoTrans.AvgTemperature); // 动态添加768个温度参数 for (int i = 0; i < 768; i++) { cmd.Parameters.AddWithValue($"@Site{i + 1}", sAutoTrans.Sites[i]); } connection.Open(); cmd.ExecuteNonQuery(); } } } lstDataStore.Clear(); } public bool S_ThresholdParamTrans(ref string[] arrParam, ref byte[] arrBytes, Hashtable htThreshold) { if (arrParam.Length != 16) { return false; } arrBytes = new byte[80]; byte[] arrBytesTmp = null; arrBytes[0] = 140; arrBytes[1] = 0; if (Utility.floatStringToBCD(arrParam[0], 1, 3, ref arrBytesTmp)) { arrBytesTmp.CopyTo(arrBytes, 2); htThreshold.Add("0110", arrParam[0]); arrBytes[4] = 140; arrBytes[5] = 1; if (Utility.floatStringToBCD(arrParam[1], 1, 3, ref arrBytesTmp)) { arrBytesTmp.CopyTo(arrBytes, 6); htThreshold.Add("0111", arrParam[1]); arrBytes[8] = 140; arrBytes[9] = 2; if (Utility.floatStringToBCD(arrParam[2], 1, 3, ref arrBytesTmp)) { arrBytesTmp.CopyTo(arrBytes, 10); htThreshold.Add("0112", arrParam[2]); arrBytes[12] = 140; arrBytes[13] = 3; if (Utility.floatStringToBCD(arrParam[3], 1, 3, ref arrBytesTmp)) { arrBytesTmp.CopyTo(arrBytes, 14); htThreshold.Add("0113", arrParam[3]); arrBytes[16] = 141; arrBytes[17] = 0; if (Utility.floatStringToBCD(arrParam[4], 3, 1, ref arrBytesTmp)) { arrBytesTmp.CopyTo(arrBytes, 18); htThreshold.Add("0120", arrParam[4]); arrBytes[20] = 141; arrBytes[21] = 1; if (Utility.floatStringToBCD(arrParam[5], 3, 1, ref arrBytesTmp)) { arrBytesTmp.CopyTo(arrBytes, 22); htThreshold.Add("0121", arrParam[5]); arrBytes[24] = 141; arrBytes[25] = 2; if (Utility.floatStringToBCD(arrParam[6], 3, 1, ref arrBytesTmp)) { arrBytesTmp.CopyTo(arrBytes, 26); htThreshold.Add("0122", arrParam[6]); arrBytes[28] = 141; arrBytes[29] = 3; if (Utility.floatStringToBCD(arrParam[7], 3, 1, ref arrBytesTmp)) { arrBytesTmp.CopyTo(arrBytes, 30); htThreshold.Add("0123", arrParam[7]); arrBytes[32] = 142; arrBytes[33] = 0; if (Utility.floatStringToBCD(arrParam[8], 7, 1, ref arrBytesTmp)) { arrBytesTmp.CopyTo(arrBytes, 34); htThreshold.Add("0130", arrParam[8]); arrBytes[38] = 142; arrBytes[39] = 1; if (Utility.floatStringToBCD(arrParam[9], 7, 1, ref arrBytesTmp)) { arrBytesTmp.CopyTo(arrBytes, 40); htThreshold.Add("0131", arrParam[9]); arrBytes[44] = 142; arrBytes[45] = 2; if (Utility.floatStringToBCD(arrParam[10], 7, 1, ref arrBytesTmp)) { arrBytesTmp.CopyTo(arrBytes, 46); htThreshold.Add("0132", arrParam[10]); arrBytes[50] = 142; arrBytes[51] = 3; if (Utility.floatStringToBCD(arrParam[11], 7, 1, ref arrBytesTmp)) { arrBytesTmp.CopyTo(arrBytes, 52); htThreshold.Add("0133", arrParam[11]); arrBytes[56] = 143; arrBytes[57] = 0; if (Utility.floatStringToBCD(arrParam[12], 6, 2, ref arrBytesTmp)) { arrBytesTmp.CopyTo(arrBytes, 58); htThreshold.Add("0140", arrParam[12]); arrBytes[62] = 143; arrBytes[63] = 1; if (Utility.floatStringToBCD(arrParam[13], 6, 2, ref arrBytesTmp)) { arrBytesTmp.CopyTo(arrBytes, 64); htThreshold.Add("0141", arrParam[13]); arrBytes[68] = 143; arrBytes[69] = 2; if (Utility.floatStringToBCD(arrParam[14], 6, 2, ref arrBytesTmp)) { arrBytesTmp.CopyTo(arrBytes, 70); htThreshold.Add("0142", arrParam[14]); arrBytes[74] = 143; arrBytes[75] = 3; if (Utility.floatStringToBCD(arrParam[15], 6, 2, ref arrBytesTmp)) { arrBytesTmp.CopyTo(arrBytes, 76); htThreshold.Add("0143", arrParam[15]); return true; } return false; } return false; } return false; } return false; } return false; } return false; } return false; } return false; } return false; } return false; } return false; } return false; } return false; } return false; } return false; } return false; } public bool E_ThresholdParamTrans(ref string[] arrParam, ref byte[] arrBytes, Hashtable htThreshold) { if (arrParam.Length != 24) { return false; } arrBytes = new byte[40]; byte[] arrBytesTmp = null; arrBytes[0] = 218; arrBytes[1] = 128; if (Utility.floatStringToBCD(arrParam[0], 4, 2, ref arrBytesTmp)) { arrBytesTmp.CopyTo(arrBytes, 2); htThreshold.Add("da80", arrParam[0]); arrBytes[5] = 218; arrBytes[6] = 129; if (Utility.floatStringToBCD(arrParam[1], 4, 2, ref arrBytesTmp)) { arrBytesTmp.CopyTo(arrBytes, 7); htThreshold.Add("da81", arrParam[1]); arrBytes[10] = 218; arrBytes[11] = 130; if (Utility.floatStringToBCD(arrParam[2], 4, 2, ref arrBytesTmp)) { arrBytesTmp.CopyTo(arrBytes, 12); htThreshold.Add("da82", arrParam[2]); arrBytes[15] = 218; arrBytes[16] = 131; if (Utility.floatStringToBCD(arrParam[3], 4, 2, ref arrBytesTmp)) { arrBytesTmp.CopyTo(arrBytes, 17); htThreshold.Add("da83", arrParam[3]); arrBytes[20] = 218; arrBytes[21] = 133; if (Utility.floatStringToBCD(arrParam[4], 6, 0, ref arrBytesTmp)) { arrBytesTmp.CopyTo(arrBytes, 22); htThreshold.Add("da85", arrParam[4]); arrBytes[25] = 218; arrBytes[26] = 134; if (Utility.floatStringToBCD(arrParam[5], 6, 0, ref arrBytesTmp)) { arrBytesTmp.CopyTo(arrBytes, 27); htThreshold.Add("da86", arrParam[5]); arrBytes[30] = 218; arrBytes[31] = 135; if (Utility.floatStringToBCD(arrParam[6], 6, 0, ref arrBytesTmp)) { arrBytesTmp.CopyTo(arrBytes, 32); htThreshold.Add("da87", arrParam[6]); arrBytes[35] = 218; arrBytes[36] = 136; if (Utility.floatStringToBCD(arrParam[7], 6, 0, ref arrBytesTmp)) { arrBytesTmp.CopyTo(arrBytes, 37); htThreshold.Add("da88", arrParam[7]); return true; } return false; } return false; } return false; } return false; } return false; } return false; } return false; } return false; } public bool E_PTCTParamTrans(ref string[] arrParam, ref byte[] arrBytes, Hashtable htPTCT) { try { if (arrParam.Length != 2) { return false; } arrBytes = new byte[8]; byte[] arrBytesTmp = new byte[2]; uint unTmp = Convert.ToUInt32(arrParam[0], 10) % 65536u; arrBytes[0] = 137; arrBytes[1] = 17; arrBytesTmp[0] = (byte)(unTmp / 256u); arrBytesTmp[1] = (byte)(unTmp % 256u); arrBytesTmp.CopyTo(arrBytes, 2); htPTCT.Add("8911", arrParam[0]); unTmp = Convert.ToUInt32(arrParam[1], 10) % 65536u; arrBytes[4] = 137; arrBytes[5] = 18; arrBytesTmp[0] = (byte)(unTmp / 256u); arrBytesTmp[1] = (byte)(unTmp % 256u); arrBytesTmp.CopyTo(arrBytes, 6); htPTCT.Add("8912", arrParam[1]); } catch (Exception) { return false; } return true; } private bool TH_DataProceeding(ref struFrame sFrame, ref struFrame sRespFrame) { bool res = true; string info2Display = ""; bool bDataValiad = true; int sectionLength = 0; sectionLength = 40; if (sFrame.lenData / sectionLength <= 0 || sFrame.lenData % sectionLength != 0) { return false; } Common.logFile.write("[" + tcpHandler.tName + "] 主动上传数据域大小:" + sFrame.lenData, LogFile.LOGLVL_DEBUG); for (int i = 0; i < sFrame.lenData / sectionLength; i++) { if (sFrame.flagCtrlD != 1) { continue; } byte flagPoint = sFrame.strData[i * sectionLength]; if (flagPoint <= 0) { continue; } int pointer = 2 + i * sectionLength; string MeterID_1 = "00" + Convert.ToInt32(sFrame.strData[pointer - 2]); MeterID_1 = MeterID_1.Substring(MeterID_1.Length - 2, 2); string MeterID_2 = "00" + Convert.ToInt32(sFrame.strData[pointer - 1]); MeterID_2 = MeterID_2.Substring(MeterID_2.Length - 2, 2); info2Display = info2Display + "\r\n\t\t\t\t测量点编号:" + sFrame.tName + "_" + MeterID_1 + MeterID_2; int nParamCount = 6; struTempTrans sAutoTrans = default(struTempTrans); sAutoTrans.TID = sFrame.tName; sAutoTrans.MID = flagPoint; sAutoTrans.monitorID = sFrame.tName + "_" + MeterID_1 + MeterID_2; sAutoTrans.TTime = "null"; sAutoTrans.TEM = "null"; sAutoTrans.HUM = "null"; sAutoTrans.illuminance = "null"; sAutoTrans.concentration = "null"; sAutoTrans.noise = "null"; while (res && pointer < (i + 1) * sectionLength) { uint ID = (uint)(sFrame.strData[pointer] * 256 + sFrame.strData[pointer + 1]); uint data = 0u; switch (ID) { case 32816u: { string strDateTime = "20" + sFrame.strData[pointer + 7].ToString("x2") + "-" + sFrame.strData[pointer + 6].ToString("x2") + "-" + sFrame.strData[pointer + 5].ToString("x2") + " " + sFrame.strData[pointer + 4].ToString("x2") + ":" + sFrame.strData[pointer + 3].ToString("x2") + ":" + sFrame.strData[pointer + 2].ToString("x2"); pointer += 8; info2Display = info2Display + "\r\n\t\t\t\t终端时间: " + strDateTime; sAutoTrans.TTime = "'" + strDateTime + "'"; nParamCount--; break; } case 36432u: data = (uint)TPackageHandler.ConvertBytesToLongInteger(TPackageHandler.CutBytes(sFrame.strData, pointer + 2, 4)); if (data == uint.MaxValue) { bDataValiad = false; } else { try { byte[] b_Press = new byte[4]; Array.Copy(sFrame.strData, pointer + 2 + 2, b_Press, 0, 2); Array.Copy(sFrame.strData, pointer + 2, b_Press, 2, 2); float f_Press = BitConverter.ToSingle(b_Press.Reverse().ToArray(), 0); if (float.IsNaN(f_Press)) { sAutoTrans.TEM = "0"; } else { sAutoTrans.TEM = Convert.ToDecimal(f_Press).ToString(); } nParamCount--; } catch (Exception) { bDataValiad = false; } } pointer += 6; info2Display = info2Display + "\r\n\t\t\t\t传感器的温度值: " + sAutoTrans.TEM; break; case 36433u: data = (uint)TPackageHandler.ConvertBytesToLongInteger(TPackageHandler.CutBytes(sFrame.strData, pointer + 2, 4)); if (data == uint.MaxValue) { bDataValiad = false; } else { try { byte[] b_Press2 = new byte[4]; Array.Copy(sFrame.strData, pointer + 2 + 2, b_Press2, 0, 2); Array.Copy(sFrame.strData, pointer + 2, b_Press2, 2, 2); float f_Press2 = BitConverter.ToSingle(b_Press2.Reverse().ToArray(), 0); if (float.IsNaN(f_Press2)) { sAutoTrans.illuminance = "0"; } else { sAutoTrans.illuminance = Convert.ToDecimal(f_Press2).ToString(); } nParamCount--; } catch (Exception) { bDataValiad = false; } } pointer += 6; info2Display = info2Display + "\r\n\t\t\t\t传感器的照度值: " + sAutoTrans.illuminance; break; case 36434u: data = (uint)TPackageHandler.ConvertBytesToLongInteger(TPackageHandler.CutBytes(sFrame.strData, pointer + 2, 4)); if (data == uint.MaxValue) { bDataValiad = false; } else { try { byte[] b_Press3 = new byte[4]; Array.Copy(sFrame.strData, pointer + 2 + 2, b_Press3, 0, 2); Array.Copy(sFrame.strData, pointer + 2, b_Press3, 2, 2); float f_Press3 = BitConverter.ToSingle(b_Press3.Reverse().ToArray(), 0); if (float.IsNaN(f_Press3)) { sAutoTrans.HUM = "0"; } else { sAutoTrans.HUM = Convert.ToDecimal(f_Press3).ToString(); } nParamCount--; } catch (Exception) { bDataValiad = false; } } pointer += 6; info2Display = info2Display + "\r\n\t\t\t\t传感器的湿度值: " + sAutoTrans.HUM; break; case 36435u: data = (uint)TPackageHandler.ConvertBytesToLongInteger(TPackageHandler.CutBytes(sFrame.strData, pointer + 2, 4)); if (data == uint.MaxValue) { bDataValiad = false; } else { try { byte[] b_Press4 = new byte[4]; Array.Copy(sFrame.strData, pointer + 2 + 2, b_Press4, 0, 2); Array.Copy(sFrame.strData, pointer + 2, b_Press4, 2, 2); float f_Press4 = BitConverter.ToSingle(b_Press4.Reverse().ToArray(), 0); if (float.IsNaN(f_Press4)) { sAutoTrans.noise = "0"; } else { sAutoTrans.noise = Convert.ToDecimal(f_Press4).ToString(); } nParamCount--; } catch (Exception) { bDataValiad = false; } } pointer += 6; info2Display = info2Display + "\r\n\t\t\t\t传感器的噪声值: " + sAutoTrans.noise; break; case 36436u: data = (uint)TPackageHandler.ConvertBytesToLongInteger(TPackageHandler.CutBytes(sFrame.strData, pointer + 2, 4)); if (data == uint.MaxValue) { bDataValiad = false; } else { try { byte[] b_Press5 = new byte[4]; Array.Copy(sFrame.strData, pointer + 2 + 2, b_Press5, 0, 2); Array.Copy(sFrame.strData, pointer + 2, b_Press5, 2, 2); float f_Press5 = BitConverter.ToSingle(b_Press5.Reverse().ToArray(), 0); if (float.IsNaN(f_Press5)) { sAutoTrans.concentration = "0"; } else { sAutoTrans.concentration = Convert.ToDecimal(f_Press5).ToString(); } nParamCount--; } catch (Exception) { bDataValiad = false; } } pointer += 6; info2Display = info2Display + "\r\n\t\t\t\t传感器的气体浓度值: " + sAutoTrans.concentration; break; default: info2Display = info2Display + "\r\n***未识别传感器对象***:0x" + ID.ToString("X4"); res = false; break; } } if (res && bDataValiad && nParamCount == 0) { lstDataStore.Add(sAutoTrans); nFlagDataStore = sFrame.flagCtrl; } } Common.logFile.write("数据解析:" + info2Display, LogFile.LOGLVL_DEBUG); Console.WriteLine("数据解析:" + info2Display); if (res) { sRespFrame.flagCtrlD = 0; sRespFrame.flagCtrlE = 0; sRespFrame.lenData = 0; sRespFrame.strData = new byte[0]; } return res; } private bool T_DataProceeding(ref struFrame sFrame, ref struFrame sRespFrame) { bool res = true; string info2Display = ""; bool bDataValiad = true; int sectionLength = 0; sectionLength = 40; int judgelenth = sFrame.lenData; if (judgelenth / sectionLength <= 0 || judgelenth % sectionLength != 0) { return false; } Common.logFile.write("[" + tcpHandler.tName + "] 主动上传数据域大小:" + sFrame.lenData, LogFile.LOGLVL_DEBUG); for (int i = 0; i < judgelenth / sectionLength; i++) { if (sFrame.flagCtrlD != 1) { continue; } byte flagPoint = sFrame.strData[i * sectionLength]; byte flagPoint2 = sFrame.strData[i * sectionLength + 1]; if (flagPoint <= 0) { continue; } string mid = flagPoint.ToString("X2") + flagPoint2.ToString("X2"); info2Display = info2Display + "\r\n\t\t\t\t测量点编号:" + mid; int pointer = 1 + i * sectionLength; int nParamCount = 6; struTempTrans sAutoTrans = default(struTempTrans); sAutoTrans.TID = "'" + sFrame.tName + "_" + mid + "'"; sAutoTrans.MID = flagPoint; sAutoTrans.TTime = "null"; sAutoTrans.TEM = "null"; sAutoTrans.HUM = "null"; sAutoTrans.illuminance = "null"; sAutoTrans.concentration = "null"; sAutoTrans.noise = "null"; while (res && pointer < (i + 1) * sectionLength) { uint ID = (uint)(sFrame.strData[pointer + 1] * 256 + sFrame.strData[pointer + 2]); uint data = 0u; switch (ID) { case 32816u: { string strDateTime = "20" + sFrame.strData[pointer + 7].ToString("x2") + "-" + sFrame.strData[pointer + 6].ToString("x2") + "-" + sFrame.strData[pointer + 5].ToString("x2") + " " + sFrame.strData[pointer + 4].ToString("x2") + ":" + sFrame.strData[pointer + 3].ToString("x2") + ":" + sFrame.strData[pointer + 2].ToString("x2"); pointer += 8; info2Display = info2Display + "\r\n\t\t\t\t终端时间: " + strDateTime; sAutoTrans.TTime = "'" + strDateTime + "'"; nParamCount--; break; } case 36432u: data = (uint)TPackageHandler.ConvertBytesToLongInteger(TPackageHandler.CutBytes(sFrame.strData, pointer + 3, 4)); if (data == uint.MaxValue) { bDataValiad = false; } else { try { byte[] b_Press = new byte[4]; Array.Copy(sFrame.strData, pointer + 2 + 2, b_Press, 0, 2); Array.Copy(sFrame.strData, pointer + 2, b_Press, 2, 2); float f_Press = BitConverter.ToSingle(b_Press.Reverse().ToArray(), 0); if (float.IsNaN(f_Press)) { sAutoTrans.TEM = "0"; } else { sAutoTrans.TEM = Convert.ToDecimal(f_Press).ToString(); } nParamCount--; } catch (Exception) { bDataValiad = false; } } pointer += 6; info2Display = info2Display + "\r\n\t\t\t\t传感器的温度值: " + sAutoTrans.TEM; break; case 36433u: data = (uint)TPackageHandler.ConvertBytesToLongInteger(TPackageHandler.CutBytes(sFrame.strData, pointer + 3, 4)); if (data == 16777215) { bDataValiad = false; } else { try { byte[] b_Press2 = new byte[4]; Array.Copy(sFrame.strData, pointer + 2 + 2, b_Press2, 0, 2); Array.Copy(sFrame.strData, pointer + 2, b_Press2, 2, 2); float f_Press2 = BitConverter.ToSingle(b_Press2.Reverse().ToArray(), 0); if (float.IsNaN(f_Press2)) { sAutoTrans.illuminance = "0"; } else { sAutoTrans.illuminance = Convert.ToDecimal(f_Press2).ToString(); } nParamCount--; } catch (Exception) { bDataValiad = false; } } pointer += 6; info2Display = info2Display + "\r\n\t\t\t\t传感器的照度值: " + sAutoTrans.TEM; break; case 36434u: data = (uint)TPackageHandler.ConvertBytesToLongInteger(TPackageHandler.CutBytes(sFrame.strData, pointer + 3, 4)); if (data == 16777215) { bDataValiad = false; } else { try { byte[] b_Press3 = new byte[4]; Array.Copy(sFrame.strData, pointer + 2 + 2, b_Press3, 0, 2); Array.Copy(sFrame.strData, pointer + 2, b_Press3, 2, 2); float f_Press3 = BitConverter.ToSingle(b_Press3.Reverse().ToArray(), 0); if (float.IsNaN(f_Press3)) { sAutoTrans.HUM = "0"; } else { sAutoTrans.HUM = Convert.ToDecimal(f_Press3).ToString(); } nParamCount--; } catch (Exception) { bDataValiad = false; } } pointer += 6; info2Display = info2Display + "\r\n\t\t\t\t传感器的湿度值: " + sAutoTrans.HUM; break; case 36435u: data = (uint)TPackageHandler.ConvertBytesToLongInteger(TPackageHandler.CutBytes(sFrame.strData, pointer + 3, 4)); if (data == 16777215) { bDataValiad = false; } else { try { byte[] b_Press4 = new byte[4]; Array.Copy(sFrame.strData, pointer + 2 + 2, b_Press4, 0, 2); Array.Copy(sFrame.strData, pointer + 2, b_Press4, 2, 2); float f_Press4 = BitConverter.ToSingle(b_Press4.Reverse().ToArray(), 0); if (float.IsNaN(f_Press4)) { sAutoTrans.noise = "0"; } else { sAutoTrans.noise = Convert.ToDecimal(f_Press4).ToString(); } nParamCount--; } catch (Exception) { bDataValiad = false; } } pointer += 6; info2Display = info2Display + "\r\n\t\t\t\t传感器的噪声值: " + sAutoTrans.HUM; break; default: info2Display = info2Display + "\r\n***未识别传感器对象***:0x" + ID.ToString("X4"); res = false; break; case 36436u: break; } } string strDateTimeData = "20" + sFrame.strData[sFrame.lenData - 1].ToString("x2") + "-" + sFrame.strData[sFrame.lenData - 2].ToString("x2") + "-" + sFrame.strData[sFrame.lenData - 3].ToString("x2") + " " + sFrame.strData[sFrame.lenData - 4].ToString("x2") + ":" + sFrame.strData[sFrame.lenData - 5].ToString("x2") + ":" + sFrame.strData[sFrame.lenData - 6].ToString("x2"); info2Display = info2Display + "\r\n\t\t\t\t终端时间: " + strDateTimeData; sAutoTrans.TTime = "'" + strDateTimeData + "'"; if (res && bDataValiad && nParamCount == 0) { lstDataStore.Add(sAutoTrans); nFlagDataStore = sFrame.flagCtrl; } } Common.logFile.write("数据解析:" + info2Display, LogFile.LOGLVL_DEBUG); if (res) { sRespFrame.flagCtrlD = 0; sRespFrame.flagCtrlE = 0; sRespFrame.lenData = 0; sRespFrame.strData = new byte[0]; } return res; } private bool E_DataProceeding(ref struFrame sFrame, ref struFrame sRespFrame) { bool res = true; string info2Display = ""; bool bDataValiad = true; int sectionLength = 0; sectionLength = 58; if (sFrame.lenData / sectionLength <= 0 || sFrame.lenData % sectionLength != 0) { return false; } Common.logFile.write("[" + tcpHandler.tName + "] 主动上传数据域大小:" + sFrame.lenData, LogFile.LOGLVL_DEBUG); for (int i = 0; i < sFrame.lenData / sectionLength; i++) { if (sFrame.flagCtrlD != 1) { continue; } byte flagPoint = sFrame.strData[i * sectionLength]; if (flagPoint <= 0) { continue; } int pointer = 2 + i * sectionLength; string MeterID_1 = "00" + Convert.ToInt32(sFrame.strData[pointer - 2]); MeterID_1 = MeterID_1.Substring(MeterID_1.Length - 2, 2); string MeterID_2 = "00" + Convert.ToInt32(sFrame.strData[pointer - 1]); MeterID_2 = MeterID_2.Substring(MeterID_2.Length - 2, 2); info2Display = info2Display + "\r\n\t\t\t\t测量点编号:" + sFrame.tName + "_" + MeterID_1 + MeterID_2; int nParamCount = 9; struAutoTrans sAutoTrans = default(struAutoTrans); sAutoTrans.TID = sFrame.tName; sAutoTrans.MID = flagPoint; sAutoTrans.monitorID = sFrame.tName + "_" + MeterID_1 + MeterID_2; sAutoTrans.TTime = "null"; sAutoTrans.UA = "null"; sAutoTrans.UB = "null"; sAutoTrans.UC = "null"; sAutoTrans.IA = "null"; sAutoTrans.IB = "null"; sAutoTrans.IC = "null"; sAutoTrans.YGZ = "null"; sAutoTrans.GP = "null"; while (res && pointer < (i + 1) * sectionLength) { uint ID = (uint)(sFrame.strData[pointer] * 256 + sFrame.strData[pointer + 1]); uint data = 0u; switch (ID) { case 32816u: { string strDateTime = "20" + sFrame.strData[pointer + 7].ToString("x2") + "-" + sFrame.strData[pointer + 6].ToString("x2") + "-" + sFrame.strData[pointer + 5].ToString("x2") + " " + sFrame.strData[pointer + 4].ToString("x2") + ":" + sFrame.strData[pointer + 3].ToString("x2") + ":" + sFrame.strData[pointer + 2].ToString("x2"); pointer += 8; info2Display = info2Display + "\r\n\t\t\t\t终端时间: " + strDateTime; sAutoTrans.TTime = "'" + strDateTime + "'"; nParamCount--; break; } case 36880u: data = (uint)TPackageHandler.ConvertBytesToLongInteger(TPackageHandler.CutBytes(sFrame.strData, pointer + 2, 4)); if (data == uint.MaxValue) { bDataValiad = false; } else { byte[] b_Press = new byte[4]; Array.Copy(sFrame.strData, pointer + 2 + 2, b_Press, 0, 2); Array.Copy(sFrame.strData, pointer + 2, b_Press, 2, 2); float f_Press = BitConverter.ToSingle(b_Press.Reverse().ToArray(), 0); if (float.IsNaN(f_Press)) { sAutoTrans.YGZ = "0"; } else { sAutoTrans.YGZ = Convert.ToDecimal(f_Press).ToString(); } nParamCount--; } info2Display = info2Display + "\r\n\t\t\t\t正向有功总电能: " + sAutoTrans.YGZ; pointer += 6; break; case 36369u: data = (uint)TPackageHandler.ConvertBytesToLongInteger(TPackageHandler.CutBytes(sFrame.strData, pointer + 2, 4)); if (data == uint.MaxValue) { bDataValiad = false; } else { try { byte[] b_Press6 = new byte[4]; Array.Copy(sFrame.strData, pointer + 2 + 2, b_Press6, 0, 2); Array.Copy(sFrame.strData, pointer + 2, b_Press6, 2, 2); float f_Press6 = BitConverter.ToSingle(b_Press6.Reverse().ToArray(), 0); if (float.IsNaN(f_Press6)) { sAutoTrans.UA = "0"; } else { sAutoTrans.UA = Convert.ToDecimal(f_Press6).ToString(); } nParamCount--; } catch (Exception) { bDataValiad = false; } } pointer += 6; info2Display = info2Display + "\r\n\t\t\t\t电力仪表的A相电压: " + sAutoTrans.UA; break; case 36370u: data = (uint)TPackageHandler.ConvertBytesToLongInteger(TPackageHandler.CutBytes(sFrame.strData, pointer + 2, 4)); if (data == uint.MaxValue) { bDataValiad = false; } else { try { byte[] b_Press7 = new byte[4]; Array.Copy(sFrame.strData, pointer + 2 + 2, b_Press7, 0, 2); Array.Copy(sFrame.strData, pointer + 2, b_Press7, 2, 2); float f_Press7 = BitConverter.ToSingle(b_Press7.Reverse().ToArray(), 0); if (float.IsNaN(f_Press7)) { sAutoTrans.UB = "0"; } else { sAutoTrans.UB = Convert.ToDecimal(f_Press7).ToString(); } nParamCount--; } catch (Exception) { bDataValiad = false; } } pointer += 6; info2Display = info2Display + "\r\n\t\t\t\t电力仪表的B相电压: " + sAutoTrans.UB; break; case 36371u: data = (uint)TPackageHandler.ConvertBytesToLongInteger(TPackageHandler.CutBytes(sFrame.strData, pointer + 2, 4)); if (data == uint.MaxValue) { bDataValiad = false; } else { try { byte[] b_Press8 = new byte[4]; Array.Copy(sFrame.strData, pointer + 2 + 2, b_Press8, 0, 2); Array.Copy(sFrame.strData, pointer + 2, b_Press8, 2, 2); float f_Press8 = BitConverter.ToSingle(b_Press8.Reverse().ToArray(), 0); if (float.IsNaN(f_Press8)) { sAutoTrans.UC = "0"; } else { sAutoTrans.UC = Convert.ToDecimal(f_Press8).ToString(); } nParamCount--; } catch (Exception) { bDataValiad = false; } } pointer += 6; info2Display = info2Display + "\r\n\t\t\t\t电力仪表的C相电压: " + sAutoTrans.UC; break; case 36385u: data = (uint)TPackageHandler.ConvertBytesToLongInteger(TPackageHandler.CutBytes(sFrame.strData, pointer + 2, 4)); if (data == uint.MaxValue) { bDataValiad = false; } else { try { byte[] b_Press3 = new byte[4]; Array.Copy(sFrame.strData, pointer + 2 + 2, b_Press3, 0, 2); Array.Copy(sFrame.strData, pointer + 2, b_Press3, 2, 2); float f_Press3 = BitConverter.ToSingle(b_Press3.Reverse().ToArray(), 0); if (float.IsNaN(f_Press3)) { sAutoTrans.IA = "0"; } else { sAutoTrans.IA = Convert.ToDecimal(f_Press3).ToString(); } nParamCount--; } catch (Exception) { bDataValiad = false; } } pointer += 6; info2Display = info2Display + "\r\n\t\t\t\t电力仪表的A相电流: " + sAutoTrans.IA; break; case 36386u: data = (uint)TPackageHandler.ConvertBytesToLongInteger(TPackageHandler.CutBytes(sFrame.strData, pointer + 2, 4)); if (data == uint.MaxValue) { bDataValiad = false; } else { try { byte[] b_Press4 = new byte[4]; Array.Copy(sFrame.strData, pointer + 2 + 2, b_Press4, 0, 2); Array.Copy(sFrame.strData, pointer + 2, b_Press4, 2, 2); float f_Press4 = BitConverter.ToSingle(b_Press4.Reverse().ToArray(), 0); if (float.IsNaN(f_Press4)) { sAutoTrans.IB = "0"; } else { sAutoTrans.IB = Convert.ToDecimal(f_Press4).ToString(); } nParamCount--; } catch (Exception) { bDataValiad = false; } } pointer += 6; info2Display = info2Display + "\r\n\t\t\t\t电力仪表的B相电流: " + sAutoTrans.IB; break; case 36387u: data = (uint)TPackageHandler.ConvertBytesToLongInteger(TPackageHandler.CutBytes(sFrame.strData, pointer + 2, 4)); if (data == uint.MaxValue) { bDataValiad = false; } else { try { byte[] b_Press5 = new byte[4]; Array.Copy(sFrame.strData, pointer + 2 + 2, b_Press5, 0, 2); Array.Copy(sFrame.strData, pointer + 2, b_Press5, 2, 2); float f_Press5 = BitConverter.ToSingle(b_Press5.Reverse().ToArray(), 0); if (float.IsNaN(f_Press5)) { sAutoTrans.IC = "0"; } else { sAutoTrans.IC = Convert.ToDecimal(f_Press5).ToString(); } nParamCount--; } catch (Exception) { bDataValiad = false; } } pointer += 6; info2Display = info2Display + "\r\n\t\t\t\t电力仪表的C相电流: " + sAutoTrans.IC; break; case 46672u: data = (uint)TPackageHandler.ConvertBytesToLongInteger(TPackageHandler.CutBytes(sFrame.strData, pointer + 2, 4)); if (data == 16777215) { bDataValiad = false; } else { try { byte[] b_Press2 = new byte[4]; Array.Copy(sFrame.strData, pointer + 2 + 2, b_Press2, 0, 2); Array.Copy(sFrame.strData, pointer + 2, b_Press2, 2, 2); float f_Press2 = BitConverter.ToSingle(b_Press2.Reverse().ToArray(), 0); if (float.IsNaN(f_Press2)) { sAutoTrans.GP = "0"; } else { sAutoTrans.GP = Convert.ToDecimal(f_Press2).ToString(); } nParamCount--; } catch (Exception) { bDataValiad = false; } } pointer += 6; info2Display = info2Display + "\r\n\t\t\t\t电力仪表的总功率因数: " + sAutoTrans.GP; break; default: info2Display = info2Display + "\r\n***未识别电表对象***:0x" + ID.ToString("X4"); res = false; break; } } if (res && ((bDataValiad && nParamCount == 0) || (sAutoTrans.YGZ.CompareTo("null") != 0 && sAutoTrans.YGZ.CompareTo("''") != 0))) { lstDataStore.Add(sAutoTrans); nFlagDataStore = sFrame.flagCtrl; } } Common.logFile.write("数据解析:" + info2Display, LogFile.LOGLVL_DEBUG); if (res) { sRespFrame.flagCtrlD = 0; sRespFrame.flagCtrlE = 0; sRespFrame.lenData = 0; sRespFrame.strData = new byte[0]; } return res; } private string calcYGZ(string sTID, int nMID, uint unYGZ, ref int PT, ref int CT) { ulong lYGZ = 0uL; string sYGZ = ""; try { lYGZ = Convert.ToUInt32(unYGZ.ToString("x")); PT = 0; CT = 0; if (htEParams != null && htEParams.Count > 0) { E_MeterParams e_MeterParams = (E_MeterParams)htEParams[sTID + nMID.ToString("d2")]; if (e_MeterParams != null) { PT = e_MeterParams.nPT; CT = e_MeterParams.nCT; sYGZ = lYGZ / 100uL + "." + (lYGZ % 100uL).ToString("d2"); } } } catch (Exception e1) { Common.logErrFile.write(e1.ToString(), LogFile.LOGLVL_ERROR); } return sYGZ; } private bool S_DataProceeding(ref struFrame sFrame, ref struFrame sRespFrame) { bool res = true; string info2Display = ""; int sectionLength = 58; int lenRemain = sFrame.lenData; for (int i = 0; i < sFrame.lenData / sectionLength; i++) { if (sFrame.flagCtrlD != 1) { continue; } byte flagPoint = sFrame.strData[i * sectionLength]; if (flagPoint <= 0) { continue; } struSDataTrans sDataTrans = default(struSDataTrans); int pointer = 2 + i * sectionLength; string MeterID_1 = "00" + Convert.ToInt32(sFrame.strData[pointer - 2]); MeterID_1 = MeterID_1.Substring(MeterID_1.Length - 2, 2); string MeterID_2 = "00" + Convert.ToInt32(sFrame.strData[pointer - 1]); MeterID_2 = MeterID_2.Substring(MeterID_2.Length - 2, 2); sDataTrans.monitorID = sFrame.tName + "_" + MeterID_1 + MeterID_2; info2Display = info2Display + "\r\n\t\t\t\t测量点编号:" + sDataTrans.monitorID; sDataTrans.TID = sFrame.tName; sDataTrans.MID = flagPoint; sDataTrans.CHAYA = "null"; sDataTrans.LJLL = "null"; sDataTrans.LJRL = "null"; sDataTrans.MIDU = "null"; sDataTrans.SSLL = "null"; sDataTrans.SSRL = "null"; sDataTrans.WD = "null"; sDataTrans.YL = "null"; sDataTrans.Time = "null"; sDataTrans.Num = "null"; bool bDataValiad = true; while (res && pointer < (i + 1) * sectionLength) { uint ID = (uint)(sFrame.strData[pointer] * 256 + sFrame.strData[pointer + 1]); uint data = 0u; switch (ID) { case 32816u: { string strDateTime = "20" + sFrame.strData[pointer + 7].ToString("x2") + "-" + sFrame.strData[pointer + 6].ToString("x2") + "-" + sFrame.strData[pointer + 5].ToString("x2") + " " + sFrame.strData[pointer + 4].ToString("x2") + ":" + sFrame.strData[pointer + 3].ToString("x2") + ":" + sFrame.strData[pointer + 2].ToString("x2"); pointer += 8; info2Display = info2Display + "\r\n\t\t\t\t终端时间:" + strDateTime; sDataTrans.TTime = strDateTime; break; } case 39680u: data = (uint)TPackageHandler.ConvertBytesToLongInteger(TPackageHandler.CutBytes(sFrame.strData, pointer + 2, 4)); if (data == uint.MaxValue) { bDataValiad = false; } else { try { byte[] b_Press2 = new byte[4]; Array.Copy(sFrame.strData, pointer + 2 + 2, b_Press2, 0, 2); Array.Copy(sFrame.strData, pointer + 2, b_Press2, 2, 2); float f_Press2 = BitConverter.ToSingle(b_Press2.Reverse().ToArray(), 0); if (float.IsNaN(f_Press2)) { sDataTrans.YL = "0"; } else { sDataTrans.YL = Convert.ToDecimal(f_Press2).ToString(); } } catch (Exception) { bDataValiad = false; } } pointer += 6; info2Display = info2Display + "\r\n\t\t\t\t蒸汽仪表压力值:" + sDataTrans.YL; break; case 39711u: data = (uint)TPackageHandler.ConvertBytesToLongInteger(TPackageHandler.CutBytes(sFrame.strData, pointer + 2, 4)); if (data == uint.MaxValue) { bDataValiad = false; } else { try { byte[] b_Press = new byte[4]; Array.Copy(sFrame.strData, pointer + 2 + 2, b_Press, 0, 2); Array.Copy(sFrame.strData, pointer + 2, b_Press, 2, 2); float f_Press = BitConverter.ToSingle(b_Press.Reverse().ToArray(), 0); if (float.IsNaN(f_Press)) { sDataTrans.YL = "0"; } else { sDataTrans.YL = Convert.ToDecimal(f_Press).ToString(); } } catch (Exception) { bDataValiad = false; } } pointer += 6; info2Display = info2Display + "\r\n\t\t\t\t蒸汽仪表压力值:" + sDataTrans.YL; break; case 39681u: data = (uint)TPackageHandler.ConvertBytesToLongInteger(TPackageHandler.CutBytes(sFrame.strData, pointer + 2, 4)); if (data == uint.MaxValue) { bDataValiad = false; } else { try { byte[] b_Press3 = new byte[4]; Array.Copy(sFrame.strData, pointer + 2 + 2, b_Press3, 0, 2); Array.Copy(sFrame.strData, pointer + 2, b_Press3, 2, 2); float f_Press3 = BitConverter.ToSingle(b_Press3.Reverse().ToArray(), 0); if (float.IsNaN(f_Press3)) { sDataTrans.WD = "0"; } else { sDataTrans.WD = Convert.ToDecimal(f_Press3).ToString(); } } catch (Exception) { bDataValiad = false; } } pointer += 6; info2Display = info2Display + "\r\n\t\t\t\t蒸汽仪表温度值:" + sDataTrans.WD; break; case 39697u: data = (uint)TPackageHandler.ConvertBytesToLongInteger(TPackageHandler.CutBytes(sFrame.strData, pointer + 2, 4)); if (data == uint.MaxValue) { bDataValiad = false; } else { try { byte[] b_Press4 = new byte[4]; Array.Copy(sFrame.strData, pointer + 2 + 2, b_Press4, 0, 2); Array.Copy(sFrame.strData, pointer + 2, b_Press4, 2, 2); float f_Press4 = BitConverter.ToSingle(b_Press4.Reverse().ToArray(), 0); if (float.IsNaN(f_Press4)) { sDataTrans.WD = "0"; } else { sDataTrans.WD = Convert.ToDecimal(f_Press4).ToString(); } } catch (Exception) { bDataValiad = false; } } pointer += 6; info2Display = info2Display + "\r\n\t\t\t\t蒸汽仪表温度值: " + sDataTrans.WD; break; case 39682u: data = (uint)TPackageHandler.ConvertBytesToLongInteger(TPackageHandler.CutBytes(sFrame.strData, pointer + 2, 4)); if (data == uint.MaxValue) { bDataValiad = false; } else { try { byte[] b_Press5 = new byte[4]; Array.Copy(sFrame.strData, pointer + 2 + 2, b_Press5, 0, 2); Array.Copy(sFrame.strData, pointer + 2, b_Press5, 2, 2); float f_Press5 = BitConverter.ToSingle(b_Press5.Reverse().ToArray(), 0); if (float.IsNaN(f_Press5)) { sDataTrans.PL = "0"; } else { sDataTrans.PL = Convert.ToDecimal(f_Press5).ToString(); } } catch (Exception) { bDataValiad = false; } } pointer += 6; info2Display = info2Display + "\r\n\t\t\t\t蒸汽仪表频率值:" + sDataTrans.PL; break; case 39694u: data = (uint)TPackageHandler.ConvertBytesToLongInteger(TPackageHandler.CutBytes(sFrame.strData, pointer + 2, 4)); if (data == uint.MaxValue) { bDataValiad = false; } else { try { byte[] b_Press6 = new byte[4]; Array.Copy(sFrame.strData, pointer + 2 + 2, b_Press6, 0, 2); Array.Copy(sFrame.strData, pointer + 2, b_Press6, 2, 2); float f_Press6 = BitConverter.ToSingle(b_Press6.Reverse().ToArray(), 0); if (float.IsNaN(f_Press6)) { sDataTrans.MIDU = "0"; } else { sDataTrans.MIDU = Convert.ToDecimal(f_Press6).ToString(); } } catch (Exception) { bDataValiad = false; } } pointer += 6; info2Display = info2Display + "\r\n\t\t\t\t蒸汽仪表密度值:" + sDataTrans.MIDU; break; case 39695u: data = (uint)TPackageHandler.ConvertBytesToLongInteger(TPackageHandler.CutBytes(sFrame.strData, pointer + 2, 4)); if (data == uint.MaxValue) { bDataValiad = false; } else { try { byte[] b_Press7 = new byte[4]; Array.Copy(sFrame.strData, pointer + 2 + 2, b_Press7, 0, 2); Array.Copy(sFrame.strData, pointer + 2, b_Press7, 2, 2); float f_Press7 = BitConverter.ToSingle(b_Press7.Reverse().ToArray(), 0); if (float.IsNaN(f_Press7)) { sDataTrans.CHAYA = "0"; } else { sDataTrans.CHAYA = Convert.ToDecimal(f_Press7).ToString(); } } catch (Exception) { bDataValiad = false; } } pointer += 6; info2Display = info2Display + "\r\n\t\t\t\t蒸汽仪表差压值:" + sDataTrans.CHAYA; break; case 39683u: data = (uint)TPackageHandler.ConvertBytesToLongInteger(TPackageHandler.CutBytes(sFrame.strData, pointer + 2, 4)); if (data == uint.MaxValue) { bDataValiad = false; } else { try { byte[] b_Press8 = new byte[4]; Array.Copy(sFrame.strData, pointer + 2 + 2, b_Press8, 0, 2); Array.Copy(sFrame.strData, pointer + 2, b_Press8, 2, 2); float f_Press8 = BitConverter.ToSingle(b_Press8.Reverse().ToArray(), 0); if (float.IsNaN(f_Press8)) { sDataTrans.SSLL = "0"; } else { sDataTrans.SSLL = Convert.ToDecimal(f_Press8).ToString(); } } catch (Exception) { bDataValiad = false; } } pointer += 6; info2Display = info2Display + "\r\n\t\t\t\t蒸汽仪表瞬时流量值:" + sDataTrans.SSLL; break; case 39685u: { byte[] b_Press11 = new byte[4]; Array.Copy(sFrame.strData, pointer + 2 + 2, b_Press11, 0, 2); Array.Copy(sFrame.strData, pointer + 2, b_Press11, 2, 2); float f_Press11 = BitConverter.ToSingle(b_Press11.Reverse().ToArray(), 0); if (float.IsNaN(f_Press11)) { sDataTrans.LJLL = "0"; } else { sDataTrans.LJLL = Convert.ToDecimal(f_Press11).ToString(); } pointer += 6; info2Display = info2Display + "\r\n\t\t\t\t蒸汽仪表累积流量值:" + sDataTrans.LJLL; break; } case 39686u: data = (uint)TPackageHandler.ConvertBytesToLongInteger(TPackageHandler.CutBytes(sFrame.strData, pointer + 2, 4)); if (data == uint.MaxValue) { bDataValiad = false; } else { try { byte[] b_Press10 = new byte[4]; Array.Copy(sFrame.strData, pointer + 2 + 2, b_Press10, 0, 2); Array.Copy(sFrame.strData, pointer + 2, b_Press10, 2, 2); float f_Press10 = BitConverter.ToSingle(b_Press10.Reverse().ToArray(), 0); if (float.IsNaN(f_Press10)) { sDataTrans.SSRL = "0"; } else { sDataTrans.SSRL = Convert.ToDecimal(f_Press10).ToString(); } } catch (Exception) { bDataValiad = false; } } pointer += 6; info2Display = info2Display + "\r\n\t\t\t\t蒸汽仪表瞬时热量:" + sDataTrans.SSRL; break; case 39687u: { byte[] b_Press9 = new byte[4]; Array.Copy(sFrame.strData, pointer + 2 + 2, b_Press9, 0, 2); Array.Copy(sFrame.strData, pointer + 2, b_Press9, 2, 2); float f_Press9 = BitConverter.ToSingle(b_Press9.Reverse().ToArray(), 0); if (float.IsNaN(f_Press9)) { sDataTrans.LJRL = "0"; } else { sDataTrans.LJRL = Convert.ToDecimal(f_Press9).ToString(); } pointer += 6; info2Display = info2Display + "\r\n\t\t\t\t蒸汽仪表累积热量:" + sDataTrans.LJRL; break; } default: res = false; bDataValiad = false; break; } } if (res && bDataValiad) { lstDataStore.Add(sDataTrans); nFlagDataStore = sFrame.flagCtrl; } } if (res) { sRespFrame.flagCtrlD = 0; sRespFrame.flagCtrlE = 0; sRespFrame.lenData = 0; sRespFrame.strData = new byte[0]; Common.logFile.write("[" + sFrame.tName + "] 数据解析:" + info2Display, LogFile.LOGLVL_DEBUG); } return res; } private bool W_DataProceeding(ref struFrame sFrame, ref struFrame sRespFrame) { return false; } private bool S_DataParsing(ref struFrame sFrame, ref Hashtable htData) { bool res = false; string info2Display = ""; bool bDataValiad = true; if (sFrame.flagCtrlD == 1) { byte flagPoint = sFrame.strData[0]; if (flagPoint > 0) { info2Display = info2Display + "\r\n\t\t\t\t测量点编号:" + flagPoint.ToString("X2"); int pointer = 1; while (pointer < sFrame.lenData) { uint ID = (uint)(sFrame.strData[pointer] * 256 + sFrame.strData[pointer + 1]); uint data = 0u; switch (ID) { case 32816u: { string strDateTime = "20" + sFrame.strData[pointer + 7].ToString("x2") + "-" + sFrame.strData[pointer + 6].ToString("x2") + "-" + sFrame.strData[pointer + 5].ToString("x2") + " " + sFrame.strData[pointer + 4].ToString("x2") + ":" + sFrame.strData[pointer + 3].ToString("x2") + ":" + sFrame.strData[pointer + 2].ToString("x2"); pointer += 8; info2Display = info2Display + "\r\n\t\t\t\t终端时间:" + strDateTime; htData.Add(ID, strDateTime); break; } case 39680u: data = (uint)TPackageHandler.ConvertBytesToLongInteger(TPackageHandler.CutBytes(sFrame.strData, pointer + 2, 2)); if (data == 65535) { bDataValiad = false; } else { try { data = Convert.ToUInt32(data.ToString("X4")); string d = data.ToString("D4"); htData.Add(ID, d.Substring(0, 1) + "." + d.Substring(1, 3)); } catch (Exception) { bDataValiad = false; } } pointer += 4; info2Display = info2Display + "\r\n\t\t\t\t蒸汽仪表压力值:" + data.ToString("D4").Substring(0, 1) + "." + data.ToString("D4").Substring(1, 3); break; case 39681u: data = (uint)TPackageHandler.ConvertBytesToLongInteger(TPackageHandler.CutBytes(sFrame.strData, pointer + 2, 2)); if (data == 65535) { bDataValiad = false; } else { try { data = Convert.ToUInt32(data.ToString("X4")); htData.Add(ID, data / 10u + "." + data % 10u); } catch (Exception) { bDataValiad = false; } } pointer += 4; info2Display = info2Display + "\r\n\t\t\t\t蒸汽仪表温度值:" + data / 10u + "." + data % 10u; break; case 39711u: data = (uint)TPackageHandler.ConvertBytesToLongInteger(TPackageHandler.CutBytes(sFrame.strData, pointer + 2, 2)); if (data == 65535) { bDataValiad = false; } else { try { data = Convert.ToUInt32(data.ToString("X4")); string d2 = data.ToString("D4"); htData.Add(ID, "-" + d2.Substring(0, 1) + "." + d2.Substring(1, 3)); } catch (Exception) { bDataValiad = false; } } pointer += 4; info2Display = info2Display + "\r\n\t\t\t\t蒸汽仪表压力值:-" + data.ToString("D4").Substring(0, 1) + "." + data.ToString("D4").Substring(1, 3); break; case 39697u: data = (uint)TPackageHandler.ConvertBytesToLongInteger(TPackageHandler.CutBytes(sFrame.strData, pointer + 2, 2)); if (data == 65535) { bDataValiad = false; } else { try { data = Convert.ToUInt32(data.ToString("X4")); htData.Add(ID, "-" + data / 10u + "." + data % 10u); } catch (Exception) { bDataValiad = false; } } pointer += 4; info2Display = info2Display + "\r\n\t\t\t\t蒸汽仪表温度值: -" + data / 10u + "." + data % 10u; break; case 39682u: data = (uint)TPackageHandler.ConvertBytesToLongInteger(TPackageHandler.CutBytes(sFrame.strData, pointer + 2, 3)); if (data == 16777215) { bDataValiad = false; } else { try { data = Convert.ToUInt32(data.ToString("X6")); htData.Add(ID, data / 10u + "." + data % 10u); } catch (Exception) { bDataValiad = false; } } pointer += 5; info2Display = info2Display + "\r\n\t\t\t\t蒸汽仪表频率值:" + data / 10u + "." + data % 10u; break; case 39694u: { data = (uint)TPackageHandler.ConvertBytesToLongInteger(TPackageHandler.CutBytes(sFrame.strData, pointer + 2, 3)); string midu = ""; if (data == 16777215) { bDataValiad = false; } else { try { data = Convert.ToUInt32(data.ToString("X6")); if (data < 10) { midu = ((double)data * 0.01).ToString(); htData.Add(ID, midu); } else { midu = data / 100u + "." + data % 100u; htData.Add(ID, data / 100u + "." + data % 100u); } } catch (Exception) { bDataValiad = false; } } pointer += 5; info2Display = info2Display + "\r\n\t\t\t\t蒸汽仪表密度值:" + midu; break; } case 39695u: { data = (uint)TPackageHandler.ConvertBytesToLongInteger(TPackageHandler.CutBytes(sFrame.strData, pointer + 2, 3)); string chaya = ""; if (data == 16777215) { bDataValiad = false; } else { try { data = Convert.ToUInt32(data.ToString("X6")); if (data < 10) { chaya = ((double)data * 0.01).ToString(); htData.Add(ID, chaya); } else { chaya = data / 100u + "." + data % 100u; htData.Add(ID, data / 100u + "." + data % 100u); } } catch (Exception) { bDataValiad = false; } } pointer += 5; info2Display = info2Display + "\r\n\t\t\t\t蒸汽仪表差压值:" + chaya; break; } case 39683u: data = (uint)TPackageHandler.ConvertBytesToLongInteger(TPackageHandler.CutBytes(sFrame.strData, pointer + 2, 4)); if (data == uint.MaxValue) { bDataValiad = false; } else { try { data = Convert.ToUInt32(data.ToString("X8")); htData.Add(ID, data / 10u + "." + data % 10u); } catch (Exception) { bDataValiad = false; } } pointer += 6; info2Display = info2Display + "\r\n\t\t\t\t蒸汽仪表瞬时流量值:" + data / 10u + "." + data % 10u; break; case 39685u: { byte[] temp = new byte[6]; string tempLiji = ""; for (int j = 0; j < temp.Length; j++) { temp[j] = sFrame.strData[pointer + 2 + j]; } for (int i = 0; i < temp.Length; i++) { if (temp[i].ToString("X2") == "FF") { bDataValiad = false; break; } tempLiji += temp[i].ToString("X2"); } try { htData.Add(ID, Convert.ToDouble(tempLiji).ToString()); } catch (Exception) { bDataValiad = false; } pointer += 8; info2Display = info2Display + "\r\n\t\t\t\t蒸汽仪表累积流量值:" + Convert.ToDouble(tempLiji); break; } case 39686u: { string sxrl = "NULL"; data = (uint)TPackageHandler.ConvertBytesToLongInteger(TPackageHandler.CutBytes(sFrame.strData, pointer + 2, 4)); if (data == uint.MaxValue) { bDataValiad = false; } else { try { data = Convert.ToUInt32(data.ToString("X8")); sxrl = ((data >= 10) ? (data / 100u + "." + data % 100u) : ((double)data * 0.01).ToString()); htData.Add(ID, sxrl); } catch (Exception) { bDataValiad = false; } } pointer += 6; info2Display = info2Display + "\r\n\t\t\t\t蒸汽仪表瞬时热量:" + sxrl; break; } case 39687u: { byte[] templjrlarr = new byte[5]; string tempLjrl = ""; for (int l = 0; l < templjrlarr.Length; l++) { templjrlarr[l] = sFrame.strData[pointer + 2 + l]; } for (int k = 0; k < templjrlarr.Length; k++) { if (templjrlarr[k].ToString("X2") == "FF") { bDataValiad = false; break; } tempLjrl += templjrlarr[k].ToString("X2"); } try { htData.Add(ID, Convert.ToDouble(tempLjrl).ToString()); } catch (Exception) { bDataValiad = false; } pointer += 7; info2Display = info2Display + "\r\n\t\t\t\t蒸汽仪表累积热量:" + Convert.ToDouble(tempLjrl); break; } } } } res = bDataValiad; } res = true; if (res) { Common.logFile.write("数据解析:" + info2Display, LogFile.LOGLVL_DEBUG); } return res; } private bool E_DataParsing(ref struFrame sFrame, ref Hashtable htData) { bool res = false; string info2Display = ""; bool bDataValiad = true; if (sFrame.flagCtrlD == 1) { byte flagPoint = sFrame.strData[0]; if (flagPoint > 0) { info2Display = info2Display + "\r\n\t\t\t\t测量点编号:" + flagPoint.ToString("D2"); int pointer = 1; while (pointer < sFrame.lenData) { uint ID = (uint)(sFrame.strData[pointer] * 256 + sFrame.strData[pointer + 1]); uint data = 0u; switch (ID) { case 32816u: { string strDateTime = "20" + sFrame.strData[pointer + 7].ToString("x2") + "-" + sFrame.strData[pointer + 6].ToString("x2") + "-" + sFrame.strData[pointer + 5].ToString("x2") + " " + sFrame.strData[pointer + 4].ToString("x2") + ":" + sFrame.strData[pointer + 3].ToString("x2") + ":" + sFrame.strData[pointer + 2].ToString("x2"); pointer += 8; info2Display = info2Display + "\r\n\t\t\t\t终端时间:" + strDateTime; htData.Add(ID, strDateTime); break; } case 36369u: Common.logFile.write("AU:In2", LogFile.LOGLVL_DEBUG); htData.Add(ID, ""); data = (uint)TPackageHandler.ConvertBytesToLongInteger(TPackageHandler.CutBytes(sFrame.strData, pointer + 2, 4)); if (data != uint.MaxValue) { try { data = Convert.ToUInt32(data.ToString("X8")); htData[ID] = data / 100u + "." + (data % 100u).ToString("d2"); } catch (Exception) { bDataValiad = false; } } pointer += 6; info2Display = info2Display + "\r\n\t\t\t\ta相电压:" + data.ToString("D8"); break; case 36370u: htData.Add(ID, ""); data = (uint)TPackageHandler.ConvertBytesToLongInteger(TPackageHandler.CutBytes(sFrame.strData, pointer + 2, 4)); if (data != uint.MaxValue) { try { data = Convert.ToUInt32(data.ToString("X8")); htData[ID] = data / 100u + "." + (data % 100u).ToString("d2"); } catch (Exception) { bDataValiad = false; } } pointer += 6; info2Display = info2Display + "\r\n\t\t\t\tb相电压:" + data.ToString("D8"); break; case 36371u: htData.Add(ID, ""); data = (uint)TPackageHandler.ConvertBytesToLongInteger(TPackageHandler.CutBytes(sFrame.strData, pointer + 2, 4)); if (data != uint.MaxValue) { try { data = Convert.ToUInt32(data.ToString("X8")); htData[ID] = data / 100u + "." + (data % 100u).ToString("d2"); } catch (Exception) { bDataValiad = false; } } pointer += 6; info2Display = info2Display + "\r\n\t\t\t\tc相电压:" + data.ToString("D8"); break; case 36385u: htData.Add(ID, ""); data = (uint)TPackageHandler.ConvertBytesToLongInteger(TPackageHandler.CutBytes(sFrame.strData, pointer + 2, 4)); if (data != uint.MaxValue) { try { data = Convert.ToUInt32(data.ToString("X8")); htData[ID] = data / 100u + "." + (data % 100u).ToString("d2"); } catch (Exception) { bDataValiad = false; } } pointer += 6; info2Display = info2Display + "\r\n\t\t\t\ta相电流:" + data.ToString("D8"); break; case 36386u: htData.Add(ID, ""); data = (uint)TPackageHandler.ConvertBytesToLongInteger(TPackageHandler.CutBytes(sFrame.strData, pointer + 2, 4)); if (data != uint.MaxValue) { try { data = Convert.ToUInt32(data.ToString("X8")); htData[ID] = data / 100u + "." + (data % 100u).ToString("d2"); } catch (Exception) { bDataValiad = false; } } pointer += 6; info2Display = info2Display + "\r\n\t\t\t\tb相电流:" + data.ToString("D8"); break; case 36387u: htData.Add(ID, ""); data = (uint)TPackageHandler.ConvertBytesToLongInteger(TPackageHandler.CutBytes(sFrame.strData, pointer + 2, 4)); if (data != uint.MaxValue) { try { data = Convert.ToUInt32(data.ToString("X8")); htData[ID] = data / 100u + "." + (data % 100u).ToString("d2"); } catch (Exception) { bDataValiad = false; } } pointer += 6; info2Display = info2Display + "\r\n\t\t\t\tc相电流:" + data.ToString("D8"); break; case 46672u: htData.Add(ID, ""); data = (uint)TPackageHandler.ConvertBytesToLongInteger(TPackageHandler.CutBytes(sFrame.strData, pointer + 2, 2)); if (data != 65535) { try { data = Convert.ToUInt32(data.ToString("X4")); htData[ID] = data / 1000u + "." + (data % 1000u).ToString("D3"); } catch (Exception) { bDataValiad = false; } } pointer += 4; info2Display = info2Display + "\r\n\t\t\t\t总功率因数:" + data.ToString("D4"); break; case 36880u: case 36890u: { htData.Add(36880u, ""); string readData = ""; try { data = (uint)TPackageHandler.ConvertBytesToLongInteger(TPackageHandler.CutBytes(sFrame.strData, pointer + 2, 4)); if (data != uint.MaxValue) { if (ID == 36880) { int PT = 0; int CT = 0; readData = calcYGZ(sFrame.tName, flagPoint, data, ref PT, ref CT); htData[36880u] = readData + " Kwh(一次侧), 抄见值=" + (data / 256u).ToString("x") + "." + (data % 256u).ToString("x2") + ", PT=" + PT + ", CT=" + CT; } else { readData = data.ToString(); htData[36880u] = readData + " Kwh(一次侧)"; } } } catch (Exception) { bDataValiad = false; } pointer += 6; info2Display = info2Display + "\r\n\t\t\t\t正向有功总电能:" + readData; break; } } } } res = bDataValiad; } if (res) { Common.logFile.write("数据解析:" + info2Display, LogFile.LOGLVL_DEBUG); } return res; } public bool S_ThresholdParamParsing(ref struFrame sFrame, ref string[] arrParam) { bool res = false; bool bDataValiad = true; if (sFrame.flagCtrlD == 1) { byte flagPoint = sFrame.strData[0]; if (flagPoint > 0) { int pointer = 1; while (pointer < sFrame.lenData) { uint ID = (uint)(sFrame.strData[pointer] * 256 + sFrame.strData[pointer + 1]); uint data = 0u; string sParam = ""; switch (ID) { case 35840u: case 35841u: case 35842u: case 35843u: data = (uint)TPackageHandler.ConvertBytesToLongInteger(TPackageHandler.CutBytes(sFrame.strData, pointer + 2, 2)); if (data == 65535) { bDataValiad = false; } else { try { data = Convert.ToUInt32(data.ToString("X4")); sParam = data / 1000u + "." + (data % 1000u).ToString("D3"); } catch (Exception) { bDataValiad = false; } } pointer += 4; if (bDataValiad) { switch (ID) { case 35840u: arrParam[0] = sParam; break; case 35841u: arrParam[1] = sParam; break; case 35842u: arrParam[2] = sParam; break; case 35843u: arrParam[3] = sParam; break; } } break; case 36096u: case 36097u: case 36098u: case 36099u: data = (uint)TPackageHandler.ConvertBytesToLongInteger(TPackageHandler.CutBytes(sFrame.strData, pointer + 2, 2)); if (data == 65535) { bDataValiad = false; } else { try { data = Convert.ToUInt32(data.ToString("X4")); sParam = data / 10u + "." + data % 10u; } catch (Exception) { bDataValiad = false; } } pointer += 4; if (bDataValiad) { switch (ID) { case 36096u: arrParam[4] = sParam; break; case 36097u: arrParam[5] = sParam; break; case 36098u: arrParam[6] = sParam; break; case 36099u: arrParam[7] = sParam; break; } } break; case 36352u: case 36353u: case 36354u: case 36355u: data = (uint)TPackageHandler.ConvertBytesToLongInteger(TPackageHandler.CutBytes(sFrame.strData, pointer + 2, 4)); if (data == uint.MaxValue) { bDataValiad = false; } else { try { data = Convert.ToUInt32(data.ToString("X8")); sParam = data / 10u + "." + data % 10u; } catch (Exception) { bDataValiad = false; } } pointer += 6; if (bDataValiad) { switch (ID) { case 36352u: arrParam[8] = sParam; break; case 36353u: arrParam[9] = sParam; break; case 36354u: arrParam[10] = sParam; break; case 36355u: arrParam[11] = sParam; break; } } break; case 36608u: case 36609u: case 36610u: case 36611u: data = (uint)TPackageHandler.ConvertBytesToLongInteger(TPackageHandler.CutBytes(sFrame.strData, pointer + 2, 4)); if (data == uint.MaxValue) { bDataValiad = false; } else { try { data = Convert.ToUInt32(data.ToString("X8")); sParam = data / 100u + "." + (data % 100u).ToString("D2"); } catch (Exception) { bDataValiad = false; } } pointer += 6; if (bDataValiad) { switch (ID) { case 36608u: arrParam[12] = sParam; break; case 36609u: arrParam[13] = sParam; break; case 36610u: arrParam[14] = sParam; break; case 36611u: arrParam[15] = sParam; break; } } break; } } } res = bDataValiad; } return res; } public bool E_ThresholdParamParsing(ref struFrame sFrame, ref string[] arrParam) { bool res = false; bool bDataValiad = true; if (sFrame.flagCtrlD == 1) { byte flagPoint = sFrame.strData[0]; if (flagPoint > 0) { int pointer = 1; while (pointer < sFrame.lenData) { uint ID = (uint)(sFrame.strData[pointer] * 256 + sFrame.strData[pointer + 1]); uint data = 0u; string sParam = ""; switch (ID) { case 55936u: case 55937u: case 55938u: case 55939u: data = (uint)TPackageHandler.ConvertBytesToLongInteger(TPackageHandler.CutBytes(sFrame.strData, pointer + 2, 3)); if (data == 16777215) { bDataValiad = false; } else { try { data = Convert.ToUInt32(data.ToString("X6")); sParam = data / 100u + "." + (data % 100u).ToString("D2"); } catch (Exception) { bDataValiad = false; } } pointer += 5; if (bDataValiad) { switch (ID) { case 55936u: arrParam[0] = sParam; break; case 55937u: arrParam[1] = sParam; break; case 55938u: arrParam[2] = sParam; break; case 55939u: arrParam[3] = sParam; break; } } break; case 55941u: case 55942u: case 55943u: case 55944u: data = (uint)TPackageHandler.ConvertBytesToLongInteger(TPackageHandler.CutBytes(sFrame.strData, pointer + 2, 3)); if (data == 16777215) { bDataValiad = false; } else { try { sParam = Convert.ToUInt32(data.ToString("X6")).ToString(); } catch (Exception) { bDataValiad = false; } } pointer += 5; if (bDataValiad) { switch (ID) { case 55941u: arrParam[4] = sParam; break; case 55942u: arrParam[5] = sParam; break; case 55943u: arrParam[6] = sParam; break; case 55944u: arrParam[7] = sParam; break; } } break; } } } res = bDataValiad; } return res; } public bool E_PTCTParamParsing(ref struFrame sFrame, ref string[] arrParam) { bool res = false; bool bDataValiad = true; if (sFrame.flagCtrlD == 1) { byte flagPoint = sFrame.strData[0]; if (flagPoint > 0) { int pointer = 1; while (pointer < sFrame.lenData) { uint ID = (uint)(sFrame.strData[pointer] * 256 + sFrame.strData[pointer + 1]); uint data = 0u; string sParam = ""; uint num = ID; uint num2 = num; if (num2 - 35089 > 1) { continue; } data = (uint)TPackageHandler.ConvertBytesToLongInteger(TPackageHandler.CutBytes(sFrame.strData, pointer + 2, 2)); if (data == 65535) { bDataValiad = false; } else { try { sParam = data.ToString(); } catch (Exception) { bDataValiad = false; } } pointer += 4; if (bDataValiad) { switch (ID) { case 35089u: arrParam[0] = sParam; break; case 35090u: arrParam[1] = sParam; break; } } } } res = bDataValiad; } return res; } public bool DataParsing(ref struFrame sFrame, ref Hashtable htData) { bool res = false; if (sFrame.flagDTN == 69) { res = E_DataParsing(ref sFrame, ref htData); } else if (sFrame.flagDTN == 83) { res = S_DataParsing(ref sFrame, ref htData); } return res; } public bool DataProceeding(ref struFrame sFrame, ref struFrame sRespFrame) { bool res = false; if (sFrame.flagCtrl == 131) { res = E_DataProceeding(ref sFrame, ref sRespFrame); } if (sFrame.flagCtrl == 132) { res = S_DataProceeding(ref sFrame, ref sRespFrame); } if (sFrame.flagCtrl == 133) { res = TH_DataProceeding(ref sFrame, ref sRespFrame); } if (sFrame.flagCtrl == 135) { res = ZD_DataProceeding(ref sFrame, ref sRespFrame); } if (sFrame.flagCtrl == 136) { res = HW_DataProceeding(ref sFrame, ref sRespFrame); } return res; } public bool AlarmDataProceeding(ref struFrame sFrame, ref struFrame sRespFrame) { bool res = false; if (sFrame.flagDTN == 69) { res = E_AlarmDataProceeding(ref sFrame, ref sRespFrame); } else if (sFrame.flagDTN == 83) { res = S_AlarmDataProceeding(ref sFrame, ref sRespFrame); } return res; } private bool S_AlarmDataProceeding(ref struFrame sFrame, ref struFrame sRespFrame) { bool res = true; string info2Display = ""; Hashtable htAlarmData = new Hashtable(); string sAlarmTime = ""; string sTID = ""; string sMID = ""; bool bDataValiad = true; if (sFrame.flagCtrlD == 1) { byte flagPoint = sFrame.strData[0]; if (flagPoint > 0) { info2Display = info2Display + "\r\n\t\t\t\t测量点编号:" + flagPoint.ToString("X2"); info2Display = info2Display + "\r\n\t\t\t\t告警数量:" + sFrame.strData[1]; sTID = sFrame.tName; sMID = flagPoint.ToString("x2"); int pointer = 2; while (res && pointer < sFrame.lenData) { uint ID = (uint)(sFrame.strData[pointer] * 256 + sFrame.strData[pointer + 1]); uint data = 0u; switch (ID) { case 32816u: { string sDateTime = "20" + sFrame.strData[pointer + 7].ToString("x2") + "-" + sFrame.strData[pointer + 6].ToString("x2") + "-" + sFrame.strData[pointer + 5].ToString("x2") + " " + sFrame.strData[pointer + 4].ToString("x2") + ":" + sFrame.strData[pointer + 3].ToString("x2") + ":" + sFrame.strData[pointer + 2].ToString("x2"); pointer += 8; info2Display = info2Display + "\r\n\t\t\t\t终端时间:" + sDateTime; sAlarmTime = sDateTime; break; } case 272u: case 273u: case 274u: case 275u: data = (uint)TPackageHandler.ConvertBytesToLongInteger(TPackageHandler.CutBytes(sFrame.strData, pointer + 2, 2)); if (data == 65535) { bDataValiad = false; } else { try { data = Convert.ToUInt32(data.ToString("X4")); htAlarmData.Add(ID.ToString("x4"), data / 1000u + "." + data % 1000u); } catch (Exception) { bDataValiad = false; } } pointer += 4; info2Display = info2Display + "\r\n\t\t\t\t告警代号:" + ID.ToString("X4") + " 蒸汽仪表压力值:" + data.ToString("D4"); break; case 288u: case 289u: case 290u: case 291u: data = (uint)TPackageHandler.ConvertBytesToLongInteger(TPackageHandler.CutBytes(sFrame.strData, pointer + 2, 2)); if (data == 65535) { bDataValiad = false; } else { try { data = Convert.ToUInt32(data.ToString("X4")); htAlarmData.Add(ID.ToString("x4"), data / 10u + "." + data % 10u); } catch (Exception) { bDataValiad = false; } } pointer += 4; info2Display = info2Display + "\r\n\t\t\t\t告警代号:" + ID.ToString("X4") + " 蒸汽仪表温度值:" + data.ToString("D4"); break; case 304u: case 305u: case 306u: case 307u: data = (uint)TPackageHandler.ConvertBytesToLongInteger(TPackageHandler.CutBytes(sFrame.strData, pointer + 2, 4)); if (data == uint.MaxValue) { bDataValiad = false; } else { try { data = Convert.ToUInt32(data.ToString("X8")); htAlarmData.Add(ID.ToString("x4"), data / 10u + "." + data % 10u); } catch (Exception) { bDataValiad = false; } } pointer += 6; info2Display = info2Display + "\r\n\t\t\t\t告警代号:" + ID.ToString("X4") + " 蒸汽仪表瞬时流量值:" + data.ToString("D8"); break; case 320u: case 321u: case 322u: case 323u: data = (uint)TPackageHandler.ConvertBytesToLongInteger(TPackageHandler.CutBytes(sFrame.strData, pointer + 2, 4)); if (data == uint.MaxValue) { bDataValiad = false; } else { try { data = Convert.ToUInt32(data.ToString("X8")); htAlarmData.Add(ID.ToString("x4"), data / 100u + "." + data % 100u); } catch (Exception) { bDataValiad = false; } } pointer += 6; info2Display = info2Display + "\r\n\t\t\t\t告警代号:" + ID.ToString("X4") + " 蒸汽仪表瞬时热量:" + data.ToString("D8"); break; default: res = false; break; } } if (res && bDataValiad) { SqlConnection connection = new SqlConnection(connectString); try { connection.Open(); foreach (DictionaryEntry de in htAlarmData) { string insertSQLString = string.Format(sS_InsertDataSQLMaskString, de.Key.ToString(), de.Value.ToString(), sAlarmTime, sTID, sMID); SqlCommand command = new SqlCommand(insertSQLString, connection); try { int i = command.ExecuteNonQuery(); } catch (SqlException e2) { Common.logErrFile.write("原SQL语句:" + insertSQLString + ",SQL语句错误:" + e2.ToString(), LogFile.LOGLVL_ERROR); } } } catch (Exception e) { Common.logErrFile.write("数据库打开错误:" + e.ToString(), LogFile.LOGLVL_ERROR); Common.logErrFile.write("数据库打开错误:详见err.txt", LogFile.LOGLVL_NORMAL); } } } } if (res) { sRespFrame.flagCtrlD = 0; sRespFrame.flagCtrlE = 0; sRespFrame.lenData = 0; sRespFrame.strData = new byte[0]; Common.logFile.write("告警数据解析:" + info2Display, LogFile.LOGLVL_DEBUG); } return res; } private bool E_AlarmDataProceeding(ref struFrame sFrame, ref struFrame sRespFrame) { bool res = true; string info2Display = ""; Hashtable htAlarmData = new Hashtable(); string sAlarmTime = ""; string sTID = ""; string sMID = ""; bool bDataValiad = true; if (sFrame.flagCtrlD == 1) { byte flagPoint = sFrame.strData[0]; if (flagPoint > 0) { info2Display = info2Display + "\r\n\t\t\t\t测量点编号:" + flagPoint.ToString("X2"); info2Display = info2Display + "\r\n\t\t\t\t告警数量:" + sFrame.strData[1]; sTID = sFrame.tName; sMID = flagPoint.ToString("x2"); int pointer = 2; while (res && pointer < sFrame.lenData) { uint ID = (uint)(sFrame.strData[pointer] * 256 + sFrame.strData[pointer + 1]); uint data = 0u; switch (ID) { case 32816u: { string sDateTime = "20" + sFrame.strData[pointer + 7].ToString("x2") + "-" + sFrame.strData[pointer + 6].ToString("x2") + "-" + sFrame.strData[pointer + 5].ToString("x2") + " " + sFrame.strData[pointer + 4].ToString("x2") + ":" + sFrame.strData[pointer + 3].ToString("x2") + ":" + sFrame.strData[pointer + 2].ToString("x2"); pointer += 8; info2Display = info2Display + "\r\n\t\t\t\t终端时间:" + sDateTime; sAlarmTime = sDateTime; break; } case 529u: case 530u: case 531u: case 532u: case 545u: case 546u: case 547u: case 548u: case 561u: case 562u: case 563u: case 564u: data = (uint)TPackageHandler.ConvertBytesToLongInteger(TPackageHandler.CutBytes(sFrame.strData, pointer + 2, 3)); if (data == 16777215) { bDataValiad = false; } else { try { data = Convert.ToUInt32(data.ToString("X6")); htAlarmData.Add(ID.ToString("x4"), data / 100u + "." + (data % 100u).ToString("D2")); } catch (Exception) { bDataValiad = false; } } pointer += 5; info2Display = info2Display + "\r\n\t\t\t\t告警代号:" + ID.ToString("X4") + " 电表电流值:" + data.ToString("D6"); break; case 577u: case 578u: case 579u: case 580u: case 593u: case 594u: case 595u: case 596u: case 609u: case 610u: case 611u: case 612u: data = (uint)TPackageHandler.ConvertBytesToLongInteger(TPackageHandler.CutBytes(sFrame.strData, pointer + 2, 3)); if (data == 16777215) { bDataValiad = false; } else { try { data = Convert.ToUInt32(data.ToString("X6")); htAlarmData.Add(ID.ToString("x4"), data.ToString()); } catch (Exception) { bDataValiad = false; } } pointer += 5; info2Display = info2Display + "\r\n\t\t\t\t告警代号:" + ID.ToString("X4") + " 电表电压值:" + data.ToString("D6"); break; default: res = false; break; } } if (res && bDataValiad) { struAlarmDataTrans alarmDataTrans = default(struAlarmDataTrans); alarmDataTrans.MID = Convert.ToInt32(sMID); alarmDataTrans.TID = sTID; alarmDataTrans.TTime = sAlarmTime; alarmDataTrans.objUserdata = htAlarmData; lstDataStore.Add(alarmDataTrans); nFlagDataStore = 256 + sFrame.flagDTN; } } } if (res) { sRespFrame.flagCtrlD = 0; sRespFrame.flagCtrlE = 0; sRespFrame.lenData = 0; sRespFrame.strData = new byte[0]; Common.logFile.write("告警数据解析:" + info2Display, LogFile.LOGLVL_DEBUG); } return res; } public bool E_saveAlarmThreshold(string sTName, string sMID, Hashtable htThreshold) { string sInsertSQLString = string.Format(sE_InsertSQLMaskString, htThreshold["da80"], htThreshold["da81"], htThreshold["da82"], htThreshold["da83"], htThreshold["da85"], htThreshold["da86"], htThreshold["da87"], htThreshold["da88"], sTName, sMID); string sUpdateSQLString = string.Format(sE_UpdateSQLMaskString, htThreshold["da80"], htThreshold["da81"], htThreshold["da82"], htThreshold["da83"], htThreshold["da85"], htThreshold["da86"], htThreshold["da87"], htThreshold["da88"], sTName, sMID); int i = 0; using (SqlConnection connection = new SqlConnection(connectString)) { try { connection.Open(); SqlCommand command = new SqlCommand(sUpdateSQLString, connection); try { i = command.ExecuteNonQuery(); if (i == 0) { command = new SqlCommand(sInsertSQLString, connection); try { i = command.ExecuteNonQuery(); } catch (SqlException e3) { Common.logErrFile.write("告警参数设置 SqlException:" + e3.ToString() + "\r\n 原SQL语句:" + sInsertSQLString, LogFile.LOGLVL_ERROR); } } } catch (SqlException e2) { Common.logErrFile.write("告警参数设置 SqlException:" + e2.ToString() + "\r\n 原SQL语句:" + sUpdateSQLString, LogFile.LOGLVL_ERROR); } } catch (Exception e) { Common.logErrFile.write("数据库打开错误:" + e.ToString(), LogFile.LOGLVL_ERROR); Common.logErrFile.write("数据库打开错误:详见err.txt", LogFile.LOGLVL_NORMAL); } } if (i == 0) { return false; } return true; } public bool saveAlarmThreshold(string sTName, string sMID, Hashtable htThreshold) { foreach (DictionaryEntry de in htThreshold) { string sInsertSQLString = string.Format(sS_InsertSQLMaskString, de.Key, de.Value, sTName, sMID); string sUpdateSQLString = string.Format(sS_UpdateSQLMaskString, de.Value, sTName, sMID, de.Key); int i = 0; using (SqlConnection connection = new SqlConnection(connectString)) { try { connection.Open(); SqlCommand command = new SqlCommand(sUpdateSQLString, connection); try { i = command.ExecuteNonQuery(); if (i == 0) { command = new SqlCommand(sInsertSQLString, connection); try { i = command.ExecuteNonQuery(); } catch (SqlException e3) { Common.logErrFile.write("告警参数设置 SqlException:" + e3.ToString() + "\r\n 原SQL语句:" + sInsertSQLString, LogFile.LOGLVL_ERROR); } } } catch (SqlException e2) { Common.logErrFile.write("告警参数设置 SqlException:" + e2.ToString() + "\r\n 原SQL语句:" + sUpdateSQLString, LogFile.LOGLVL_ERROR); } } catch (Exception e) { Common.logErrFile.write("数据库打开错误:" + e.ToString(), LogFile.LOGLVL_ERROR); Common.logErrFile.write("数据库打开错误:详见err.txt", LogFile.LOGLVL_NORMAL); } } if (i == 0) { return false; } } return true; } public void SaveLoginInfo(string sTName, DateTime dtLoginTime) { string sInsertMaskString = "INSERT INTO [dbo].[T_Sys_CollectDevice_Log]([CollectDeviceID] ,[Online] ,[Offline])VALUES('{0}','{1}',null)"; string sInsertString = string.Format(sInsertMaskString, sTName, dtLoginTime.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture)); SqlConnection connection = new SqlConnection(connectString); SqlCommand command = new SqlCommand(sInsertString, connection); try { connection.Open(); try { command.ExecuteNonQuery(); } catch (SqlException e2) { Common.logErrFile.write("原SQL语句:" + sInsertString + ",SQL语句错误:" + e2.ToString(), LogFile.LOGLVL_ERROR); } } catch (Exception e) { Common.logErrFile.write("数据库打开错误:" + e.ToString(), LogFile.LOGLVL_ERROR); Common.logErrFile.write("数据库打开错误:详见err.txt", LogFile.LOGLVL_NORMAL); } } public void SaveLogoutInfo(string sTName, DateTime dtLoginTime, DateTime dtLogoutTime, int nFlag) { string sOff; switch (nFlag) { case 0: sOff = "SET [Offline] = '{0}' "; break; case 1: sOff = "SET [Kickoff] = '{0}' "; break; case 2: sOff = "SET [Serviceoff] = '{0}' "; break; case 3: sOff = "SET [Replaceoff] = '{0}' "; break; default: sOff = "SET [Offline] = '{0}' "; break; } string sUpdateMaskString = "UPDATE [dbo].[T_Sys_CollectDevice_Log] " + sOff + "where [CollectDeviceID] = '{1}' and [Online] = '{2}'" + " " + " if not exists(select * from dbo.T_Sys_CollectDevice_Log where CollectDeviceID='{1}' and Online>'{2}')" + "begin update dbo.T_Monitor set OnLineStatus='0' where CollectDeviceID='{1}' end"; //姜志浩/2018-11-12改,更新终端状态,确认离线,离线是0 //判断语句是为了避免先上线后踢掉的同名实例。 string sUpdateString = string.Format(sUpdateMaskString, dtLogoutTime.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture), sTName, dtLoginTime.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture)); using (SqlConnection connection = new SqlConnection(connectString)) { SqlCommand command = new SqlCommand(sUpdateString, connection); try { connection.Open(); try { command.ExecuteNonQuery(); } catch (SqlException e) { Common.logErrFile.write("原SQL语句:" + sUpdateString + ",SQL语句错误:" + e.ToString(), LogFile.LOGLVL_ERROR); } } catch (Exception e) { Common.logErrFile.write("数据库打开错误:" + e.ToString(), LogFile.LOGLVL_ERROR); Common.logErrFile.write("数据库打开错误:详见err.txt", LogFile.LOGLVL_NORMAL); } } } public void SaveOffLineWarning(string sTName, int nOffLineHours) { string sInsertAlarmDataSQLMask = " INSERT INTO dbo.T_Sys_AlarmData ( MonitorID ,AlarmType ,AlarmValue ,AlarmTime)SELECT CollectDeviceID ,'{0}','{1}', '{2}' FROM dbo.T_CollectDeviceInfo WHERE CollectDeviceID='{3}'"; string sInsertAlarmDataSQL = string.Format(sInsertAlarmDataSQLMask, 1001, nOffLineHours, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture), sTName); SqlConnection connection = new SqlConnection(connectString); SqlCommand command = new SqlCommand(sInsertAlarmDataSQL, connection); try { connection.Open(); try { command.ExecuteNonQuery(); } catch (SqlException e2) { Common.logErrFile.write("原SQL语句:" + sInsertAlarmDataSQL + ",SQL语句错误:" + e2.ToString(), LogFile.LOGLVL_ERROR); } } catch (Exception e) { Common.logErrFile.write("数据库打开错误:" + e.ToString(), LogFile.LOGLVL_ERROR); Common.logErrFile.write("数据库打开错误:详见err.txt", LogFile.LOGLVL_NORMAL); } } private string calcMultiI(string sTID, int nMID, float unI, ref int PT, ref int CT) { string result = string.Empty; try { CT = 0; if (htEParams != null && htEParams.Count > 0) { E_MeterParams e_MeterParams = (E_MeterParams)htEParams[sTID + nMID.ToString("d2")]; if (e_MeterParams != null) { CT = e_MeterParams.nCT; result = (unI * (float)e_MeterParams.nCT).ToString(); if (result.Length > 5) { result = Math.Round(unI * (float)e_MeterParams.nCT, 2, MidpointRounding.AwayFromZero).ToString(); } } } } catch (Exception e1) { Common.logErrFile.write(e1.ToString(), LogFile.LOGLVL_ERROR); } return result; } private bool ZD_DataProceeding(ref struFrame sFrame, ref struFrame sRespFrame) { bool res = true; string info2Display = ""; bool bDataValiad = true; int sectionLength = 0; sectionLength = 28; if ((sFrame.lenData - 8) / sectionLength <= 0 || (sFrame.lenData - 8) % sectionLength != 0) { return false; } Common.logFile.write("[" + tcpHandler.tName + "] 主动上传数据域大小:" + sFrame.lenData, LogFile.LOGLVL_DEBUG); for (int i = 0; i < (sFrame.lenData - 8) / sectionLength; i++) { if (sFrame.flagCtrlD != 1) { continue; } byte flagPoint = sFrame.strData[i * sectionLength]; if (flagPoint <= 0) { continue; } int pointer = i * sectionLength; string MeterID_1 = "00" + Convert.ToInt32(sFrame.strData[pointer]); MeterID_1 = MeterID_1.Substring(MeterID_1.Length - 2, 2); info2Display = info2Display + "\r\n\t\t\t\t测量点编号:" + sFrame.tName + "_" + MeterID_1; int nParamCount = 5; TVibrationSensorData sAutoTrans = new TVibrationSensorData(); sAutoTrans.SensorId = sFrame.tName + "_00" + MeterID_1; while (res && pointer < (i + 1) * sectionLength) { uint ID = (uint)(sFrame.strData[pointer + 1] * 256 + sFrame.strData[pointer + 2]); uint data = 0u; switch (ID) { case 46080u: nParamCount--; pointer += 10; break; case 46081u: data = (uint)TPackageHandler.ConvertBytesToLongInteger(TPackageHandler.CutBytes(sFrame.strData, pointer + 2 + 1, 3)); if (data == 16777215) { bDataValiad = false; } else { try { string f_Press = data / 100u + "." + (data % 100u).ToString("d2"); sAutoTrans.Speed = decimal.Parse(f_Press); nParamCount--; } catch (Exception) { bDataValiad = false; } } pointer += 5; info2Display = info2Display + "\r\n\t\t\t\t传感器的速度值: " + sAutoTrans.Speed; break; case 46082u: data = (uint)TPackageHandler.ConvertBytesToLongInteger(TPackageHandler.CutBytes(sFrame.strData, pointer + 1 + 2, 2)); if (data == 65535) { bDataValiad = false; } else { try { decimal D_Press = decimal.Parse(data.ToString()); sAutoTrans.Displacement = D_Press; nParamCount--; } catch (Exception) { bDataValiad = false; } } pointer += 4; info2Display = info2Display + "\r\n\t\t\t\t传感器的位移值: " + sAutoTrans.Displacement; break; case 46083u: data = (uint)TPackageHandler.ConvertBytesToLongInteger(TPackageHandler.CutBytes(sFrame.strData, pointer + 1 + 2, 2)); if (data == 65535) { bDataValiad = false; } else { try { decimal D_Press2 = decimal.Parse(data.ToString()); if (D_Press2 > 2048m) { sAutoTrans.Acceleration = (2048m - D_Press2) / 100m; } else { sAutoTrans.Acceleration = D_Press2 / 100m; } nParamCount--; } catch (Exception) { bDataValiad = false; } } pointer += 4; info2Display = info2Display + "\r\n\t\t\t\t传感器的加速度值: " + sAutoTrans.Acceleration; break; case 46084u: data = (uint)TPackageHandler.ConvertBytesToLongInteger(TPackageHandler.CutBytes(sFrame.strData, pointer + 1 + 2, 2)); if (data == 65535) { bDataValiad = false; } else { try { decimal D_Press3 = decimal.Parse(data.ToString()); if (D_Press3 > 2048m) { sAutoTrans.Temperature = (2048m - D_Press3) / 10m; } else { sAutoTrans.Temperature = D_Press3 / 10m; } nParamCount--; } catch (Exception) { bDataValiad = false; } } pointer += 5; info2Display = info2Display + "\r\n\t\t\t\t传感器的温度值: " + sAutoTrans.Temperature; break; default: info2Display = info2Display + "\r\n***未识别传感器对象***:0x" + ID.ToString("X4"); res = false; break; } } if (res && bDataValiad && nParamCount == 0) { sAutoTrans.CollectTime = "'" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "'"; lstDataStore.Add(sAutoTrans); nFlagDataStore = sFrame.flagCtrl; } } Common.logFile.write("数据解析:" + info2Display, LogFile.LOGLVL_DEBUG); if (res) { sRespFrame.flagCtrlD = 0; sRespFrame.flagCtrlE = 0; sRespFrame.lenData = 0; sRespFrame.strData = new byte[0]; } return res; } private bool HW_DataProceeding(ref struFrame sFrame, ref struFrame sRespFrame) { bool res = true; string info2Display = ""; bool bDataValiad = true; int sectionLength = 0; sectionLength = 781; if (sFrame.lenData / sectionLength <= 0 || sFrame.lenData % sectionLength != 0) { return false; } for (int i = 0; i < sFrame.lenData / sectionLength; i++) { int ipoint = i * sectionLength; if (sFrame.flagCtrlD == 1) { TemperatureData temperatureData = new TemperatureData(); //sFrame.strData[774] = 10; string MeterID_1 = ""; //if (sFrame.strData[ipoint + 774] < 10) //{ // MeterID_1 = "00" + Convert.ToInt32(sFrame.strData[ipoint + 774]); //} //else //{ // MeterID_1 = "00" + sFrame.strData[ipoint + 774].ToString("X2"); //} MeterID_1 = "00" + Convert.ToInt32(sFrame.strData[ipoint + 774]); MeterID_1 = MeterID_1.Substring(MeterID_1.Length - 2, 2); temperatureData.monitorId = sFrame.tName + "_00" + MeterID_1; temperatureData.ProbeAddress = sFrame.strData[774].ToString(); temperatureData.MaxTemperature = decimal.Parse(sFrame.strData[768 + ipoint] + "." + sFrame.strData[769]); temperatureData.MinTemperature = decimal.Parse(sFrame.strData[770 + ipoint] + "." + sFrame.strData[771]); temperatureData.AvgTemperature = decimal.Parse(sFrame.strData[772 + ipoint] + "." + sFrame.strData[773]); //arrayTemperatureData.monitorId = sFrame.tName + "_00" + MeterID_1; //arrayTemperatureData.ProbeAddress = sFrame.strData[774].ToString(); //arrayTemperatureData.MaxTemperature = decimal.Parse(sFrame.strData[768 + ipoint] + "." + sFrame.strData[769]); //arrayTemperatureData.MinTemperature = decimal.Parse(sFrame.strData[770 + ipoint] + "." + sFrame.strData[771]); //arrayTemperatureData.AvgTemperature = decimal.Parse(sFrame.strData[772 + ipoint] + "." + sFrame.strData[773]); string strDateTime = "20" + sFrame.strData[ipoint + 780].ToString("x2") + "-" + sFrame.strData[ipoint + 779].ToString("x2") + "-" + sFrame.strData[ipoint + 778].ToString("x2") + " " + sFrame.strData[ipoint + 777].ToString("x2") + ":" + sFrame.strData[ipoint + 776].ToString("x2") + ":" + sFrame.strData[ipoint + 775].ToString("x2"); //arrayTemperatureData.CollectTime = "'" + strDateTime + "'"; temperatureData.CollectTime = DateTime.Parse(strDateTime); const int TotalSites = 768; for (int j = 0; j < TotalSites; j++) { // 用 TryParse 替代 Parse,防止格式错误导致程序崩溃 if (decimal.TryParse(sFrame.strData[ipoint + j].ToString(), out decimal value)) { temperatureData.Sites[j] = value; } else { // 转换失败默认赋值0,可根据业务修改 temperatureData.Sites[j] = 0m; } } lstDataStore.Add(temperatureData); nFlagDataStore = sFrame.flagCtrl; } } Common.logFile.write("[" + tcpHandler.tName + "] 主动上传数据域大小:" + sFrame.lenData, LogFile.LOGLVL_DEBUG); Common.logFile.write("数据解析:" + info2Display, LogFile.LOGLVL_DEBUG); if (res) { sRespFrame.flagCtrlD = 0; sRespFrame.flagCtrlE = 0; sRespFrame.lenData = 0; sRespFrame.strData = new byte[0]; } return res; } } }