/* hl7demo.c * * Copyright (C) 1993 Igor Chernizer. Columbia University. * Columbia-Presbyterian Medical Center ******************************************************************* * * Demonstration of HL7 Import/Export functions * * A. Builds ADT^A17 HL7 message - Swap Patients : msh-evn-{pid-pv1} * * 1. Start to create new structure * 2. Insert and fill MSH * 3. Insert and fill EVN * 4. Insert and fill PV1 * 5. Insert and fill PID * 6. Try to build flat HL7 message * Trap error * Rebuild message correctly * 7. Set current position on a first segment in the message * 8. Find first occurence of PID * 9. Print 2nd component from 5th field from current PID message * 10. Build HL7 message * 11. Traverse trough message * 12. Free HL7 data structure * 13. Start to create new structure * 14. Encode HL7 message and put data in Data Structure * 15. Get last segment * 16. If last segment is PV1, append and fill PID * 17. Try to encode it and catch error message ********************************************************************* * * */ #include #include #include #include /*#include "unistd.h"*/ #include "hl7_api.h" static void PrtHL7Msg(char *, int); main() { HL7MSG *pMsg=0; SIZE Len; HL7SegRule *pSegType; /* segment type */ int iResult; char wire[10000]; /* buffer for HL7 message */ char stdinJnk[89],*pC; FILE *dtmfd; HL7Init( "", ".v21"); /* read in and build tables */ printf("\n1. Start to create new structure"); fflush(stdout); pMsg = HL7Alloc(); printf("\n2. Insert and fill MSH"); fflush(stdout); HL7InsSeg(pMsg, HL7pMSH); /* fill out MSH */ HL7PutFld(pMsg, "|", 1); HL7PutFld(pMsg, "^~\\&", 2); HL7PutFld(pMsg, "PM", 3); HL7PutFld(pMsg, "CIS", 4); HL7PutFld(pMsg, "19911030140014865", 7); HL7PutFld(pMsg, "ADT", 9); HL7PutComp(pMsg, "A17", 9); /* add new component */ HL7PutFld(pMsg, "14001U090", 10); HL7PutFld(pMsg, "P", 11); HL7PutFld(pMsg, "2.1", 12); printf("\n3. Insert and fill EVN"); fflush(stdout); HL7InsSeg(pMsg, HL7pEVN); /* fill out EVN */ HL7PutFld(pMsg, "A17", 1); HL7PutFld(pMsg, "19911030140014865", 2); HL7PutFld(pMsg, "003", 4); printf("\n4. Insert and fill PV1"); fflush(stdout); HL7InsSeg(pMsg, HL7pPV1); /* fill out PV1 */ HL7PutFld(pMsg,"1", 1); HL7PutFld(pMsg,"10", 13); HL7PutFld(pMsg,"12", 32); HL7PutFld(pMsg,"13", 33); HL7PutFld(pMsg,"15", 47); HL7PutFld(pMsg,"16", 48); HL7PutFld(pMsg,"17", 49); HL7PutFld(pMsg,"13", 50); printf("\n5. Insert and fill PID"); fflush(stdout); HL7InsSeg(pMsg, HL7pPID); /* fill out PID */ HL7PutFld(pMsg, "1", 1); HL7PutFld(pMsg, "2596690", 3); HL7PutFld(pMsg, "379112", 4); HL7PutFld(pMsg, "MORALES^MARIANA", 5); HL7PutFld(pMsg, "19440704", 6); HL7PutFld(pMsg, "F", 7); HL7PutFld(pMsg, "U", 9); HL7PutFld(pMsg, "95 LENOX AVE APT 10F~NYC~NY~10026", 10); printf("\n6. Build Wire "); fflush(stdout); if ((iResult = HL7WriteMsg(pMsg, wire, 10000, &Len)) == HL7_OK) { fprintf( stderr,"\n MISSED ERROR !\n press return to continue"); gets( stdinJnk); } else printf("\n trapped error, %s", ErrTxt(pMsg, iResult)); HL7Free(pMsg); printf("\n rebuilding message."); pMsg = HL7Alloc(); HL7InsSeg(pMsg, HL7pMSH); /* fill out MSH */ HL7PutFld(pMsg, "|", 1); HL7PutFld(pMsg, "^~\\&", 2); HL7PutFld(pMsg, "PM", 3); HL7PutFld(pMsg, "CIS", 4); HL7PutFld(pMsg, "19911030140014865", 7); HL7PutFld(pMsg, "ADT", 9); HL7PutComp(pMsg, "A17", 9); /* add new component */ HL7PutFld(pMsg, "14001U090", 10); HL7PutFld(pMsg, "P", 11); HL7PutFld(pMsg, "2.1", 12); HL7InsSeg(pMsg, HL7pEVN); /* fill out EVN */ HL7PutFld(pMsg, "A17", 1); HL7PutFld(pMsg, "19911030140014865", 2); HL7PutFld(pMsg, "003", 4); HL7InsSeg(pMsg, HL7pPID); /* fill out PID */ HL7PutFld(pMsg, "1", 1); HL7PutFld(pMsg, "2596690", 3); HL7PutFld(pMsg, "379112", 4); HL7PutFld(pMsg, "MORALES^MARIANA", 5); HL7PutFld(pMsg, "19440704", 6); HL7PutFld(pMsg, "F", 7); HL7PutFld(pMsg, "U", 9); HL7PutFld(pMsg, "95 LENOX AVE APT 10F~NYC~NY~10026", 10); HL7InsSeg(pMsg, HL7pPV1); /* fill out PV1 */ HL7PutFld(pMsg,"1", 1); HL7PutFld(pMsg,"10", 13); HL7PutFld(pMsg,"12", 32); HL7PutFld(pMsg,"13", 33); HL7PutFld(pMsg,"15", 47); HL7PutFld(pMsg,"16", 48); HL7PutFld(pMsg,"17", 49); HL7PutFld(pMsg,"13", 50); if ((iResult = HL7WriteMsg(pMsg, wire, 10000, &Len)) != HL7_OK) { printf("\n unexpected ERROR, %s \n press return to conitinue", ErrTxt(pMsg, iResult)); gets( stdinJnk); } PrtHL7Msg(wire, strlen(wire)); /* print message */ /* 7. Set current position on a first segment in the message */ printf("\n7. position to 1st Segment "); fflush(stdout); HL7FirstSeg(pMsg, &pSegType); /* posit first segment */ printf("\n8. Find PID "); fflush(stdout); HL7FindSeg(pMsg, HL7pPID); /* find first PID */ printf("\n field #4 from PID - '%s'",HL7GetFld(pMsg, 4)); printf("\n9. component #2 from field #5 from PID - '%s'", HL7GetComp(pMsg, 5, 1, 2)); fflush(stdout); /* 11. Traverse trough message */ printf("\n11. Traverse message "); fflush(stdout); HL7FirstSeg(pMsg, &pSegType); /* posit first segment */ do { printf("\n message-'%s'", pSegType->azNam); } while (HL7NextSeg(pMsg, &pSegType)!= HL7_END_OF_STRUCT); /* 12. Free HL7 data structure */ printf("\n12. Free HL7 structure "); fflush(stdout); HL7Free(pMsg); /* 13. Start to create new structure */ printf("\n13. Start a new structure "); fflush(stdout); pMsg = HL7Alloc(); /* 14. Encode HL7 message and put data in Data Structure */ printf("\n14. Encode HL7 message from wire "); fflush(stdout); HL7ReadMsg(pMsg, wire); /* 15. Get last segment */ printf("\n15. Get Last Segment "); fflush(stdout); HL7LastSeg(pMsg, &pSegType); printf(" %s ",pSegType->azNam); /* 16. If last segment is PV1, append and fill PID */ printf("\n16. Add PID "); fflush(stdout); if (pSegType == HL7pPV1) { HL7InsSeg(pMsg, HL7pPID); HL7PutFld(pMsg, "1", 1); HL7PutFld(pMsg, "2596690", 3); HL7PutFld(pMsg, "379112", 4); HL7PutComp(pMsg, "MORALES^MARIANA", 5); HL7PutComp(pMsg, "MARIANA", 5); HL7PutFld(pMsg, "19440704", 6); HL7PutFld(pMsg, "F", 7); HL7PutFld(pMsg, "U", 9); HL7PutRep(pMsg, "95 LENOX AVE APT 10F", 10); HL7PutRep(pMsg, "NYC", 10); HL7PutRep(pMsg, "NY", 10); HL7PutRep(pMsg, "10026", 10); } /* 17. Try to encode it and catch error message */ printf("\n17. Encode and catch error message"); fflush(stdout); if ((iResult = HL7WriteMsg(pMsg, wire, 10000, &Len)) == HL7_OK) { printf("\n MISSED ERROR! \n press return to continue"); gets( stdinJnk); } printf("\n Caught error, %s", ErrTxt(pMsg, iResult)); HL7Free(pMsg); printf("\n18. Encode a bunch of test messages from demotst.msgs\n"); dtmfd = fopen( "demotst.msgs","r"); if (dtmfd==0) { fprintf(stderr,"\nFailed to open demotst.msgs\n"); exit(0);} while( fgets( wire, 9999, dtmfd)) { pMsg = HL7Alloc(); if (( iResult = HL7ReadMsg(pMsg, wire)) == HL7_OK) { HL7FirstSeg( pMsg, &pSegType); printf(" %s ", pSegType->azNam); while (HL7NextSeg( pMsg, &pSegType)==HL7_OK) { printf(" %s ", pSegType->azNam); if (pSegType==pSegRule("OBR")) if ( (pC= HL7GetFld( pMsg, 1)) ) printf(":%s", pC); } printf("\n"); } else printf("Failed on:%.7s %.60s\n", pMsg->cMsgType, wire); HL7Free(pMsg); } close(dtmfd); printf("\n");fflush(stdout); } static void PrtHL7Msg(char *string, int iStringLen) { int i; printf("\n\n"); fflush(stdout); for (i=0; (i