How to send HL7 ACK message as a TCP response?
Asked Answered
O

1

5

I'm receiving HL7 messages via TCP connection. Those messages will always be of type ADT. I'm using Kestrel to listen for thise messages and the NHAPI package to deal with them. I took David Fowler's Kestrel sample code to setup a TCP listener. So based on this sample code

internal class HL7Listener : ConnectionHandler
{
    public override async Task OnConnectedAsync(ConnectionContext connection)
    {
        try
        {
            // handle the incoming message
        }
        catch (Exception exception)
        {
            // handle exceptions
        }
        finally
        {
            ACK acknowledgement = new ACK(); // create an ACK message
            PipeParser pipeParser = new PipeParser();
            string ackMessage = pipeParser.Encode(acknowledgement); // produces => MSH|^~\&|||||||ACK|||2.3
            byte[] ackMessageBytes = Encoding.UTF8.GetBytes(ackMessage);
            
            await connection.Transport.Output.WriteAsync(ackMessageBytes); // send the ACK
        }
    }
}

I'm using the tool 7Edit to send HL7 messages to my application. The echo sample from the repository (link above) works fine. The echo sample code produces a log like this

enter image description here

But when using my code I get this error

enter image description here

So I think I don't convert the ACK message correctly. Any ideas how to fix this?

Ogham answered 17/9, 2020 at 10:7 Comment(1)
Piped HL7 usually comes from a device like a blood tester. So the ack is usually just one character (sometimes with a return). So you have to read the device manual to see what to use for the ACK. You do not need an await which may be you problem. The device may be timing out because you did not send another message. What do you think is going to let code continue after the await? The sample code does not have an await at the end. The sample code has an await for the reading not the writing.Physics
M
6

I doubt you are not implementing MLLP (also called LLP) protocol while sending ACK. I know, 7Edit expects MLLP to be implemented. This way, when you send an ACK to 7Edit (TCP/MLLP client), it looks for Start Block in your incoming data. It never finds it. It just discards your entire message considering garbage and keeps waiting; which causes timeout as you can see.

May be you should look for some setting in 7Edit to disable MLLP; but this will be temporary solution. Better, you implement MLLP block.

Description                 HEX     ASCII   Symbol
Message starting character  0B      11      <VT>
Message ending characters   1C,0D   28,13   <FS>,<CR>

With MLLP implemented, your message (the stuff you are writing on socket) should look something like below:

<VT>MSH|^~\\&|......|ACK|......<FS><CR>

Note the <VT>, <CR> and <FS> are place holders in above message.

You should modify your following line of code:

byte[] ackMessageBytes = Encoding.UTF8.GetBytes(ackMessage);

as below:

ackMessage = ((char) 11).ToString() + ackMessage + ((char) 28).ToString() + ((char) 13).ToString();
byte[] ackMessageBytes = Encoding.UTF8.GetBytes(ackMessage);

For more details, you may refer to this answer.

Mention answered 17/9, 2020 at 11:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.