private Records.RecordRow ParseDataRecord(byte [] data) { DateTime dtTimeStamp = new DateTime(); int iGlucose = 0; string strUnits = string.Empty; if (data.Length != 0x18) { return(null); } int year = GetValue(data, 1, 2) + 2000; int month = GetValue(data, 4, 5); int day = GetValue(data, 7, 8); int hour = GetValue(data, 10, 11); int minutes = GetValue(data, 13, 14); int seconds = GetValue(data, 16, 17); dtTimeStamp = new DateTime(year, month, day, hour, minutes, seconds); iGlucose = GetValue(data, 19, 20); return(Records.AddRecordRow(dtTimeStamp, iGlucose, SampleFormat == SampleFormat.MGDL ? "mg/dl" : "mmol")); }
public void DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { Thread.Sleep(20); int readNumber = Port.BytesToRead; byte[] data = new byte[readNumber]; Port.Read(data, 0, readNumber); RawData += System.Text.Encoding.Default.GetString(data); if (RawData.Contains("\r\n") || currentCommand == "read") { switch (currentCommand) { case "test": case "ack": //read software version command _MeterFound = true; break; case "serial": //read serial number from meter SerialNumber = RawData.Split(new char[] { '\"' })[1].Trim(); #if DEBUG Console.WriteLine("SerialNumber: " + SerialNumber); #endif break; case "soft": //read software version and date string software = RawData.Split(new char[] { ' ' })[0].Substring(2).Trim(); OnHeaderRead(new HeaderReadEventArgs(5000, this)); #if DEBUG Console.WriteLine("Software Version: " + software); #endif break; case "format": //read software version and date string format = RawData.Split(new char[] { '\"' })[1].Trim().ToLower(); SampleFormat = (format == "mg/dl") ? SampleFormat.MGDL : SampleFormat.MMOL; #if DEBUG Console.WriteLine("SampleFormat: " + format); #endif break; case "count": case "read": //reads the glucose samples //check if End Record was read if (data.Length == 7) { SampleCount = Records.Count; _EndRecordEncountered = true; Port.DataReceived -= new System.IO.Ports.SerialDataReceivedEventHandler(DataReceived); OnReadFinished(new ReadFinishedEventArgs(this)); Close(); Dispose(); break; } //if //data = unEscape10s(data); //print raw hex data Console.Write("Record: "); foreach (byte b in data) { Console.Write(b.ToString("X") + " "); } //if Console.Write(Environment.NewLine); //check type byte to make sure its a glucose record if (data[12] == 0) { byte[] packetIDBytes = { data[2], data[3] }; UInt16 packetID = BitConverter.ToUInt16(packetIDBytes, 0); //parse glucose record byte[] glucose = new byte[2]; glucose[0] = data[0x07]; glucose[1] = data[0x08]; glucose[1] <<= 4; glucose[1] >>= 4; //parse date byte[] recordDate = new byte[4]; Array.Copy(data, 4, recordDate, 0, 3); DateTime recordDateTime = DateTime.Parse("1/1/2000 00:00"); int minutesFromSpike = BitConverter.ToInt32(recordDate, 0); recordDateTime = recordDateTime.AddMinutes((double)minutesFromSpike); try { OnRecordRead(new RecordReadEventArgs(Records.AddRecordRow(recordDateTime, BitConverter.ToInt16(glucose, 0), (SampleFormat == SampleFormat.MGDL ? "mg/dl" : "mmol")))); } catch { } } break; default: break; }//switch currentCommand = String.Empty; if (Port.IsOpen) { Port.DiscardInBuffer(); }//if RawData = String.Empty; }//if }
public void DataReceived(object sender, SerialDataReceivedEventArgs e) { _MeterResponded = true; if (!Port.IsOpen) { return; } _TempString += Port.ReadExisting(); if (_TempString.Contains("\r\n")) { string commandResponse = _TempString.Substring(0, _TempString.IndexOf("\r\n") + 2); _TempString = _TempString.Replace(commandResponse, ""); //========== serial number response ========== if (commandResponse.StartsWith("@")) { SerialNumber = commandResponse.Split(new char[] { ' ' })[1].Replace('\"', ' ').Trim(); #if DEBUG Console.WriteLine("SerialNumber: " + SerialNumber); #endif }//if //========== patient record response ========== else if (commandResponse.StartsWith("P")) { if (!_HeaderRead) { //first row of P records is the header _HeaderRead = true; _RecordCount = 0; string[] header = commandResponse.Split(new char[] { ',' }); SampleCount = int.Parse(header[0].Split(new char[] { ' ' })[1]); SerialNumber = header[1].Replace("\"", ""); SampleFormat = header[2].Substring(header[2].IndexOf('\"') + 1, header[2].LastIndexOf('\"') - (header[2].IndexOf('\"') + 1)).ToLower().Trim() == "mg/dl" ? SampleFormat.MGDL : SampleFormat.MMOL; #if DEBUG Console.WriteLine("SampleCount: " + SampleCount); Console.WriteLine("SampleFormat: " + SampleFormat.ToString()); Console.WriteLine("SerialNumber: " + SerialNumber); #endif if (_TestMode) { base.Close(); Dispose(); } OnHeaderRead(new HeaderReadEventArgs(SampleCount, this)); }//if else { //all other P records are glucose records Console.WriteLine("Record: " + commandResponse); string[] parsedMsg = commandResponse.Replace("\"", "").Replace(" ", "").Split(new char[] { ',' }); string[] parsedDate = parsedMsg[1].Split(new char[] { '/' }); string[] parsedTime = parsedMsg[2].Split(new char[] { ':' }); try { DateTime dtTimeStamp = new DateTime(int.Parse(parsedDate[2].ToString()), int.Parse(parsedDate[0].ToString()), int.Parse(parsedDate[1].ToString()), int.Parse(parsedTime[0].ToString()), int.Parse(parsedTime[1].ToString()), int.Parse(parsedTime[2].ToString())); if (dtTimeStamp.Year < 100) { //two digit year encountered (all dates are assumed to be in 2000+) dtTimeStamp = dtTimeStamp.AddYears(2000); }//if OnRecordRead(new RecordReadEventArgs(Records.AddRecordRow(dtTimeStamp, Int32.Parse(parsedMsg[3].ToString()), "mg/dl"))); }//try catch { } if (++_RecordCount == SampleCount) { //all records read so close the port and dispose OnReadFinished(new ReadFinishedEventArgs(this)); Dispose(); } //if } } //elseif } //if }
public void DataReceived(object sender, SerialDataReceivedEventArgs e) { if (Port == null || !Port.IsOpen) { return; } RawData += Port.ReadExisting(); //header already read so grab the records if (_HeaderRead) { using (StringReader reader = new StringReader(RawData)) { string line; while ((line = reader.ReadLine()) != null) { //end record encountered if (line.Contains("END")) { RawData = String.Empty; _autoResetEvent.Set(); break; }//if else { //not end of data file so parse it string[] linesplit = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); //add line if its not the end line if (line != Environment.NewLine && !line.Contains("END") && linesplit.Length > 0 && linesplit[linesplit.Length - 1] == "0x00") { RawData = RawData.Replace(line + "\r\n", ""); DateTime dtTimeStamp = DateTime.Parse(line.Substring(5, 18), CultureInfo.InvariantCulture); try { if (Records.FindByTimestamp(dtTimeStamp) == null) { #if DEBUG Console.WriteLine("Record: " + line); #endif OnRecordRead(new RecordReadEventArgs(Records.AddRecordRow(dtTimeStamp, Int32.Parse(linesplit[0]), SampleFormat == SampleFormat.MGDL ? "mg/dl" : "mmol"))); }//if else { #if DEBUG Console.WriteLine("DUPLIC: " + line); #endif } //else } //try catch { #if DEBUG //glucose was not a number (ex. LO) Console.WriteLine("NOTNUM: " + line); #endif }//catch line = null; } //if } //else } //while } //using } //if //header record is present if (!_HeaderRead && RawData.Contains("\r\n\n")) { string[] headeranddata = RawData.Split(new string[] { "\r\n\n" }, StringSplitOptions.RemoveEmptyEntries); string[] header = headeranddata[0].Split(new string[] { Environment.NewLine }, StringSplitOptions.None); #if DEBUG Console.WriteLine("Header: " + headeranddata[0]); #endif RawData = (headeranddata.Length > 1) ? headeranddata[1] : String.Empty; //set header read _HeaderRead = true; //set serial number SerialNumber = header[1]; #if DEBUG Console.WriteLine("SERIAL: " + SerialNumber); #endif //set sample count SampleCount = int.Parse(header[4]); OnHeaderRead(new HeaderReadEventArgs(SampleCount, this)); }//if }
public void DataReceived(object sender, SerialDataReceivedEventArgs e) { Thread.Sleep(75); if (!Port.IsOpen) { return; } RawData += Port.ReadExisting(); byte[] data = Statics.StrToByteArray(RawData); if (RawData.Contains(((char)0x03).ToString()) && RawData.Length >= RawData.IndexOf(((char)0x03).ToString()) + 3) { switch (currentCommand) { case "dis": //disconnect command Console.WriteLine("Disconnect"); break; case "soft": //read software version command softwareVersion = Statics.TrimNonPrintableCharacters(RawData.Substring(RawData.IndexOf("?A") + 2, RawData.IndexOf("?", RawData.IndexOf("?A") + 2) - (RawData.IndexOf("?A") + 2))); #if DEBUG Console.WriteLine("Software Version: " + softwareVersion); #endif break; case "serial": //read serial number from meter SerialNumber = Statics.TrimNonPrintableCharacters(RawData.Substring(RawData.IndexOf("?A") + 2, RawData.IndexOf("?", RawData.IndexOf("?A") + 2) - (RawData.IndexOf("?A") + 2))); #if DEBUG Console.WriteLine("Serial Number: " + SerialNumber); #endif break; case "count": //read serial number from meter string [] countReply = RawData.Split(new char[] { (char)AsciiCodes.STX }); byte[] dataReply = Statics.StrToByteArray(countReply[countReply.Length - 1]); SampleCount = 0; SampleCount = dataReply[3]; SampleCount = SampleCount << 8; SampleCount += dataReply[2]; numread = 0; OnHeaderRead(new HeaderReadEventArgs(SampleCount, this)); Console.WriteLine("Sample Count: " + SampleCount); break; case "format": Console.WriteLine("Format: " + RawData); byte[] formatReply = Statics.StrToByteArray(RawData); SampleFormat = (formatReply[0x0b] == 0) ? SampleFormat.MGDL : SampleFormat.MMOL; break; case "read": byte[] readReply = Statics.StrToByteArray(RawData); //get date out of bytes long iDate = readReply[0x0e]; iDate = iDate << 8; iDate += readReply[0x0d]; iDate = iDate << 8; iDate += readReply[0x0c]; iDate = iDate << 8; iDate += readReply[0x0b]; DateTime timestamp = UnixTimeStampToDateTime(iDate); double hexrep = ConvertToTimestamp(timestamp); int iGlucose = readReply[0x12]; iGlucose = iGlucose << 8; iGlucose += readReply[0x11]; iGlucose = iGlucose << 8; iGlucose += readReply[0x10]; iGlucose = iGlucose << 8; iGlucose += readReply[0x0f]; #if DEBUG Console.WriteLine("Record: " + timestamp + " " + iGlucose + " " + (SampleFormat == SampleFormat.MGDL ? "mg/dl" : "mmol")); #endif OnRecordRead(new RecordReadEventArgs(Records.AddRecordRow(timestamp, iGlucose, (SampleFormat == SampleFormat.MGDL ? "mg/dl" : "mmol")))); if (++numread >= SampleCount) { Port.DataReceived -= new SerialDataReceivedEventHandler(DataReceived); OnReadFinished(new ReadFinishedEventArgs(this)); Dispose(); } //if break; default: break; }//switch currentCommand = String.Empty; if (Port.IsOpen) { Port.DiscardInBuffer(); }//if RawData = String.Empty; }//if }