title: Multiple serial port communication in VB.net
body: <p>I'm currently developing a Laboratory Information System which communicates with an Analyzer/Lab machine, so far I'm not having any problem with the communication from Analyzer to Computer (1-1) ratio. My problem is that I tried to connect multiple Analyzer into 1 Computer (connected to PC with USB-4 Ports RS232 Converter) and it messed up the results all results have been mixed up. </p>
  
  <p>My question is how can I determined which Port sends a particular data so that I can separate and segregate it into each corresponding tables.</p>
  
  <p>Here's my code in receiving:</p>
  
  <pre><code>Public Sub ReadData(ByVal sender As Object, ByVal COmPOrt As SerialPort)
      COmPOrt = DirectCast(sender, SerialPort)
      Dim received As String
      RXString = COmPOrt.ReadExisting
  
      received = Nothing
      received = RXString
  
      If (received = ack) Then
          wh.Set()
      ElseIf (received = enq) Then
          Acknowledge(COmPOrt)
      ElseIf (received = eot) Then
          COmPOrt.DiscardInBuffer()
          received = Nothing
      Else
          DataReceived = received(2).ToString()
          With My.Settings
              Select Case (DataReceived)
                  Case "H"
                      Acknowledge(COmPOrt)
                  Case "P"
                      PatientData1 = received.Split("|").GetValue(.PatientID).ToString().TrimEnd &amp; vbCrLf
                      PatientData2 = received.Split("|").GetValue(.PatientName).ToString().TrimEnd &amp; vbCrLf
                      PatientData2 = PatientData2.Replace("^", ", ").ToString().Trim
                      Acknowledge(COmPOrt)
                  Case "O"
                      OrderSampleID = received.Split("|").GetValue(.SampleID).ToString().TrimEnd &amp; vbCrLf
                      OrderData1 = StrConv(OrderSampleID.Split("^").GetValue(0).ToString.Trim, VbStrConv.Uppercase)
                      FinalOrderID = OrderData1
                      Acknowledge(COmPOrt)
                      Me.Invoke(New MethodInvoker(AddressOf IdentifyOrder))
                  Case "R"
                      Data2 = Replace(received.Split("|").GetValue(.Parameter).ToString(), "^", " ").TrimStart() &amp; vbCrLf
                      FinalData2 = Data2.Split(" "c)(0)
                      Data3 = received.Split("|").GetValue(.Measurement).ToString() &amp; vbCrLf
                      Acknowledge(COmPOrt)
                      Me.Invoke(New MethodInvoker(AddressOf DisplayResult))
                  Case "Q"
                      Acknowledge(COmPOrt)
                  Case "C"
                      Acknowledge(COmPOrt)
                  Case "L"
                      Acknowledge(COmPOrt)
                      Me.Invoke(New MethodInvoker(AddressOf LoadResult))
              End Select
          End With
      End If
  End Sub
  
  Public Sub OpenSP()
      CloseSP()
      For Each PortName As String In SerialPort.GetPortNames
          Try
              Dim NewSP As New SerialPort(PortName)
              NewSP.BaudRate = 9600
              NewSP.StopBits = 1
              NewSP.DataBits = 8
              NewSP.Handshake = Handshake.None
              NewSP.Parity = Parity.None
              NewSP.Open()
              If NewSP.IsOpen Then
                  AddHandler NewSP.DataReceived, AddressOf frmSystemTray.COMPort_DataReceived
                  COMPort.Add(NewSP)
              End If
          Catch ex As Exception
              MessageBox.Show(ex.Message, "Communication Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
              Exit Sub
          End Try
      Next
  End Sub
  </code></pre>
  
  <p>Below I provided the received data from Analyzer.</p>
  
  <pre><code>1H|\^&amp;|||OPTI Medical OPTILION^2.00.0016^1601||||||Meas|P|2.2|2019011214426A
  2P|1||ROSARIO|||||U17
  3O|1||MEASUREMENT^1141||||||||||||Blood^Serum|||719815||||201901121442CA
  4C|1|I|Na+  under  135 (Ref.Lim)^iCa  under 1.15 (Ref.Lim)^|G11
  5R|1|^^^Na^M^^|135|mmol/L|135 to 145|L||||||201803221320|OPTI Medical OPTILION^2.00.0016^1601F9
  6R|2|^^^K^M^^|4.2|mmol/L|3.5 to 5.1|||||||201803221320|OPTI Medical OPTILION^2.00.0016^16013D
  7R|3|^^^Cl^M^^|109|mmol/L|95 to 115|||||||201803221320|OPTI Medical OPTILION^2.00.0016^160184
  0R|4|^^^iCa^M^^|1.13|mmol/L|1.15 to 1.33|L||||||201803221320|OPTI Medical OPTILION^2.00.0016^1601D6
  1R|5|^^^pH^M^^|7.41||7.20 to 7.60|||||||201803221320|OPTI Medical OPTILION^2.00.0016^160116
  2R|6|^^^nCa^C^^|1.13|mmol/L|||||||||OPTI Medical OPTI LION^2.00.0016^160184
  3R|7|^^^AccNum^S^^||||||||||201803221320|OPTI Medical OPTI LION^2.00.0016^160120
  4R|8|^^^Temp^S^^|37.0|?C||||||||201803221320|OPTI Medical OPTILION^2.00.0016^160184
  5L|1|N08
  </code></pre>
  
  <p>And here's what I received when receiving from 2 analyzer simultaneously</p>
  
  <pre><code>1H|\^&amp;|||OPTI Medical OPTI LION^2.00.0016^1601||||||Meas|P|2.2|20190112145069
  2P|1||ROSARIO|||||U17
  3O|1||MEASUREMENT^1141||||||||||||Blood^Serum|||719815||||201901121450C9
  4C|1|I|Na+  under  135 (Ref.Lim)^iCa  under 1.15 (Ref.Lim)^|G11
  1H|\^&amp;||||||||||P|E1394-97|2019011214512277
  2P|1||212059||DALUSUNG, ALYSSA DAN||19960608|F32
  5R|1|^^^Na^M^^|135|mmol/L|135 to 145|L||||||201803221320|OPTI Medical OPTI LION^2.00.0016^1601F9
  3O|1|18106789130||||20180801011651|20180801001700||||||||1||||||||||F10
  4R|1|^^^25^GluP|5.98|4||H||F|||20180801011719CF
  5C|1|I|Flag^NORM_RANGEH|I63
  6R|2|^^^K^M^^|4.2|mmol/L|3.5 to 5.1|||||||201803221320|OPTI Medical OPTI LION^2.00.0016^16013D
  6R|2|^^^211^CREA3|72.35|6||||F|||20180801011655B9
  7L|1|N0A
  7R|3|^^^Cl^M^^|109|mmol/L|95 to 115|||||||201803221320|OPTI Medical OPTI LION^2.00.0016^160184
  0R|4|^^^iCa^M^^|1.13|mmol/L|1.15 to 1.33|L||||||201803221320|OPTI Medical OPTI LION^2.00.0016^1601D6
  1R|5|^^^pH^M^^|7.41||7.20 to 7.60|||||||201803221320|OPTI Medical OPTI LION^2.00.0016^160116
  2R|6|^^^nCa^C^^|1.13|mmol/L|||||||||OPTI Medical OPTI LION^2.00.0016^160184
  3R|7|^^^AccNum^S^^||||||||||201803221320|OPTI Medical OPTI LION^2.00.0016^160120
  4R|8|^^^Temp^S^^|37.0|?C||||||||201803221320|OPTI Medical OPTI LION^2.00.0016^160184
  5L|1|N08
  </code></pre>
  
  <p>Expected output should be:</p>
  
  <pre><code>1H|\^&amp;|||OPTI Medical OPTI LION^2.00.0016^1601||||||Meas|P|2.2|2019011214526B
  2P|1||ROSARIO|||||U17
  3O|1||MEASUREMENT^1141||||||||||||Blood^Serum|||719815||||201901121452CB
  4C|1|I|Na+  under  135 (Ref.Lim)^iCa  under 1.15 (Ref.Lim)^|G11
  5R|1|^^^Na^M^^|135|mmol/L|135 to 145|L||||||201803221320|OPTI Medical OPTI LION^2.00.0016^1601F9
  6R|2|^^^K^M^^|4.2|mmol/L|3.5 to 5.1|||||||201803221320|OPTI Medical OPTI LION^2.00.0016^16013D
  7R|3|^^^Cl^M^^|109|mmol/L|95 to 115|||||||201803221320|OPTI Medical OPTI LION^2.00.0016^160184
  0R|4|^^^iCa^M^^|1.13|mmol/L|1.15 to 1.33|L||||||201803221320|OPTI Medical OPTI LION^2.00.0016^1601D6
  1R|5|^^^pH^M^^|7.41||7.20 to 7.60|||||||201803221320|OPTI Medical OPTI LION^2.00.0016^160116
  2R|6|^^^nCa^C^^|1.13|mmol/L|||||||||OPTI Medical OPTI LION^2.00.0016^160184
  3R|7|^^^AccNum^S^^||||||||||201803221320|OPTI Medical OPTI LION^2.00.0016^160120
  4R|8|^^^Temp^S^^|37.0|?C||||||||201803221320|OPTI Medical OPTI LION^2.00.0016^160184
  5L|1|N08
  
  1H|\^&amp;||||||||||P|E1394-97|201901121453077C
  2P|1||212059||DALUSUNG, ALYSSA DAN||19960608|F32
  3O|1|18106789130||||20180801011651|20180801001700||||||||1||||||||||F10
  4R|1|^^^25^GluP|5.98|4||H||F|||20180801011719CF
  5C|1|I|Flag^NORM_RANGEH|I63
  6R|2|^^^211^CREA3|72.35|6||||F|||20180801011655B9
  7L|1|N0A
  </code></pre>