6

I'm using the HAPI hapi-structures-v25 library with version 2.3 to parse HL7v2 message & convert that into FHIR resources. I'm facing a strange issue while receiving and parsing the HL7V2 message using HAPI via TCP listener.

Determine encoding for message. The following is the first 50 chars of the message for reference, although this may not be where the issue is: MSH|^~\&|test|DrJhonDoe|TEST|UNKNOWN|20210216190432||ADT^A01^ADT_A01|60b647d4-b5a5-4fae-a928-d4a3849de3c8|T|2.5

Strange that I'm not getting this error when I'm trying to send this message as a string in main function. I'm getting this error only when I receive the data over TCP/IP to my Java function. I tried sending the HL7 message to my receiving TCP port using Mirth as well external tool & my result is same.

Here is the sample of my HL7v2 message Im trying to process

MSH|^~\\&|test|Dr.JhonDoe|TEST|UNKNOWN|20210216190432.7||ADT^A01^ADT_A01|60b647d4b5a54faea928d4a3849de3c8|T|2.5
EVN||20210216|20210216|

While receiving the data from tcp/ip im converting the byte to string using the UTF-8 charset.

InputStream in = connection.getInputStream();
OutputStream out = connection.getOutputStream();
receivedMessageSize = in.read(receivedByeBuffer);
String incomingHl7Message = new String(receivedByeBuffer, StandardCharsets.UTF_8);

Im getting the message properly. But not sure why the error comes.

2
  • Is hl7Message correct when passed to parser.parse? Have you checked it in debugger? Have you verified that there are no any unprintable characters in it? You may need to use some text editor which shows unprintable characters; Notepad++ may be.
    – Amit Joshi
    Feb 17, 2021 at 6:57
  • That's exactly the problem i'm facing here. I identified the issue. it is because of getting <VT> and <FS> only. When receiving data via normal Basic TCP i'm not getting this issue. But this issue im getting only when receiving the data thorugh MLLP & I couldn't find a better way to handle this characters in the incoming Bytes @AmitJoshi Feb 18, 2021 at 4:11

1 Answer 1

0

As mentioned in the answer by Amit, it needs to be escaped in JAVA. The HL7v2 when transmitted via MLLP it adds <VT>, <CR> Unicode data to the text. The understanding needed here is that these are not junk characters. By the protocol of MLLP the starting and ending of the messages are marked by these unicode characters to describe the starting and ending of a frame.

The HAPI HL7 parse cannot parse these special (non-printable) characters. Happy that I've found a solution on the same forum to handle it in java wisely. How to remove control characters from java string?

A simple regex will do the trick as shown below:

.replaceAll("[\\p{Cntrl}&&[^\r\n\t]]", "");

Also make sure that you're encoding characters are also handled properly with JAVA. Usually JAVA is not good in handling backslash. So, escape the backslash .replace("\\", "\\\\")

This will do the trick.

1
  • This might not be a complete solution. The fact is that MLLP is a streaming protocol not a typical socket connection. Hence, we first need to read the stream of message using the framing characters. But no luck in accomplishing that in Java. Mar 8, 2021 at 16:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.