Author Topic: Serial port monitor  (Read 302 times)

mabdelouahab

  • Member
  • ***
  • Posts: 428
Serial port monitor
« on: August 10, 2019, 09:54:59 PM »

this is BA-88A, Semi-Auto Chemistry Analyzer.
The owner of this machine asked me to create a software that transfered data from this machine to the computer
This machine uses the serial port (RS-232) to do this job , in the form of a char array (HL7)


I completed the work easily, to convert the results of each patient individually
But the problem is that the machine manufacturer, states that the data can be transferred to several patients at once.
We can select  data for multiple patients at once

but during conversion, only one result reaches
I thought the mistake was in the program I made.
I've tried several programs ready from the Internet, but only one result at a time.

Apparently, the machine stays in a state of waiting for some time, then a message appears error: transfer data failed
Maybe the machine is waiting for a message from the data receiver, to complete the conversion, maybe

Any old ideas, suggestions, or experience?

Code: [Select]
ComThread1 PROC ofs___
local L_handle :DWORD                             
local L_NumberOfBytesRead :DWORD                         
local lpEvtMask :DWORD                                   
local lpError :DWORD                             
local W_ReadBuf :DWORD                                 
local L_bIndexWR :DWORD     
local L_ReadBuffer_ [1024]:DWORD     
local __r :DWORD     


MOV EAX,ofs___
MOV EAX,DWORD PTR [EAX+4]
mov L_handle, EAX                                    ; save the handle

invoke SetCommMask, L_handle, EV_RING or EV_BREAK or EV_CTS or EV_DSR or EV_DSR or EV_RLSD or EV_RXFLAG or EV_TXEMPTY or EV_RXCHAR OR EV_ERR
;------------------------------------------------------
MOV ECX,L_handle ; get the handle

_WHILE:                                                 
invoke WaitCommEvent, L_handle, ADDR lpEvtMask, NULL     
TEST EAX,EAX
JNZ @F
invoke crt_printf,cfm$("WaitCommEvent failed")
jmp ExitComThread
@@:

.IF lpEvtMask == EV_ERR                               
invoke crt_printf,cfm$(" ..Error.. ")
invoke ClearCommError, L_handle, ADDR lpError, NULL
.elseif lpEvtMask ==    EV_RING                               
invoke crt_printf,cfm$(" ring Event"); }
.elseif lpEvtMask ==    EV_BREAK                               
    invoke crt_printf,cfm$(" break Event"); }
.elseif lpEvtMask ==    EV_CTS                               
    invoke crt_printf,cfm$(" cts Event"); }
.elseif lpEvtMask ==    EV_DSR                               
    invoke crt_printf,cfm$(" dsr Event"); }
.elseif lpEvtMask ==    EV_RLSD                               
    invoke crt_printf,cfm$(" rlsd Event"); }
.elseif lpEvtMask ==    EV_RXFLAG                               
    invoke crt_printf,cfm$(" rxflag Event"); }
.elseif lpEvtMask ==    EV_TXEMPTY                               
    invoke crt_printf,cfm$(" txempty Event"); }
.elseif lpEvtMask == EV_RXCHAR             
.WHILE TRUE                       
MOV W_ReadBuf,0
invoke ReadFile, L_handle, ADDR W_ReadBuf,1, ADDR L_NumberOfBytesRead, NULL         ; Read in one DWORD from the serial port
mov eax,L_NumberOfBytesRead
.BREAK .IF L_NumberOfBytesRead == 0                       
.if  W_ReadBuf==01CH;; end of MSH message
mov __r,1
.endif
    lea eax,L_ReadBuffer_
    lea ebx,W_ReadBuf
    add eax,L_bIndexWR
    mov cl,byte ptr [ebx]
.if  cl==07CH ; Field
mov cl,0
.endif
    mov byte ptr [eax],cl
    inc L_bIndexWR
    invoke crt_printf,cfm$(",0%XH"), W_ReadBuf
.ENDW
  .if __r ==1 ; end of MSH message
  mov __r,0
  invoke ParceBuffer,addr L_ReadBuffer_
.endif
.else
  invoke crt_printf,cfm$(" else  Event %d ") ,lpEvtMask
.ENDIF

JMP _WHILE

ExitComThread:
invoke CloseHandle, L_handle    ; close this  port
MOV EAX,ofs___
MOV DWORD PTR [EAX+4],0 ; clear handle

ret

ComThread1 endp
Hexadecimal message:

Code: [Select]
;BH,4DH,53H,48H,7CH,5EH,7EH,5CH,26H,7CH,7CH,7CH,7CH,7CH,32H
;,30H,31H,39H,30H,38H,30H,37H,30H,34H,35H,30H,32H,37H,7CH,7CH,4FH,52H,55H,5EH,52H
;,30H,31H,7CH,34H,31H,7CH,50H,7CH,32H,2EH,33H,2EH,31H,7CH,7CH,7CH,7CH,30H,7CH,
;7CH,41H,53H,43H,49H,49H,7CH,7CH,7CH,DH,50H,49H,44H,7CH,34H,31H,7CH,7CH,7CH,7CH,7CH
;,7CH,32H,30H,31H,39H,30H,31H,30H,31H,30H,30H,30H,30H,30H,30H,7CH,4FH,7CH,7CH,7CH,7CH
;,7CH,7CH,7CH,7CH,7CH,7CH,7CH,7CH,7CH,7CH,7CH,7CH,7CH,7CH,7CH,7CH,7CH,7CH,7CH
;,DH,4FH,42H,52H,7CH,34H,31H,7CH,7CH,35H,7CH,5EH,7CH,4EH,7CH,7CH,32H,30H,31H,39H
;,30H,38H,30H,36H,30H,30H,30H,30H,30H,30H,7CH,7CH,7CH,7CH,7CH,7CH,7CH,32H,30H,31H
;,39H,30H,38H,30H,36H,30H,30H,30H,30H,30H,30H,7CH,7CH,7CH,7CH,7CH,7CH,7CH,7CH,7CH
;,7CH,7CH,7CH,7CH,7CH,7CH,7CH,7CH,7CH,7CH,7CH,7CH,7CH,7CH,7CH,7CH,7CH,7CH,7CH,7CH
;,7CH,7CH,7CH,7CH,7CH,DH,4FH,42H,58H,7CH,31H,7CH,4EH,4DH,7CH,7CH,43H,52H,45H,41H,
;7CH,31H,32H,2EH,32H,31H,30H,30H,30H,30H,7CH,31H,7CH,7CH,4EH,7CH,7CH,7CH,46H,7CH,
;7CH,31H,32H,2EH,32H,31H,30H,30H,30H,30H,7CH,32H,30H,31H,39H,30H,38H,30H,36H,30H,
;30H,30H,30H,30H,30H,7CH,7CH,7CH,7CH,DH,4FH,42H,58H,7CH,32H,7CH,4EH,4DH,7CH,7CH,50H
;,52H,4FH,20H,20H,32H,34H,68H,7CH,39H,37H,2EH,30H,30H,30H,30H,30H,30H,7CH,31H,7CH
;,7CH,4EH,7CH,7CH,7CH,46H,7CH,7CH,39H,37H,2EH,30H,30H,30H,30H,30H,30H,7CH,32H,30H
;,31H,39H,30H,38H,30H,36H,30H,30H,30H,30H,30H,30H,7CH,7CH,7CH,7CH,DH,1CH,DH

ASCII message:

?MSH|^~\&|||||20190806193603||ORU^R01|9|P|2.3.1||||0||ASCII
|||?PID|9||||||20190101000000|O|||||||||||||||||||||||?OBR|9||2|^|N||20190806000
000|||||||20190806000000||||||||||||||||||||||||||||||||||?OBX|1|NM||AST|47.0000
00|16||N|||F||47.000000|20190806000000||||?OBX|2|NM||ALP|133.000000|16||N|||F||1
33.000000|20190806000000||||?OBX|3|NM||DBIL|5.800000|1||N|||F||5.800000|20190806
000000||||?OBX|4|NM||CREA|13.820000|1||N|||F||13.820000|20190806000000||||?OBX|5
|NM||PHOS|43.370000|1||N|||F||43.370000|20190806000000||||?OBX|6|NM||PRO  24h|-5
4.000000|1||N|||F||-54.000000|20190806000000||||???

Siekmanski

  • Member
  • *****
  • Posts: 1862
Re: Serial port monitor
« Reply #1 on: August 10, 2019, 11:21:08 PM »
AFAIK, the RS-232 protocol is communication between 1 host and 1 client.
Both have 1 receive and 1 transmit port.
It doesn't support multiple clients like TCP/IP socket connections.

Or you need as many RS-232 port connections as there are clients and communicate separately with them.

Maybe it is possible if all client RX data lines are connected with the TX data line of the host?
Creative coders use backward thinking techniques as a strategy.

Siekmanski

  • Member
  • *****
  • Posts: 1862
Re: Serial port monitor
« Reply #2 on: August 10, 2019, 11:58:21 PM »
Maybe I didn't understand this line: "the data can be transferred to several patients at once"
Creative coders use backward thinking techniques as a strategy.

K_F

  • Member
  • *****
  • Posts: 1502
  • Anybody out there?
Re: Serial port monitor
« Reply #3 on: August 11, 2019, 12:21:46 AM »
HIS is 'Hospital Information System' and it usually operates over a normal 801 network (or other types for that matter).
If that device only has RS232 on it you'll need a network to RS232 converter, hardware piece.

This is the Physical Protocol difference, whereas the TCP/IP protocol stack can remain the same on both devices - the trick is the driver software for the RS232 side. You'd be lucky to get off the shelf hardware that is flawless.

The next problem would be the difference in speed between the network (anywhere from 10Mbit to 1Gbit) and RS232 (max of 115KBaud (KBit)).
If this is not taken into account you could end up with timeout errors on the faster side of the network, when in fact there is no error.

;)

'Sire, Sire!... the peasants are Revolting !!!'
'Yes, they are.. aren't they....'

AW

  • Member
  • *****
  • Posts: 2240
  • Let's Make ASM Great Again!
Re: Serial port monitor
« Reply #4 on: August 11, 2019, 01:53:08 AM »
I don't know if this will help, but one point is clear to me: 1 (one) HL7 message is enough to send either 1 patient data or multiple patients data.
So it not a question of multiple clients from 1 host. In both cases there is 1 (one) host, the medical device, and 1 (one) client, 1 computer. In other words, no RS232 multiplexing whatsoever.

So, you will have to see how the HL7 message is built. From a quick look at the manual I noticed that the HL7 message is made up of segments. "Some patient demographics are carried in the PID segment and PV1 segment", they say at one point.
So, all is there to solve the puzzle, it appears. Good luck then.  :thumbsup:

mabdelouahab

  • Member
  • ***
  • Posts: 428
Re: Serial port monitor
« Reply #5 on: August 11, 2019, 02:41:25 AM »
Maybe I didn't understand this line: "the data can be transferred to several patients at once"

error in translation, I mean that several patient's information can be collected, Then send them in a single SMH message

HIS is 'Hospital Information System' and it usually operates over a normal 801 network (or other types for that matter).
If that device only has RS232 on it you'll need a network to RS232 converter, hardware piece.

This is the Physical Protocol difference, whereas the TCP/IP protocol stack can remain the same on both devices - the trick is the driver software for the RS232 side. You'd be lucky to get off the shelf hardware that is flawless.

The next problem would be the difference in speed between the network (anywhere from 10Mbit to 1Gbit) and RS232 (max of 115KBaud (KBit)).
If this is not taken into account you could end up with timeout errors on the faster side of the network, when in fact there is no error.

Is not HIS, is Laboratory information system (LIS) or laboratory management system (LMS).

I don't know if this will help, but one point is clear to me: 1 (one) HL7 message is enough to send either 1 patient data or multiple patients data.
So it not a question of multiple clients from 1 host. In both cases there is 1 (one) host, the medical device, and 1 (one) client, 1 computer. In other words, no RS232 multiplexing whatsoever.

So, you will have to see how the HL7 message is built. From a quick look at the manual I noticed that the HL7 message is made up of segments. "Some patient demographics are carried in the PID segment and PV1 segment", they say at one point.
So, all is there to solve the puzzle, it appears. Good luck then.  :thumbsup:

If I select only one patient , Or select several patient , the result is the same, one message to one patient containing one PID Segment and one OBR Segment

?MSH|^~\&|||||20190806192819||ORU^R01|7|P|2.3.1||||0||ASCII
|||?PID|7||||||20190101000000|O|||||||||||||||||||||||?OBR|7||2|^|N||20190806000
000|||||||20190806000000||||||||||||||||||||||||||||||||||?OBX|1|NM||AST|47.0000
00|16||N|||F||47.000000|20190806000000||||?OBX|2|NM||ALP|133.000000|16||N|||F||1
33.000000|20190806000000||||?OBX|3|NM||DBIL|5.800000|1||N|||F||5.800000|20190806
000000||||?OBX|4|NM||CREA|13.820000|1||N|||F||13.820000|20190806000000||||?OBX|5
|NM||PHOS|43.370000|1||N|||F||43.370000|20190806000000||||?OBX|6|NM||PRO  24h|-5
4.000000|1||N|||F||-54.000000|20190806000000||||???

AW

  • Member
  • *****
  • Posts: 2240
  • Let's Make ASM Great Again!
Re: Serial port monitor
« Reply #6 on: August 11, 2019, 05:12:37 AM »
I am not seeing any patient at all, it appears they are using the same PID for everyone.
PID|7||||||20190101000000|O|||||||||||||||||||||||?
This is someone with no name born in 1st January 2019.

Compare with some exemple from the manual:
PID|||M1015_00010||John^||20091112|M|||^^^^||| <CR>
This is John, born 12th November 2009, Patient Medical Record Number M1015_00010, Male


mabdelouahab

  • Member
  • ***
  • Posts: 428
Re: Serial port monitor
« Reply #7 on: August 11, 2019, 05:25:50 AM »
I am not seeing any patient at all, it appears they are using the same PID for everyone.
PID|7||||||20190101000000|O|||||||||||||||||||||||?
This is someone with no name born in 1st January 2019.

Compare with some exemple from the manual:
PID|||M1015_00010||John^||20091112|M|||^^^^||| <CR>
This is John, born 12th November 2009, Patient Medical Record Number M1015_00010, Male


:biggrin:
This is just an test, I did it, I didn't tire myself filling the fields

AW

  • Member
  • *****
  • Posts: 2240
  • Let's Make ASM Great Again!
Re: Serial port monitor
« Reply #8 on: August 11, 2019, 05:33:38 AM »
I see, you want different patients but you are not filling the patient data fields.  :rolleyes:

mabdelouahab

  • Member
  • ***
  • Posts: 428
Re: Serial port monitor
« Reply #9 on: August 13, 2019, 07:23:44 AM »
I found it  :eusa_dance:

The solution is called: Acknowledgement message (ACK Message), we must tell the machine that the message was received without error
Is a MSH message  contains MSA segment with
- field : Acknowledgement Code =(•  AA: Application Accept (positive acknowledgement) •  AE: Application Error (negative acknowledgement) •  AR: Application Reject (negative acknowledgement))
- field :received message control ID


Received Message: From the Machine
♂MSH|^~\&|||||20190806044228||ORU^R01|18    |P|2.3.1||||0||ASCII|||......

ACK Message: From PC
♂MSH|^~\&|||||20190812204812||ACK^R01|ACK001|P|2.3.1||||0||ASCII|||
MSA|AA|18♪∟♪


Now everything is working properly