多串口通信VB.net

时间:2019-01-12 07:27:28

标签: vb.net

大家好,

我目前正在开发一个与分析仪/实验室机器通信的实验室信息系统,到目前为止,我对从分析仪到计算机(1-1)比率的通信没有任何问题。我尝试将多个分析仪连接到1台计算机(通过USB-4端口RS232转换器连接到PC)时遇到了我的问题,它弄乱了所有结果,结果混在一起。

我的问题是我如何确定哪个端口发送特定数据,以便可以将其分离并隔离到每个对应的表中。

这是我的接收代码。

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 & vbCrLf
                    PatientData2 = received.Split("|").GetValue(.PatientName).ToString().TrimEnd & vbCrLf
                    PatientData2 = PatientData2.Replace("^", ", ").ToString().Trim
                    Acknowledge(COmPOrt)
                Case "O"
                    OrderSampleID = received.Split("|").GetValue(.SampleID).ToString().TrimEnd & 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() & vbCrLf
                    FinalData2 = Data2.Split(" "c)(0)
                    Data3 = received.Split("|").GetValue(.Measurement).ToString() & 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

下面我提供了从Analyzer接收到的数据。

1H|\^&|||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

这是我同时从2个分析仪接收到的信息

1H|\^&|||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|\^&||||||||||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

应该是预期的输出。

1H|\^&|||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|\^&||||||||||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

谢谢大家,天哪!

1 个答案:

答案 0 :(得分:0)

最后,经过数天的重新编码,我可以按需运行它。

这是我的关于从USB-4Ports RS232转换器接收数据的多个ComPort的代码。

Dim Ports As New List(Of IO.Ports.SerialPort)

For x As Integer = 0 To My.Computer.Ports.SerialPortNames.Count - 1
            Dim a As Integer = x
            Dim PatientID(a) As String
            Dim PatientName(a) As String
            Dim OrderID(a) As String
            Dim ResultName(a) As String
            Dim ResultValue(a) As String

            Ports.Add(New SerialPort)

            Ports(a).PortName = My.Computer.Ports.SerialPortNames(a)
            Ports(a).BaudRate = 9600
            Ports(a).Open()
            AddHandler Ports(a).DataReceived, Sub(obj As Object, evtArgs As SerialDataReceivedEventArgs)

                                                  Dim str(a) As String
                                                  str(a) = Ports(a).ReadExisting

                                                  If Not str(a) = "" Then
                                                      Dim received(a) As String
                                                      Dim DataReceived(a) As Char

                                                      Ports(a) = CType(obj, SerialPort)
                                                      Ports(a).RtsEnable = True
                                                      Ports(a).DtrEnable = True
                                                      Ports(a).ReadTimeout = 10000

                                                      received(a) = Nothing
                                                      received(a) = str(a)

                                                      If (received(a) = ack) Then
                                                          wh.Set()
                                                      ElseIf (received(a) = enq) Then
                                                          Acknowledge(Ports(a))
                                                      ElseIf (received(a) = eot) Then
                                                          Ports(a).DiscardInBuffer()
                                                          received(a) = Nothing
                                                      Else
                                                          DataReceived(a) = received(a)(2).ToString()
                                                          With My.Settings
                                                              Select Case (DataReceived(a))
                                                                  Case "H"
                                                                      Acknowledge(Ports(a))
                                                                  Case "P"
                                                                      PatientID(a) = received(a).Split("|").GetValue(.PatientID).ToString().Trim & vbCrLf
                                                                      PatientName(a) = received(a).Split("|").GetValue(.PatientName).ToString().Trim & vbCrLf
                                                                      PatientName(a) = PatientName(a).Replace("^", ", ").ToString().Trim
                                                                      Acknowledge(Ports(a))
                                                                  Case "O"
                                                                      OrderID(a) = received(a).Split("|").GetValue(.SampleID).ToString().Trim & vbCrLf
                                                                      OrderID(a) = StrConv(OrderID(a).Split("^").GetValue(0).ToString.Trim, VbStrConv.Uppercase)
                                                                      Acknowledge(Ports(a))
                                                                      Me.Invoke(Sub() IdentifyOrder(OrderID(a), PatientID(a), PatientName(a)))
                                                                  Case "R"
                                                                      ResultName(a) = Replace(received(a).Split("|").GetValue(.Parameter).ToString(), "^", " ").TrimStart() & vbCrLf
                                                                      ResultName(a) = ResultName(a).Split(" "c)(0)
                                                                      ResultValue(a) = received(a).Split("|").GetValue(.Measurement).ToString() & vbCrLf
                                                                      Acknowledge(Ports(a))

                                                                      Me.Invoke(Sub() DisplayResult(OrderID(a), ResultName(a), ResultValue(a)))
                                                                  Case "Q"
                                                                      Acknowledge(Ports(a))
                                                                  Case "C"
                                                                      Acknowledge(Ports(a))
                                                                  Case "L"
                                                                      Acknowledge(Ports(a))
                                                                      Me.Invoke(New MethodInvoker(AddressOf LoadResult))
                                                              End Select
                                                          End With
                                                      End If
                                                  End If
                                              End Sub
        Next

我希望这可以帮助其他人像我一样遇到同样的问题。

更多建议,请在下面发表评论以改善此答案。

谢谢!