public void DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { _TempString += Port.ReadExisting(); //Get number of samples on meter if (!_NumResultsRead && (_TempString.Contains(Statics.GetStringFromAsciiCode((byte)AsciiCodes.ENQ)) || _TempString.Contains(Statics.GetStringFromAsciiCode((byte)AsciiCodes.ACK)))) { switch (_CountStep) { case 0: Port.Write(new byte[] { 0x15 }, 0, 1); _CountStep++; break; case 1: Port.Write(new byte[] { 0x05 }, 0, 1); _CountStep++; break; case 2: Port.Write("R|"); _CountStep++; break; case 3: Port.Write("M|"); _CountStep++; break; case 4: Port.Write(new byte[] { 0x39, 0x37, 0x0d, 0x0a }, 0, 4); _CountStep++; System.Threading.Thread.Sleep(1000); break; case 5: _NumResultsRead = true; try { byte[] temp = Statics.StrToByteArray(_TempString); string tempCount = _TempString.Substring(_TempString.IndexOf("|") + 1, _TempString.LastIndexOf("|") - _TempString.IndexOf("|") - 1); SampleCount = Convert.ToInt32(tempCount); Port.Write(new byte[] { 0x04 }, 0, 1); _TempString = string.Empty; _CountStep = 0; System.Threading.Thread.Sleep(100); } catch { Port.DataReceived += null; Port.DiscardInBuffer(); Port.DiscardOutBuffer(); _TestFailed = true; return; } #if DEBUG Console.WriteLine("SampleCount: " + SampleCount); #endif break; default: break; }//switch return; }//if else if (_NumResultsRead && !_ConfigRead && (_TempString.Contains(Statics.GetStringFromAsciiCode((byte)AsciiCodes.ENQ)) || _TempString.Contains(Statics.GetStringFromAsciiCode((byte)AsciiCodes.ACK)))) { switch (_CountStep) { case 0: Port.Write(new byte[] { 0x15 }, 0, 1); _CountStep++; break; case 1: Port.Write(new byte[] { 0x05 }, 0, 1); _CountStep++; break; case 2: Port.Write("R|"); _CountStep++; break; case 3: Port.Write("C|"); _CountStep++; break; case 4: Port.Write(new byte[] { 0x38, 0x44, 0x0d, 0x0a }, 0, 4); _CountStep++; System.Threading.Thread.Sleep(1000); break; case 5: _ConfigRead = true; try { string[] splitData = _TempString.Split(new char[] { '|' }); System.Collections.BitArray bitary = new System.Collections.BitArray(Byte.Parse(splitData[1])); SampleFormat = (bitary.Get(2)) ? SampleFormat.MMOL : SampleFormat.MGDL; Port.Write(new byte[] { 0x04 }, 0, 1); _CountStep = 0; _TempString = string.Empty; } catch { Port.DataReceived += null; Port.DiscardInBuffer(); Port.DiscardOutBuffer(); _TestFailed = true; return; } #if DEBUG Console.WriteLine("SampleFormat: " + SampleFormat.ToString()); #endif break; default: break; }//switch return; }//else //if data received is the ENQ to start communications else if (!_HeaderRead && _TempString.Contains(Statics.GetStringFromAsciiCode((byte)AsciiCodes.ENQ))) { Port.Write(Statics.GetStringFromAsciiCode((byte)AsciiCodes.ACK)); RawData += _TempString; _TempString = String.Empty; return; }//if //if data contains an STX and a following LF then a full frame can be trimmed else if (_TempString.Contains(Statics.GetStringFromAsciiCode((byte)AsciiCodes.STX)) && _TempString.Substring(_TempString.IndexOf(Statics.GetStringFromAsciiCode((byte)AsciiCodes.STX))).Contains(Statics.GetStringFromAsciiCode((byte)AsciiCodes.LF))) { RawData += _TempString; //full frame encountered (cut out full frame, and remove the STX on the front) string fullframe = _TempString.Split(new string[] { Statics.GetStringFromAsciiCode((byte)AsciiCodes.CR) }, StringSplitOptions.None)[0].Replace(Statics.GetStringFromAsciiCode((byte)AsciiCodes.STX), ""); //trim off the frame as more data may be in the buffer _TempString = _TempString.Substring(_TempString.LastIndexOf(Statics.GetStringFromAsciiCode((byte)AsciiCodes.LF)) + 1); #region HeaderRecord if (fullframe[1] == 'H') { _HeaderRead = true; string[] headerrecord = fullframe.Split(new char[] { '|' }); string[] typeandserial = headerrecord[4].Split(new char[] { '^' }); string accesspassword = headerrecord[3]; string softwareversion = typeandserial[1].Split(new char[] { '\\' })[0]; string eepromversion = typeandserial[1].Split(new char[] { '\\' })[1]; MeterDescription = typeandserial[0]; string MeterType = typeandserial[0]; SerialNumber = typeandserial[2]; //breeze meters have a product number of 6115 _MeterFound = (MeterType.ToLowerInvariant() == "bayer6115"); if (_TestMode) { Port.DataReceived += null; return; } OnHeaderRead(new HeaderReadEventArgs(SampleCount, this)); Console.WriteLine("Header: " + fullframe); }//if #endregion #region Glucose Record else if (fullframe[1] == 'R') { string[] splitrecord = fullframe.Split(new char[] { '|' }); //only if glucose record if (splitrecord.Length > 10) { int year = int.Parse(splitrecord[11].Substring(0, 4)); int month = int.Parse(splitrecord[11].Substring(4, 2)); int day = int.Parse(splitrecord[11].Substring(6, 2)); int hour = int.Parse(splitrecord[11].Substring(8, 2)); int minute = int.Parse(splitrecord[11].Substring(10, 2)); int glucose = int.Parse(splitrecord[3]); string units = splitrecord[4].Split(new char[] { '^' })[0]; DateTime dtTimeStamp = new DateTime(year, month, day, hour, minute, 0); //put the record in the dataset and raise the read event try { if (Records.FindByTimestamp(dtTimeStamp) == null) { #if DEBUG Console.WriteLine("Record: " + fullframe); #endif OnRecordRead(new RecordReadEventArgs(this._Records.AddRecordRow(dtTimeStamp, glucose, units))); }//if else { #if DEBUG Console.WriteLine("DUPLIC: " + fullframe); #endif } //else } //try catch { } //catch } //if } //elseif #endregion } //else //end of transmission encountered after a header record is read else if (_HeaderRead && _TempString.Contains(Statics.GetStringFromAsciiCode((byte)AsciiCodes.EOT))) { _HeaderRead = false; Port.DataReceived -= new System.IO.Ports.SerialDataReceivedEventHandler(DataReceived); OnReadFinished(new ReadFinishedEventArgs(this)); Close(); Dispose(); return; }//elseif //send response if (_NumResultsRead) { Port.Write(Statics.GetStringFromAsciiCode((byte)AsciiCodes.ACK)); } }
public void DataReceived(object sender, UsbLibrary.DataRecievedEventArgs e) { Array.Clear(writeBuffer, 0, writeBuffer.Length); char[] asciiChars = new char[Encoding.ASCII.GetCharCount(e.data, 0, e.data.Length)]; Encoding.ASCII.GetChars(e.data, 0, e.data.Length, asciiChars, 0); string newString = new string(asciiChars); tempString += new string(asciiChars).Substring(5); tempString = tempString.Replace("\0", string.Empty); //send read number of results on device message if (_HeaderRead && !_NumResultsRead && _CountStep <= 3) { GetSampleCountMessages(); return; } ////send read config message //if (_HeaderRead && _NumResultsRead && !_ConfigRead && _CountStep <= 1) //{ // GetConfigMessages(); // return; //} //preheader if (tempString.StartsWith(Statics.GetStringFromAsciiCode((byte)AsciiCodes.ENQ)) && tempString.Length == 1) { tempString = string.Empty; writeBuffer[3] = (byte)AsciiCodes.RH; writeBuffer[4] = (byte)AsciiCodes.ACK; Port.SpecifiedDevice.SendData(writeBuffer); return; } //new line detected so frame is complete if (tempString.Contains("\r\n")) { tempString = tempString.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries)[0]; //header if (tempString.Contains(((char)AsciiCodes.STX).ToString() + "1H")) { _HeaderRead = true; ParseHeader(tempString); tempString = string.Empty; writeBuffer[3] = (byte)AsciiCodes.RH; writeBuffer[4] = (byte)AsciiCodes.EOT; Port.SpecifiedDevice.SendData(writeBuffer); Thread.Sleep(100); return; } //patient record if (tempString.Length > tempString.IndexOf((char)AsciiCodes.STX) + 2 && tempString[tempString.IndexOf((char)AsciiCodes.STX) + 2] == 'P') { tempString = string.Empty; } //terminator record if (tempString.Length > tempString.IndexOf((char)AsciiCodes.STX) + 2 && tempString[tempString.IndexOf((char)AsciiCodes.STX) + 2] == 'L') { tempString = string.Empty; } //glucose record if (tempString.Contains("^^^Glucose")) { string[] splitrecord = tempString.Split(new string[] { "|", "\r", "^" }, StringSplitOptions.RemoveEmptyEntries); int year = int.Parse(splitrecord[7].Substring(0, 4)); int month = int.Parse(splitrecord[7].Substring(4, 2)); int day = int.Parse(splitrecord[7].Substring(6, 2)); int hour = int.Parse(splitrecord[7].Substring(8, 2)); int minute = int.Parse(splitrecord[7].Substring(10, 2)); DateTime dtTimeStamp = new DateTime(year, month, day, hour, minute, 0); int glucose = int.Parse(splitrecord[3]); string sampleFormat = splitrecord[4]; this.SampleFormat = sampleFormat.ToLowerInvariant().Contains("mmol") ? SampleFormat.MMOL : SampleFormat.MGDL; //put the record in the dataset and raise the read event try { if (Records.FindByTimestamp(dtTimeStamp) == null) { //only insert non-duplicate records OnRecordRead(new RecordReadEventArgs(this._Records.AddRecordRow(dtTimeStamp, glucose, sampleFormat))); RecordsRead++; } //if } //try catch { }//catch tempString = string.Empty; } //num results message arrived if (tempString.Contains("D|") && !_NumResultsRead) { //cut message out of string string message = tempString.Substring(tempString.IndexOf("D|")); string [] splits = tempString.Split(new string[] { "|" }, StringSplitOptions.RemoveEmptyEntries); this.SampleCount = Convert.ToInt32(splits[1]); tempString = string.Empty; _NumResultsRead = true; _CountStep = 0; Thread.Sleep(100); OnHeaderRead(new HeaderReadEventArgs(SampleCount, this)); //GetConfigMessages(); return; } ////results format //if (tempString.Contains("D|") && !_ConfigRead) //{ // //cut message out of string // string message = tempString.Substring(tempString.IndexOf("D|")); // string[] splits = tempString.Split(new string[] { "|" }, StringSplitOptions.RemoveEmptyEntries); // BitArray bitary = new BitArray(Byte.Parse(splits[1])); // SampleFormat = (bitary.Get(2)) ? SampleFormat.MMOL : SampleFormat.MGDL; // tempString = string.Empty; // _ConfigRead = true; // _CountStep = 0; // Thread.Sleep(100); // return; //} } //end of transmission encountered after a header record is read if (_HeaderRead && _NumResultsRead && RecordsRead >= SampleCount) { _HeaderRead = false; Port.OnDataRecieved -= new UsbLibrary.DataRecievedEventHandler(DataReceived); Port.SpecifiedDevice.DataRecieved -= SpecifiedDevice_DataRecieved; OnReadFinished(new ReadFinishedEventArgs(this)); Close(); Dispose(); return; }//elseif if (_NumResultsRead)// && _ConfigRead) { Array.Clear(writeBuffer, 0, writeBuffer.Length); writeBuffer[4] = (byte)AsciiCodes.RH; writeBuffer[5] = (byte)AsciiCodes.ACK; Port.SpecifiedDevice.SendData(writeBuffer); Thread.Sleep(100); } }
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 }