Home > Software design >  Unable to decode serial port data
Unable to decode serial port data

Time:11-18

I tried to receive data from medical 'Mindray bs 200' device through serial port. data received but is unreadable. Unable to find the kind of data encryption.

Here is the code that receives the data

Private Sub comPort_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles comPort.DataReceived
    Dim str As String = ""
    If e.EventType = SerialData.Chars Then
        Do
            Dim bytecount As Integer = comPort.BytesToRead
            If bytecount = 0 Then
                Exit Do
            End If
            Dim byteBuffer(bytecount) As Byte

            comPort.Encoding = Encoding.GetEncoding(28591)
            ' comPort.Encoding = Encoding.GetEncoding(1252)
            'comPort.Encoding = Encoding.GetEncoding("Windows-1252")
            comPort.Read(byteBuffer, 0, bytecount)
            str = str & System.Text.Encoding.ASCII.GetString(byteBuffer, 0, 1)
            ' The str looks like
        Loop
    End If
    RaiseEvent ScanDataRecieved(str)
End Sub

Here is the data received ??????????????????????????????????????????????????????????????????????????????????????????????????????????????????X???????????????????

CodePudding user response:

While not for this particular device, enter image description here

Form1.vb

Note: In the code below, you'll need to update helper.Connect("COM1") with the correct COM port.

Public Class Form1
    Private helper As HelperSerialPort = New HelperSerialPort()
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Disconnect()
        If helper IsNot Nothing Then
            'unsubscribe from event(s)
            RemoveHandler helper.DataReceived, AddressOf Helper_DataReceived
            RemoveHandler helper.ErrorReceived, AddressOf Helper_ErrorReceived

            helper.Dispose()
            helper = Nothing
        End If
    End Sub

    Private Sub btnConnectDisconnect_Click(sender As Object, e As EventArgs) Handles btnConnectDisconnect.Click
        If helper Is Nothing Then
            'create new instance
            helper = New HelperSerialPort()
        End If

        If btnConnectDisconnect.Text = "Connect" Then
            'clear existing data
            RichTextBox1.Clear()

            'ToDo: change to your desired COM port
            helper.Connect("COM1")

            'subscribe to event(s)
            AddHandler helper.DataReceived, AddressOf Helper_DataReceived
            AddHandler helper.ErrorReceived, AddressOf Helper_ErrorReceived

            'set text
            btnConnectDisconnect.Text = "Disconnect"
            btnConnectDisconnect.Refresh()
        Else
            Disconnect()

            'set text
            btnConnectDisconnect.Text = "Connect"
            btnConnectDisconnect.Refresh()
        End If
    End Sub

    Private Sub Helper_DataReceived(ByVal sender As Object, ByVal data As String)
        'ToDo: add desired code
        System.Diagnostics.Debug.WriteLine(DateTime.Now.ToString("HH:mm:ss") & " - Helper_DataReceived: " & data)

        'append data to RichTextBox
        RichTextBox1.Invoke(New MethodInvoker(Sub()
                                                  RichTextBox1.AppendText(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff") & " - " & data)
                                              End Sub))
    End Sub

    Private Sub Helper_ErrorReceived(ByVal sender As Object, ByVal errMsg As String)
        'ToDo: add desired code
        System.Diagnostics.Debug.WriteLine(DateTime.Now.ToString("HH:mm:ss") & " - Helper_ErrorReceived: " & errMsg)

        'append error message to RichTextBox
        RichTextBox1.Invoke(New MethodInvoker(Sub()
                                                  RichTextBox1.AppendText(errMsg)
                                              End Sub))
    End Sub

    Private Sub Form1_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
        Disconnect()
    End Sub
End Class

Resources:

  • Related