private void xb_xbeePacketReceived(xbeePacket xbp) { parsePacket(xbp); packetCount++; foreach (PowerCollection pc in pcList) { if (pc.PacketCount >= intervalLength) interval(); } }
private xbeePacket getPacket() { xbeePacket xbp = new xbeePacket(); //addToLog("-- get packet started. buffer.count: " + buffer.Count); int i = 0; int[] packet = new int[88]; //addToLog("getting packet..."); try { while (i < 88) { //addToLog("i: " + i + " | buffer.count: " + buffer.Count); if (buffer.Count > 0) { packet[i] = (int)buffer.Dequeue(); if (i == 0 && packet[i] != 126) { //osae.AddToLog("bad first byte. i remains 0"); } else { i = i + 1; } } } //addToLog("packet found"); } catch (Exception ex) { //osae.AddToLog("error getting packet"); packet[0] = 0; } xbp.Address = (packet[4] * 256) + packet[5]; xbp.RSSI = packet[6]; xbp.Samples = packet[8]; for (int k = 3; k < 88; k++) { xbp.Checksum = xbp.Checksum + packet[k]; } int x = 0; for (int j = 11; j < 87; j = j + 4) { xbp.Voltage[x] = packet[j] * 256 + packet[j + 1]; xbp.Amp[x] = packet[j + 2] * 256 + packet[j + 3]; x = x + 1; } return(xbp); }
private void xb_xbeePacketReceived(xbeePacket xbp) { parsePacket(xbp); packetCount++; foreach (PowerCollection pc in pcList) { if (pc.PacketCount >= intervalLength) { interval(); } } }
private xbeePacket getPacket() { xbeePacket xbp = new xbeePacket(); //addToLog("-- get packet started. buffer.count: " + buffer.Count); int i = 0; int[] packet = new int[88]; //addToLog("getting packet..."); try { while (i < 88) { //addToLog("i: " + i + " | buffer.count: " + buffer.Count); if (buffer.Count > 0) { packet[i] = (int)buffer.Dequeue(); if (i == 0 && packet[i] != 126) { //osae.AddToLog("bad first byte. i remains 0"); } else i = i + 1; } } //addToLog("packet found"); } catch (Exception ex) { //osae.AddToLog("error getting packet"); packet[0] = 0; } xbp.Address = (packet[4] * 256) + packet[5]; xbp.RSSI = packet[6]; xbp.Samples = packet[8]; for (int k = 3; k < 88; k++) { xbp.Checksum = xbp.Checksum + packet[k]; } int x = 0; for (int j = 11; j < 87; j = j + 4) { xbp.Voltage[x] = packet[j] * 256 + packet[j + 1]; xbp.Amp[x] = packet[j + 2] * 256 + packet[j + 3]; x = x + 1; } return xbp; }
private void fetch() { xbeePacket xbp = new xbeePacket(); if (buffer.Count > 0) { xbp = getPacket(); if (xbp.Address > 0) { if (this.xbeePacketReceived != null) this.xbeePacketReceived(xbp); } } }
private void fetch() { xbeePacket xbp = new xbeePacket(); if (buffer.Count > 0) { xbp = getPacket(); if (xbp.Address > 0) { if (this.xbeePacketReceived != null) { this.xbeePacketReceived(xbp); } } } }
private void parsePacket(xbeePacket packet) { lock (thisLock) { try { Log.Debug("Received Packet: " + packet.Address); VREF = Int32.Parse(OSAEObjectPropertyManager.GetObjectPropertyValue(pName, "VREF").Value); double watts = 0; int max = 0, min = 1024, avgv, vpp; if (packet.Checksum % 256 == 255) { int x = 0; for (int i = 0; i < 19; i++) { if (packet.Voltage[i] < min) min = packet.Voltage[i]; if (packet.Voltage[i] > max) max = packet.Voltage[i]; } avgv = (min + max) / 2; vpp = max - min; Log.Debug(" avgv: " + avgv); for (int i = 0; i < 19; i++) { //osae.AddToLog("voltage[" + i + "]: " + packet.Voltage[i]); //osae.AddToLog("amp[" + i + "]: " + packet.Amp[i]); packet.Voltage[i] = (packet.Voltage[i] - avgv) * 340 / vpp; packet.Amp[i] = (packet.Amp[i] - VREF) / 17; watts = watts + (packet.Voltage[i] * packet.Amp[i]); } watts = Math.Round(watts / 19, 3); if (watts < 0) watts = 0; try { PowerCollection pc = GetPowerCollection(packet.Address); Log.Debug(" watts: " + watts.ToString()); pc.DataWattBuffer = pc.DataWattBuffer + watts; pc.PacketCount = pc.PacketCount + 1; pc.RSSI = packet.RSSI; Log.Debug(" RSSI: " + pc.RSSI.ToString()); } catch (Exception ex) { Log.Error(" error updating object statuses!", ex); } } else Log.Debug(" bad checksum"); } catch (Exception ex) { Log.Error("- Error parsing packet!", ex); } } }
private void parsePacket(xbeePacket packet) { lock (thisLock) { try { Log.Debug("Received Packet: " + packet.Address); VREF = Int32.Parse(OSAEObjectPropertyManager.GetObjectPropertyValue(pName, "VREF").Value); double watts = 0; int max = 0, min = 1024, avgv, vpp; if (packet.Checksum % 256 == 255) { int x = 0; for (int i = 0; i < 19; i++) { if (packet.Voltage[i] < min) { min = packet.Voltage[i]; } if (packet.Voltage[i] > max) { max = packet.Voltage[i]; } } avgv = (min + max) / 2; vpp = max - min; Log.Debug(" avgv: " + avgv); for (int i = 0; i < 19; i++) { //osae.AddToLog("voltage[" + i + "]: " + packet.Voltage[i]); //osae.AddToLog("amp[" + i + "]: " + packet.Amp[i]); packet.Voltage[i] = (packet.Voltage[i] - avgv) * 340 / vpp; packet.Amp[i] = (packet.Amp[i] - VREF) / 17; watts = watts + (packet.Voltage[i] * packet.Amp[i]); } watts = Math.Round(watts / 19, 3); if (watts < 0) { watts = 0; } try { PowerCollection pc = GetPowerCollection(packet.Address); Log.Debug(" watts: " + watts.ToString()); pc.DataWattBuffer = pc.DataWattBuffer + watts; pc.PacketCount = pc.PacketCount + 1; pc.RSSI = packet.RSSI; Log.Debug(" RSSI: " + pc.RSSI.ToString()); } catch (Exception ex) { Log.Error(" error updating object statuses!", ex); } } else { Log.Debug(" bad checksum"); } } catch (Exception ex) { Log.Error("- Error parsing packet!", ex); } } }