HL7 Questions And Answers

Please feel free to send us any questions you have about HL7 and HL7 Interfaces at support@transworldscribe.com and if possible we will answer them here in this Blog/FAQ!

 

How to manipulate HL7 messages with C#

🛠️ Manipulating HL7 using our C# Script Library is surprisingly easy. The C# Script Library is implemented in 2 of our commercial products, the CORE HL7 C# Script Engine and the CORE HL7 TCP/IP Listener, as well as in our FREE product the CORE HL7 Viewer.

 

You don't have to be an expert C# coder and you don't have to be an expert in HL7 to write quick and easy scripts in Microsoft C# to perform HL7 related tasks! When you create your scripts you don't have to worry about where the messages come from, our platform will prevent the raw HL7 message to your script for you to manipulate as you like. In your scripts you have the ability to:

  • Examine and extract data from all parts of the HL7 message with easy-to-use pre-built class objects.
  • Remove or Add HL7 Segments.
  • Query or Update your Microsoft SQL or MySQL databases with the built in SQL Connectors
  • You can even create new HL7 messages.
  • You can create different types of scripts, those that are intended to be "fed" HL7 messages by the hosting application (see the example at the bottom of this article), OR scripts that are designed to just run continuously until you tell the hosting application to stop them.

We have extensive help available for programmers as well as a robust library of YouTube Videos on creating, testing, and implementing your C# scripts. See the C# Script Engine YouTube Playlist for tutorial videos.

 

🔍 Recommended Next Steps to Get Started

  • Download and install our FREE CORE HL7 Viewer.
  • Download and install the CORE HL7 C# Script Engine. FYI, you can activate the Script Engine software and get fully functional 30 day demo license!

  • If you want to run your C# Scripts as HL7 is being received by the HL7 TCP/IP Listener download and install the CORE HL7 TCP/IP Listener.

  • We recommend that you use Microsoft Visual Studio to edit your scripts. You can download the FREE Community edition if you don't have Visual Studio installed.

  • Watch a few of the HL7 C# tutorial videos on our YouTube channel.

  • Get Coding! If you need any help at all please just contact us here at support@transworldscribe.com OR by phone 24/7 at our USA number 254-549-0825 (Country Code 1)

 

Example HL7 C# Script

Below is an example CORE HL7 C# Script. This is an example of what we call a TYPE 1 script, meaning that is meant to be "fed" HL7 messages by the hosting application one by one. In the CORE HL7 Viewer you can run this script against any HL7 messages you have loaded into your viewer. The CORE HL7 Script Engine will monitor a Windows folder for HL7 message data files and when one (or more) is detected it will extract all messages into memory and "feed" them one at a time to this script. The script is only compiled ONCE and then run many times with fresh HL7 messages. We clock this script on our development computers as taking about 6 to 7 milliseconds to run for each message.

 


 

//This very simple script only does a couple of simple things

//It resets the Message Control ID to a GUID and it changes the

//Patient Name to John Smith

using System;

using COREHL7;

using COREHL7ScriptHost;

/* ABOVE are the using statements which are required */

using System.Collections.Generic;

using System.Linq;

using System.IO;

using System.Xml;

using System.Data;

using System.Data.SqlClient;

using System.Text;

/* ABOVE are the optional using statements you can include */

 

      public static COREHL7Script globals;

      public static CHS_Message MyMessage;

      public class Script

   {

          public void Execute(COREHL7Script parent)

     {

       globals = parent;

              try

       {

         globals.Trace("Script is running.");

         globals.Completed = true;

                  if (string.IsNullOrEmpty(globals.InitialHL7Message))

         {

           globals.SetException("No action required.No initial message""Script()", 321342);

           globals.Completed = true;

                      return;

          }

         MyMessage = globals.CreateInitialMessage();

                  if (globals.HasException)

         {

           globals.MoveInitialMessageToError();

           globals.Completed = true;

                      return;

          }

                  //We will set the Message Control ID (MSH 10.1) to a GUID

         MyMessage.MessageControlID = System.Guid.NewGuid().ToString();

                  CHS_Segment pid = MyMessage.GetFirstSegmentNamed("PID");

                  if (pid != null)

         {

                      //Get the Patient Name Field

                      CHS_Field pid5 = pid.GetFieldToEdit(5);

                      //Set Last Name and First Name just for fun

           pid5.SetValue("Smith", 1); //Last Name

           pid5.SetValue("John", 2); //First Name

          }

                  //Commit message to see changes in viewer OR write to output folder in Script Engine

         globals.CommitMessage(MyMessage);

         globals.Completed = true;

                  return;

       }

              catch (Exception ex)

       {

                  //This SetException method is your global method to handle exceptions

         globals.SetException("System Error: " + ex.Message, "RunScript()", 10201);

         globals.Completed = false;

                  return;

        }

      }

    } //End of Script Class

 

      /* This line below MUST be the LAST line in your script */

      return new Script();