Getting Started

PreviousNext

Creating a simple project

(c) 2007-2008 Hermetech International Ltd.

For demonstration purposes all project / code examples will be using Visual Basic .Net 2005. Note that this document is intended for HL7 professionals, it is beyond the scope of this document to explain what HL7 is and how it is implemented. It is assumed that you have a basic understanding of the HL7 message structure and data requirements at least as it pertains to your particular objective. Feel free to contact customer service (US#254-549-0825) to discuss your project requirements and goals. We will be more than happy to give you our opinion (for free and worth 10x what you pay for it) of whether anything within our arsenal of HL7 tools would be beneficial and how.

 

Step 1. In VB.Net 2005 create a blank Windows Forms Application.

Show Screenshot
New Windows Application

New Windows Application

 

Step 2. Add a reference to the toolkit DLL (filename: UPLTK.Net2005.dll). Note that you will have to "browse" for the DLL when adding the reference.

Show Screenshot
Add a reference to UPLTK.Net2005.dll

Add a reference to UPLTK.Net2005.dll

 

Step 3. In your main form add 2 buttons, 1 textbox and 1 richtextbox. Also add some labels as shown below. Our objective here is to create an application which allows you to a) enter the port number you will be receiving HL7 messages on into textbox1 b) click the 'Start Receiving Messages' button to begin c) echo anything received into RichTextbox1 and d) allow you to stop receiving messages by clicking the 'Stop Receiving Messages' button.

Show Screenshot
Design View of a simple form

Design View of a simple form

 

Step 4. In the code view (declarations section) declare your Proxy object and some variables.

Show Code

 

'The Listener Proxy Toolkit Object

Private WithEvents MyListener As New UPLTK.Net2005.ListenerProxy

 

'Some miscellaneous variables we'll use to keep score

Private nMessagesReceived As Long = 0

Private dtLastMessage As Date

Private strLastControlID As String = ""

Private strLastSendingApplication As String = ""

Private strLastType As String = ""

Private strLastEvent As String = ""

 

Step 5. Since the events raised by the Proxy object might be coming in from another thread and since we're in Windows Form we need a thread safe way to update our richtextbox.

Show Code

   Delegate Sub ChangeControlTextDelegate(ByVal ctrl As RichTextBox, _

                                      ByVal text As String)

   'The method with the delegate signature

   'Provides cross-thread safe response to events in this case using

   'a RichTextBox control

   Private Sub ChangeControlText(ByVal ctrl As RichTextBox, _

                                 ByVal text As String)

       If Me.InvokeRequired Then

           Me.Invoke(New ChangeControlTextDelegate(AddressOf ChangeControlText), _

                     New Object() {ctrl, text})

           Return

       End If

       Dim buff$

       Try

           buff$ = Now.ToString & " " & text & Environment.NewLine

           With ctrl

               If .TextLength > 50000 Then .Text = ""

               .SelectionStart = 0

               .SelectionLength = 0

               .SelectedText = buff$

           End With

 

       Catch ex As Exception

           MessageBox.Show(ex.Message, "Error Tracing", MessageBoxButtons.OK, MessageBoxIcon.Error)

       End Try

   End Sub

   Private Sub Trace(ByVal strMessage As String)

       'This is multi-thread safe

       ChangeControlText(RichTextBox1, strMessage)

       'This is not

       'Dim buff as String

       'Try

       '    buff = Now.ToString & " " & strMessage & Environment.Newline

       '    With RichTextBox1

       '        .SelectionStart = 0

       '        .SelectionLength = 0

       '        .SelectedText = buff

       '    End With

 

       'Catch ex As Exception

       '    MsgBox(ex.ToString)

       'End Try

   End Sub

 

Step 6. Add code to Button1 (the Start Receiving Messages button)

Show Code

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

       With MyListener

           If .IsListening() Then

               Button2.Enabled = True

               MessageBox.Show("The listener is running!", "Error", MessageBoxButtons.OK)

               Exit Sub

           End If

           .ClearErrors()

           RichTextBox1.Text = ""

           Trace("Starting the listener")

           Try

               Dim i As Integer

               i = CInt(TextBox1.Text.Trim())

              'Verify that it's a valid port number

               If i < 1 Or i > (1024 * 64) Then

                   MessageBox.Show("Invalid port number!", "Error", MessageBoxButtons.OK)

                   Exit Sub

               End If

               If Not MyListener.StartListening(i) Then

                   MessageBox.Show(MyListener.LastError, "Error From Object", MessageBoxButtons.OK, MessageBoxIcon.Error)

                   'Enable the Stop Listener Button

                   Button2.Enabled = True

                   Exit Sub

               End If

               Button2.Enabled = True

           Catch ex As Exception

               MessageBox.Show(ex.Message, "System Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

               Button2.Enabled = True

           End Try

       End With

   End Sub

 

Step 7. Add code to Button2 (the Stop Receiving Messages button)

Show Code

   Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

       Try

           With MyListener

               If .IsListening Then

                   .StopListening()

               End If

               Button2.Enabled = False

           End With

 

       Catch ex As Exception

           MessageBox.Show(ex.Message)

       End Try

   End Sub

 

Step 8. Handle the Events raised by the Proxy Object

Show Code

   Private Sub MyListener_ConnectionAccepted() Handles MyListener.ConnectionAccepted

       Trace("A connection was accepted!")

   End Sub

 

   Private Sub MyListener_ConnectionDropped() Handles MyListener.ConnectionDropped

       Trace("A connection was dropped")

   End Sub

 

   Private Sub MyListener_ListenerStarted() Handles MyListener.ListenerStarted

       Trace("The listener started!")

   End Sub

 

   Private Sub MyListener_ListenerStopped(ByVal strMessage As String) Handles MyListener.ListenerStopped

       Trace("The listener stopped!")

   End Sub

 

   Private Sub MyListener_HL7MessageReceived(ByRef objHL7Message As UPLTK.Net2005.ProxyHL7Message) Handles MyListener.HL7MessageReceived

       Try

           Dim sBuff As String = ""

           With objHL7Message

               nMessagesReceived += 1

               dtLastMessage = System.DateTime.Now

               strLastControlID = .HL7MSH_ControlID

               strLastSendingApplication = .HL7MSH_SendingApplication

               strLastType = .HL7MSH_MessageType

               strLastEvent = .HL7MSH_MessageEvent

               sBuff = .ThisHL7Message  'The entire HL7 message

           End With

           Trace("HL7 Message Received: " & Environment.NewLine & sBuff)

           'Update some of our scorekeeping labels

           lblMessageCount.Text = nMessagesReceived.ToString

 

           sBuff = dtLastMessage.ToShortTimeString & " From [" & _

           strLastSendingApplication & "] Type/Event [" & _

           strLastType & "/" & strLastEvent & _

           "] Control # [" & strLastControlID & "]"

 

           lblDetail.Text = sBuff

       Catch ex As Exception

 

       End Try

   End Sub

 

Step 9. Add some house cleaning code to the Form Closing event.

Show Code

   Private Sub frmMain_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

       Try

           If MyListener.IsListening Then

               MyListener.StopListening()

           End If

       Catch ex As Exception

       End Try

       MyListener = Nothing

 

Your Next Step: Review the ListenerProxy object