• Inner Harbour Software

    Inner Harbour Software consists of a dedicated team of .Net programmers whose mission is to create exceptional and affordable HL7 products. Please email us at support@HL7Spy.com to contact us with questions regarding our products or services.

Archive for 2012

How to handle Z-Segments in Custom Code

How can we handle Z-Segments in HL7Spy’s Custom Code Tool?

The following code demonstrates how use Z-Segments. We start by adding a couple of ZF1 segments (1), then we set a few fields (2), and finally print the fields out using the Log function.

public override void Run() {
  // Get an HL7 Message in parsed format
  Hl7Message message = GetParsedMessage();
  // (1) Append a couple of ZF1 segments
  message.Append(new Segment("ZF1")); // Append ZF1
  message.Append(new Segment("ZF1")); // Append a second ZF1
  string today=DateTime.Today.ToString("yyyyMMdd");
  int count=0;
  foreach(Segment zf1 in message.GetSegments("ZF1"))
    //(2) set ZF1-3.1, and ZF1-3.2 with information from the MSH 
    zf1[3,1,1] = message.MSH[3,1,1] + "-V" + today;
    zf1[3,1,2] = message.MSH[9,1,2] + "-TEST";
    zf1[6] = count.ToString();
  // (3) Log the information out using the Hl7Message class
  string firstZF1_6 = message["ZF1-6"];
  string secondZF1_6 = message["ZF1[2]-6"];
  Log(Severity.Informational,string.Format("ZF1-6={0}", firstZF1_6));
  // save the message out to a new tab
  SaveMessage(message, "Modified Messages");  

HL7Spy 1.7.79 Released

  • Bug – Fix exception that occurs when DataGridView is asked to show a row, but there is not enough room
  • Bug – Better error handing in OpenTcpPortView
  • Bug – Fixed issue pasting text into a message when the text is not a full message
  • Bug – Refresh button was duplicating messages within the message tab
  • Improvement – Auto-retry connection if HL7 MLLP Send fails on first attempt
  • Improvement – Support characters above 127 in the ASCII table in the Message Framing dialog
  • Improvement – Faster HL7 MLLP Receive

HL7Spy 1.7.77 Released

  • Bug – Fixed InvalidCastException when exporting the query results from the Sql Loader data grid to Excel.
  • Improvement – Better handling of messages pasted from emails or other applications that modify segment line endings
  • Improvement – Less flicker when tabs are closed
  • Improvement – Faster application shutdown
  • Improvement – Enable dragging/dropping messages from 1 tab to another. This is useful for creating an ad hoc collection of messages to be saved, or transmitted. The best way to utilize this feature is to drag one of the tabs out of the main document area, or to drag one of the tabs so that it side-by-side with the other tabs as shown in the figure below. Messages that are dragged and dropped are always inserted after the currently displayed message.

HL7Spy v1.7.75 Released

  • Improvement – Upgraded some 3rd Party libraries that we use within the application
  • Bug – Fixed an issue when you double-click on a file in Windows Explorer and an existing instance of HL7Spy is shutting down. (thank you George S. for reporting)
  • Bug – Fixed an issue in the Splitting Tool where splitting by message index range was not working
  • Improvement – Sped up start time by lazy initializing the Custom Code tool
  • Improvement – Sped up application start time by 5 seconds.
  • Bug – Fixed a problem with gzip handling
  • Bug – Fixed a FileDisposed exception being thrown when the last Query Results tab is closed
  • Bug – Fixed an issue in HL7 SQL where the left most column showed incorrect index numbers when DISTINCT is selected from the toolbar.
  • Bug – Fix the formatting of date/times in Excel (thanks to Larry E. for reporting and giving us the fix!)

Find last known allergies for each patient

How can we use HL7Spy to find the last set of known allergies for each patient in a set of messages?

The best way is to write a custom function that remembers the last set of allergies for each patient. Then in the OnFinished function we print out all patients with their last set of known allergies.

string _fileName = @"c:\temp\_test.txt";
Dictionary<string,Patient> _data = new Dictionary<string,Patient>(10000);

public class Patient
   public Patient(string patientId) 
    Allergies = new HashSet<string>();
    PatientId = patientId;
   public HashSet<string> Allergies;
   public string PatientId;

public override void Run() { 
  // Get an HL7 Message in parsed format
  Hl7Message message = GetParsedMessage();
  MSH msh = message.Segments.First<MSH>();
  if(msh==null || msh.MessageType_09.TriggerEvent_02.Value!="A08")
    return; // not an ADT^A08 message
  PID pid = message.Segments.First<PID>();
    return; // no pid segment
  List<AL1> patientAllergies = message.Segments.OfType<AL1>();
    return; // no allergies
  string patientId = pid.PatientIdentifierList_03.First.ToString();  
  Patient pat;
  if(!_data.TryGetValue(patientId,out pat))
     pat = new Patient(patientId); 
  // loop through all AL1 segments
  foreach(AL1 al1 in patientAllergies)
// Called once after the last message has been processed.
// It is a good place to perform cleanup and to report information.
// Always called from the UI thread.
public override void OnFinish() 
  // to figure out the distribution of all allergies
  Dictionary<string, int> allergies = new Dictionary<string,int>(1000);
  // write the data out to a file
  using(System.IO.StreamWriter wr = new System.IO.StreamWriter(_fileName))
    wr.WriteLine("Patient ID vs Allergy for {0} patient samples",_data.Count);
    foreach(Patient p in _data.Values.OrderByDescending(p=>p.Allergies.Count))
        foreach(string s in p.Allergies)
           // calculate the total number of allergy types for the given set of patients
           int frequency=0;
           if(!allergies.TryGetValue(s,out frequency))
           } else {
    wr.WriteLine("Allergy distribution for {0} patient samples.",_data.Count);
    wr.WriteLine("Number of distinct allergies is {0}.",allergies.Count);    
    foreach(KeyValuePair<string,int> d in allergies)
// launch notepad to display the file.