/// <summary> /// Serial Data Received Event Handler /// </summary> /// <param name="SerialData"> Array of Received Serial byte values </param> void _SerialInstance_SerialDataReceived(byte[] SerialData) { try { DisableTimer(); if (SerialData.Length >= 1) { switch (CurrentState) { case Library.PacketStates.PacketState.CLEAR_EEPROM: { //SerialCloseDisposeCollect(); TransactionStatusClass cCompleted = new TransactionStatusClass(); //Should only receive 2 bytes CR & LF if ((SerialData.Length == 2) && (SerialData.Contains((byte)0x0d) && SerialData.Contains((byte)0x0a))) { cCompleted.TransactionStatus = TransactionStatusClass.TransactionValues.SUCCESS; } else { cCompleted.TransactionStatus = TransactionStatusClass.TransactionValues.FAILED; } //Activate Delegate Event if (TempSensor_ByteArray_Received != null) { TempSensor_ByteArray_Received(SerialData); } if (Clear_EEPROM_Response_REceived != null) { Clear_EEPROM_Response_REceived(cCompleted); } break; } case Library.PacketStates.PacketState.DOWNLOAD_EEPROM: { //Data Class Instantiation TransactionStatusClass cCompleted = new TransactionStatusClass(); EEPROMSerialDataClass Data = new EEPROMSerialDataClass(); int yy; int MM; int dd; int HH; int mm; string syy; string syyyy; string sMM; string sdd; string sHH; string smm; //Start Decoding - Check that array contains LineFeed and Carriage Returm if (SerialData.Contains((byte)0x0d) && SerialData.Contains((byte)0x0a)) { try { //Remove Carriage Returm and Line Feed byte[] newArray = SerialData.Where(b => (b != 0x0d) & (b != 0x0a)).ToArray(); if (newArray.Length > 2) { //Should do a mod Test to ensure that the right number of bytes are here. Stream stream = new MemoryStream(newArray); BinaryReader reader = new BinaryReader(stream); BigEndianReader BigEndianReader = new BigEndianReader(reader); //UInt16 mask = 0x03FF; //UInt16 BatteryVoltage = BigEndianReader.ReadUInt16(); //Data.ControllerBatteryVoltage = (UInt16)(BatteryVoltage & mask); //Data.ConvertedBatteryVoltage = Convert.ToDecimal(Data.ControllerBatteryVoltage) * 3.3M * 3 / 1023; byte seperationbyte; while (BigEndianReader.BaseStream.Position != BigEndianReader.BaseStream.Length) { char[] cMAC; byte[] cTemp = new byte[2]; string sDate = String.Empty; //cMAC = Encoding.UTF8.GetChars(BigEndianReader.ReadBytes(6)); string BaseStationMacAddress = BitConverter.ToString(BigEndianReader.ReadBytes(3)).Replace("-", string.Empty); //Sensor Mac Address string sMac = BitConverter.ToString(BigEndianReader.ReadBytes(3)).Replace("-", string.Empty); cMAC = sMac.ToCharArray(); Data.BaseMAC.Add(cMAC); // READ BATTERY VOLTAGE UInt16 mask = 0x03FF; UInt16 BatteryVoltage = BigEndianReader.ReadUInt16(); Data.TempBatteryVoltage.Add((UInt16)(BatteryVoltage & mask)); //READ SENSOR TEMPERATURE cTemp = BigEndianReader.ReadBytes(4); float myFloatTemp = BitConverter.ToSingle(cTemp, 0); Data.TempSensorValue.Add(cTemp); Data.ConvertedTemp.Add((decimal)(myFloatTemp)); //READ TIME DATE BYTES BEFORE DOING CONVERSION sDate = Encoding.UTF8.GetString(BigEndianReader.ReadBytes(10)); Data.DeviceDateTime.Add(sDate); //READ END OF TEMP DATA SEPERATOR seperationbyte = BigEndianReader.ReadByte(); //CONVERT TIME DATE TO APPROPRIATE FORMAT Data.yy.Add(sDate.ToCharArray(0, 2)); Data.MM.Add(sDate.ToCharArray(2, 2)); Data.dd.Add(sDate.ToCharArray(4, 2)); Data.HH.Add(sDate.ToCharArray(6, 2)); Data.mm.Add(sDate.ToCharArray(8, 2)); syy = new string(sDate.ToCharArray(0, 2)); syyyy = DateTime.Now.ToString("yyyy"); sMM = new string(sDate.ToCharArray(2, 2)); sdd = new string(sDate.ToCharArray(0, 2)); sHH = new string(sDate.ToCharArray(6, 2)); smm = new string(sDate.ToCharArray(8, 2)); yy = int.Parse(syyyy.Remove(2, 2) + syy); MM = int.Parse(sMM); dd = int.Parse(sdd); HH = int.Parse(sHH); mm = int.Parse(smm); if (MM < 1) { MM = 1; } if (dd < 1) { dd = 1; } //DateTime test = DateTime.Now.ToString("yyyy"); DateTime ConvertedTime = new DateTime(yy, MM, dd, HH, mm, 0); Data.ConvertedDateTime.Add(ConvertedTime); } //if ((Data.DeviceTemp.Count == Data.DeviceID.Count) && (Data.DeviceDateTime.Count == Data.DeviceID.Count)) if ((Data.TempSensorValue.Count == Data.BaseMAC.Count) && (Data.DeviceDateTime.Count == Data.BaseMAC.Count)) { cCompleted.TransactionStatus = TransactionStatusClass.TransactionValues.SUCCESS; System.Diagnostics.Debug.WriteLine("Class: CommsDecoder, Line 248: Success - Received Download EEPROM Event"); // Close Com Port //_SerialInstance.WriteData(Encoding.UTF8.GetBytes("!A")); ////////////////////////////////////// System.Diagnostics.Debug.WriteLine("Class: CommsDecoder, Line 256: Success - Send EOT"); } else { cCompleted.TransactionStatus = TransactionStatusClass.TransactionValues.FAILED; System.Diagnostics.Debug.WriteLine("Class: CommsDecoder, Line 258: Failed Count not equal"); } } else { cCompleted.TransactionStatus = TransactionStatusClass.TransactionValues.SUCCESS; System.Diagnostics.Debug.WriteLine("Class: CommsDecoder, Line 263: Success But Download EEPROM Packet <2"); } } catch (Exception Ex) { // Catch decode Exception - this will be triggered if binary reader gets to the end of the stream before // identifying the CRLF cCompleted.TransactionStatus = TransactionStatusClass.TransactionValues.FAILED; System.Diagnostics.Debug.WriteLine(Ex.ToString()); } } else { cCompleted.TransactionStatus = TransactionStatusClass.TransactionValues.FAILED; System.Diagnostics.Debug.WriteLine("Class: CommsDecoder, Line 270: Download EEPROM Failed - No CR + LF"); } //CloseDisposeCollect(); System.Diagnostics.Debug.WriteLine("Class: CommsDecoder, Line 175: Send GC Collect"); //Activate Delegate Event if (TempSensor_ByteArray_Received != null) { TempSensor_ByteArray_Received(SerialData); } if (EEPROM_Data_Received != null) { EEPROM_Data_Received(Data, cCompleted); } break; } case Library.PacketStates.PacketState.READ_RTC: { // Close Com Port // SerialCloseDisposeCollect(); TransactionStatusClass cCompleted = new TransactionStatusClass(); Library.RTCSerialDataClass sDate = new RTCSerialDataClass(); //Start Decoding - Check that array contains LineFeed and Carriage Returm //if (SerialData.Contains((byte)13) && SerialData.Contains((byte)10)) if (SerialData.Contains((byte)0x0d) && SerialData.Contains((byte)0x0a)) { //Remove Carriage Returm and Line Feed byte[] newArray = SerialData.Where(b => (b != 0x0d) & (b != 0x0a)).ToArray(); // Format must be 12 characters else incorrect if (newArray.Length == 12) { Stream stream = new MemoryStream(newArray); BinaryReader reader = new BinaryReader(stream); sDate.yy = reader.ReadChars(2); sDate.MM = reader.ReadChars(2); sDate.dd = reader.ReadChars(2); sDate.dow = reader.ReadChars(2); //sDate.ww = reader.ReadChars(2); sDate.HH = reader.ReadChars(2); sDate.mm = reader.ReadChars(2); cCompleted.TransactionStatus = TransactionStatusClass.TransactionValues.SUCCESS; } else { sDate = null; cCompleted.TransactionStatus = TransactionStatusClass.TransactionValues.FAILED; } //Activate Delegate Event if (TempSensor_ByteArray_Received != null) { TempSensor_ByteArray_Received(newArray); } //Activate RTC Data Event if (RTC_Data_Received != null) { RTC_Data_Received(sDate, cCompleted); } } else { //Do what when it does not contain CR & LF sDate = null; cCompleted.TransactionStatus = TransactionStatusClass.TransactionValues.FAILED; // Close Com Port //_SerialInstance.ClosePort(); //Send Data that was not correct to front end for testing if (TempSensor_ByteArray_Received != null) { TempSensor_ByteArray_Received(SerialData); } //Activate RTC Data Event if (RTC_Data_Received != null) { RTC_Data_Received(sDate, cCompleted); } } break; } case Library.PacketStates.PacketState.WRITE_RTC: { //CloseDisposeCollect(); TransactionStatusClass cCompleted = new TransactionStatusClass(); //Should only receive 2 bytes CR & LF if ((SerialData.Length == 2) && (SerialData.Contains((byte)0x0d) && SerialData.Contains((byte)0x0a))) { cCompleted.TransactionStatus = TransactionStatusClass.TransactionValues.SUCCESS; } else { cCompleted.TransactionStatus = TransactionStatusClass.TransactionValues.FAILED; } //Activate Delegate Event if (TempSensor_ByteArray_Received != null) { TempSensor_ByteArray_Received(SerialData); } if (RTC_Write_Response_Received != null) { RTC_Write_Response_Received(cCompleted); } break; } default: { //_SerialInstance.ClosePort(); break; } } } // Else If DataLength <=0 // No data was received else { //_SerialInstance.ClosePort(); } } catch (Exception Ex) { System.Diagnostics.Debug.WriteLine(Ex); throw; } }
/// <summary> /// Serial Data Received Event Handler /// </summary> /// <param name="SerialData"> Array of Received Serial byte values </param> void _SerialInstance_SerialDataReceived(byte[] SerialData) { try { DisableTimer(); if (SerialData.Length >= 1) { switch (CurrentState) { case Library.PacketStates.PacketState.CLEAR_EEPROM: { //SerialCloseDisposeCollect(); TransactionStatusClass cCompleted = new TransactionStatusClass(); //Should only receive 2 bytes CR & LF if ((SerialData.Length == 2) && (SerialData.Contains((byte)0x0d) && SerialData.Contains((byte)0x0a))) { cCompleted.TransactionStatus = TransactionStatusClass.TransactionValues.SUCCESS; } else { cCompleted.TransactionStatus = TransactionStatusClass.TransactionValues.FAILED; } //Activate Delegate Event if (TempSensor_ByteArray_Received != null) { TempSensor_ByteArray_Received(SerialData); } if (Clear_EEPROM_Response_REceived != null) { Clear_EEPROM_Response_REceived(cCompleted); } break; } case Library.PacketStates.PacketState.DOWNLOAD_EEPROM: { //Data Class Instantiation TransactionStatusClass cCompleted = new TransactionStatusClass(); EEPROMSerialDataClass Data = new EEPROMSerialDataClass(); int yy; int MM; int dd; int HH; int mm; string syy; string syyyy; string sMM; string sdd; string sHH; string smm; //Start Decoding - Check that array contains LineFeed and Carriage Returm if (SerialData.Contains((byte)0x0d) && SerialData.Contains((byte)0x0a)) { try { //Remove Carriage Returm and Line Feed byte[] newArray = SerialData.Where(b => (b != 0x0d)&(b != 0x0a)).ToArray(); if (newArray.Length > 2) { //Should do a mod Test to ensure that the right number of bytes are here. Stream stream = new MemoryStream(newArray); BinaryReader reader = new BinaryReader(stream); BigEndianReader BigEndianReader = new BigEndianReader(reader); //UInt16 mask = 0x03FF; //UInt16 BatteryVoltage = BigEndianReader.ReadUInt16(); //Data.ControllerBatteryVoltage = (UInt16)(BatteryVoltage & mask); //Data.ConvertedBatteryVoltage = Convert.ToDecimal(Data.ControllerBatteryVoltage) * 3.3M * 3 / 1023; byte seperationbyte; while (BigEndianReader.BaseStream.Position != BigEndianReader.BaseStream.Length) { char[] cMAC; byte[] cTemp = new byte[2]; string sDate = String.Empty; //cMAC = Encoding.UTF8.GetChars(BigEndianReader.ReadBytes(6)); string BaseStationMacAddress = BitConverter.ToString(BigEndianReader.ReadBytes(3)).Replace("-", string.Empty); //Sensor Mac Address string sMac = BitConverter.ToString(BigEndianReader.ReadBytes(3)).Replace("-", string.Empty); cMAC = sMac.ToCharArray(); Data.BaseMAC.Add(cMAC); // READ BATTERY VOLTAGE UInt16 mask = 0x03FF; UInt16 BatteryVoltage = BigEndianReader.ReadUInt16(); Data.TempBatteryVoltage.Add((UInt16)(BatteryVoltage & mask)); //READ SENSOR TEMPERATURE cTemp = BigEndianReader.ReadBytes(4); float myFloatTemp = BitConverter.ToSingle(cTemp, 0); Data.TempSensorValue.Add(cTemp); Data.ConvertedTemp.Add((decimal)(myFloatTemp)); //READ TIME DATE BYTES BEFORE DOING CONVERSION sDate = Encoding.UTF8.GetString(BigEndianReader.ReadBytes(10)); Data.DeviceDateTime.Add(sDate); //READ END OF TEMP DATA SEPERATOR seperationbyte = BigEndianReader.ReadByte(); //CONVERT TIME DATE TO APPROPRIATE FORMAT Data.yy.Add(sDate.ToCharArray(0, 2)); Data.MM.Add(sDate.ToCharArray(2, 2)); Data.dd.Add(sDate.ToCharArray(4, 2)); Data.HH.Add(sDate.ToCharArray(6, 2)); Data.mm.Add(sDate.ToCharArray(8, 2)); syy = new string(sDate.ToCharArray(0, 2)); syyyy = DateTime.Now.ToString("yyyy"); sMM = new string(sDate.ToCharArray(2, 2)); sdd = new string(sDate.ToCharArray(0, 2)); sHH = new string(sDate.ToCharArray(6, 2)); smm = new string(sDate.ToCharArray(8, 2)); yy = int.Parse(syyyy.Remove(2, 2) + syy); MM = int.Parse(sMM); dd = int.Parse(sdd); HH = int.Parse(sHH); mm = int.Parse(smm); if (MM < 1) {MM = 1;} if (dd < 1) {dd = 1;} //DateTime test = DateTime.Now.ToString("yyyy"); DateTime ConvertedTime = new DateTime(yy, MM, dd, HH, mm, 0); Data.ConvertedDateTime.Add(ConvertedTime); } //if ((Data.DeviceTemp.Count == Data.DeviceID.Count) && (Data.DeviceDateTime.Count == Data.DeviceID.Count)) if ((Data.TempSensorValue.Count == Data.BaseMAC.Count) && (Data.DeviceDateTime.Count == Data.BaseMAC.Count)) { cCompleted.TransactionStatus = TransactionStatusClass.TransactionValues.SUCCESS; System.Diagnostics.Debug.WriteLine("Class: CommsDecoder, Line 248: Success - Received Download EEPROM Event"); // Close Com Port //_SerialInstance.WriteData(Encoding.UTF8.GetBytes("!A")); ////////////////////////////////////// System.Diagnostics.Debug.WriteLine("Class: CommsDecoder, Line 256: Success - Send EOT"); } else { cCompleted.TransactionStatus = TransactionStatusClass.TransactionValues.FAILED; System.Diagnostics.Debug.WriteLine("Class: CommsDecoder, Line 258: Failed Count not equal"); } } else { cCompleted.TransactionStatus = TransactionStatusClass.TransactionValues.SUCCESS; System.Diagnostics.Debug.WriteLine("Class: CommsDecoder, Line 263: Success But Download EEPROM Packet <2"); } } catch (Exception Ex) { // Catch decode Exception - this will be triggered if binary reader gets to the end of the stream before // identifying the CRLF cCompleted.TransactionStatus = TransactionStatusClass.TransactionValues.FAILED; System.Diagnostics.Debug.WriteLine(Ex.ToString()); } } else { cCompleted.TransactionStatus = TransactionStatusClass.TransactionValues.FAILED; System.Diagnostics.Debug.WriteLine("Class: CommsDecoder, Line 270: Download EEPROM Failed - No CR + LF"); } //CloseDisposeCollect(); System.Diagnostics.Debug.WriteLine("Class: CommsDecoder, Line 175: Send GC Collect"); //Activate Delegate Event if (TempSensor_ByteArray_Received != null) { TempSensor_ByteArray_Received(SerialData); } if (EEPROM_Data_Received != null) { EEPROM_Data_Received(Data,cCompleted); } break; } case Library.PacketStates.PacketState.READ_RTC: { // Close Com Port // SerialCloseDisposeCollect(); TransactionStatusClass cCompleted = new TransactionStatusClass(); Library.RTCSerialDataClass sDate = new RTCSerialDataClass(); //Start Decoding - Check that array contains LineFeed and Carriage Returm //if (SerialData.Contains((byte)13) && SerialData.Contains((byte)10)) if (SerialData.Contains((byte)0x0d) && SerialData.Contains((byte)0x0a)) { //Remove Carriage Returm and Line Feed byte[] newArray = SerialData.Where(b => (b != 0x0d) & (b != 0x0a)).ToArray(); // Format must be 12 characters else incorrect if (newArray.Length == 12) { Stream stream = new MemoryStream(newArray); BinaryReader reader = new BinaryReader(stream); sDate.yy = reader.ReadChars(2); sDate.MM = reader.ReadChars(2); sDate.dd = reader.ReadChars(2); sDate.dow = reader.ReadChars(2); //sDate.ww = reader.ReadChars(2); sDate.HH = reader.ReadChars(2); sDate.mm = reader.ReadChars(2); cCompleted.TransactionStatus = TransactionStatusClass.TransactionValues.SUCCESS; } else { sDate = null; cCompleted.TransactionStatus = TransactionStatusClass.TransactionValues.FAILED; } //Activate Delegate Event if (TempSensor_ByteArray_Received != null) { TempSensor_ByteArray_Received(newArray); } //Activate RTC Data Event if (RTC_Data_Received != null) { RTC_Data_Received(sDate, cCompleted); } } else { //Do what when it does not contain CR & LF sDate = null; cCompleted.TransactionStatus = TransactionStatusClass.TransactionValues.FAILED; // Close Com Port //_SerialInstance.ClosePort(); //Send Data that was not correct to front end for testing if (TempSensor_ByteArray_Received != null) { TempSensor_ByteArray_Received(SerialData); } //Activate RTC Data Event if (RTC_Data_Received != null) { RTC_Data_Received(sDate, cCompleted); } } break; } case Library.PacketStates.PacketState.WRITE_RTC: { //CloseDisposeCollect(); TransactionStatusClass cCompleted = new TransactionStatusClass(); //Should only receive 2 bytes CR & LF if ((SerialData.Length == 2) && (SerialData.Contains((byte)0x0d) && SerialData.Contains((byte)0x0a)) ) { cCompleted.TransactionStatus = TransactionStatusClass.TransactionValues.SUCCESS; } else { cCompleted.TransactionStatus = TransactionStatusClass.TransactionValues.FAILED; } //Activate Delegate Event if (TempSensor_ByteArray_Received != null) { TempSensor_ByteArray_Received(SerialData); } if (RTC_Write_Response_Received != null) { RTC_Write_Response_Received(cCompleted); } break; } default: { //_SerialInstance.ClosePort(); break; } } } // Else If DataLength <=0 // No data was received else { //_SerialInstance.ClosePort(); } } catch (Exception Ex) { System.Diagnostics.Debug.WriteLine(Ex); throw; } }
void decoder_RTC_Data_Received(RTCSerialDataClass RTC_DATA, TransactionStatusClass STATUS) { Function_Return_GET_RTC_STATUS(STATUS.TransactionStatus); // decoderDisposeCollect(); //Not implemented }
void decoder_RTC_Data_Received(RTCSerialDataClass RTC_DATA, TransactionStatusClass STATUS) { Function_Return_GET_RTC_STATUS(STATUS.TransactionStatus); // decoderDisposeCollect(); //Not implemented }