//string strss = ""; void StartHandleData(string[] msg) { if (msg[0] == null) { return; } if (msg.Length == 6 || msg.Length == 7) { if (zoomId != "" && zoomId != "0") { NormalData(msg); } } else if (msg.Length == 8) { if (msg[0] == "FF" && msg[1] == "0C" && msg[2] == "02") { string zid = Hexadecimal.ConvertBase(msg[3], 16, 10); if (zid != "0" && !string.IsNullOrWhiteSpace(zid)) { if (int.Parse(oldId) < zCount * dNo && int.Parse(oldId) >= int.Parse(zid)) { ExceptionData(); } } zoomId = Hexadecimal.ConvertBase(msg[3], 16, 10); } } }
/// <summary> /// 正常数据 /// </summary> void NormalData(string[] msg) { //if (zoomId == oldId) // return; string statusNo = Hexadecimal.ConvertBase(msg[0], 16, 10); if (string.IsNullOrWhiteSpace(statusNo)) { return; } string status = Enum.GetName(typeof(H3CMsgType), int.Parse(statusNo)); if (string.IsNullOrWhiteSpace(status)) { return; } oldId = zoomId; string dianya = Hexadecimal.ConvertBase(msg[1] + msg[2], 16, 10); StringHelper.RepairZero(dianya, 4); string dianliu = (double.Parse(Hexadecimal.ConvertBase(msg[3], 16, 10)) / 10).ToString("#0.0"); if (dianliu == "0.0" && dianya == "0" && status == "正常") { //checkTime = DateTime.Now; // return; status = "异常"; statusNo = "7"; } H3CPowerGridData data = new H3CPowerGridData() { DeviceIp = powerIp, EventType = (H3CMsgType)Enum.Parse(typeof(H3CMsgType), statusNo), PowerFlow = dianliu, PowerGridState = int.Parse(statusNo), Voltage = dianya, ZoomId = int.Parse(zoomId) }; SendZoomData(data); }
/// <summary> /// 电网数据解析 /// </summary> /// <param name="sender">数据</param> private void MyEvent(object sender) { try { XFPowerGridData pg = new XFPowerGridData(); byte[] date = sender as byte[]; string strMsg = Encoding.Default.GetString(date, 0, date.Length);// 将接受到的字节数据转化成字符串; strMsg = strMsg.Trim('\0'); int x = 0; string w = ""; foreach (char item in strMsg) { if (x == 1) { w += item + " "; x = 0; } else { w += item; x++; } } w = w.Substring(0, w.Length - " ".Length); string[] msg = w.Split(' '); if (msg.Length == 18) { if (msg[0] == "3A" && msg[16] == "0A") { pg.LowerComputerId = int.Parse(Hexadecimal.ConvertBase(msg[1], 16, 10)); pg.IsClearAlarm = int.Parse(Hexadecimal.ConvertBase(msg[14], 16, 10)); pg.LowerComputerIsPowerOn = int.Parse(Hexadecimal.ConvertBase(msg[15], 16, 10)); pg.ExtensionComputerId = int.Parse(Hexadecimal.ConvertBase(msg[17], 16, 10)); pg.PowerGridState = int.Parse(Hexadecimal.ConvertBase(msg[2], 16, 10)); if (pg.ExtensionComputerId == 0 && pg.PowerGridState == 0) { return; } pg.CombatPower = int.Parse(Hexadecimal.ConvertBase(msg[3], 16, 10)); pg.Triggerthreshold = int.Parse(Hexadecimal.ConvertBase(msg[4], 16, 10)); pg.ExtensionState = int.Parse(Hexadecimal.ConvertBase(msg[5], 16, 10)); ////下位机状态 //if (pg.PowerGridState == 0) state = pg.ExtensionState; //else if (pg.PowerGridState == 4 || pg.PowerGridState == 1 || pg.PowerGridState == 3) //主机状态 // state = pg.ExtensionState = 5; pg.PowerFlow = int.Parse(Hexadecimal.ConvertBase(msg[8], 16, 10)) * 256 + int.Parse(Hexadecimal.ConvertBase(msg[9], 16, 10)); if (pg.ExtensionComputerId == 0 || (pg.PowerFlow == 0 && pg.Voltage == 0)) { return; } if (int.Parse(Hexadecimal.ConvertBase(msg[10], 16, 10)) * 256 + int.Parse(Hexadecimal.ConvertBase(msg[11], 16, 10)) == 0) { pg.Voltage = (int.Parse(Hexadecimal.ConvertBase(msg[6], 16, 10)) * 256 + int.Parse(Hexadecimal.ConvertBase(msg[7], 16, 10))) % 1000 + 4700; } else { pg.Voltage = (int.Parse(Hexadecimal.ConvertBase(msg[6], 16, 10)) * 256 + int.Parse(Hexadecimal.ConvertBase(msg[7], 16, 10))) % 1000 + 10000; } pg.DeviceIp = powerIp; pg.EventType = (XFPowerState)Enum.Parse(typeof(XFPowerState), state.ToString()); XFZoomStatus?.Invoke(pg); checkTime = DateTime.Now; isSendNetState = false; } } else { return; } string status = Enum.GetName(typeof(XFPowerState), state); w = string.Format("{0}:电压{1} {4} 电流{2} {5} 状态 {3};", pg.ExtensionComputerId, pg.Voltage, pg.PowerFlow, status, "V", "mA"); ShowMsg(w); if (pg.ExtensionComputerId <= ZoomCount) { allZoomData.Add(pg); } stateCount += state; if (pg.ExtensionComputerId == ZoomCount) { if (allZoomData.Count == ZoomCount) { XFPowerGridFormatStatus?.Invoke((XFPowerState)Enum.Parse(typeof(XFPowerState), stateCount == 0 ? "0" : "7"), allZoomData); } allZoomData.Clear(); } // checkTime = DateTime.Now; } catch (Exception) { } }
private void MyEvent(object sender) { try { if (sender == null) { return; } byte[] date = sender as byte[]; string strMsg = Encoding.Default.GetString(date, 0, date.Length);// 将接受到的字节数据转化成字符串; strMsg = ByteHelper.ByteToHexStr(date, date.Length); strMsg = strMsg.Trim('\0'); int x = 0; string w = ""; foreach (char item in strMsg) { if (x == 1) { w += item + " "; x = 0; } else { w += item; x++; } } w = w.Substring(0, w.Length - " ".Length); string[] msg = w.Split(' '); if (msg.Length == 8) { if (msg[0] == "FF" && msg[1] == "0C" && msg[2] == "02") { id = Hexadecimal.ConvertBase(msg[3], 16, 10); } } else if (msg.Length == 6) { if (id != "" && id != "0") { if (id == oldId) { return; } oldId = id; string statusNo = Hexadecimal.ConvertBase(msg[0], 16, 10); string status = Enum.GetName(typeof(H3CMsgType), int.Parse(statusNo)); string dianya = Hexadecimal.ConvertBase(msg[1] + msg[2], 16, 10); if (dianya.Length < 4) { int count = dianya.Length; for (int i = 0; i < 4 - count; i++) { dianya = "0" + dianya; } } string dianliu = (double.Parse(Hexadecimal.ConvertBase(msg[3], 16, 10)) / 10).ToString("#0.0"); H3CPowerGridData data = new H3CPowerGridData(); data.DeviceIp = powerIp; data.EventType = (H3CMsgType)Enum.Parse(typeof(H3CMsgType), statusNo); data.PowerFlow = dianliu; data.PowerGridState = int.Parse(statusNo); data.Voltage = dianya; data.ZoomId = int.Parse(id); if (dianliu == "0" && dianya == "0" && status == "正常") { return; } H3CZoomStatus?.Invoke(data); dianliu += " "; dianya += " "; ShowMsg(string.Format("{0}:电压{1} V 电流{2} A 状态 {3}", id, dianya, dianliu, status)); if (id == "") { return; } stateCount += int.Parse(statusNo); if (int.Parse(id) <= ZoomCount) { txt += string.Format("{0}:电压{1}电流{2}状态 {3};", id, dianya, dianliu, status); allZoomData.Add(data); } if (id == ZoomCount.ToString()) { if (allZoomData.Count == ZoomCount) { H3CPowerGridFormatStatus?.Invoke((H3CMsgType)Enum.Parse(typeof(H3CMsgType), stateCount == 0 ? "0" : "7"), allZoomData); } txt = " "; allZoomData.Clear(); //Thread.Sleep(3000); } id = ""; } } } catch (Exception) { } checkTime = DateTime.Now; }