vb.net SerialPort编写和接收

时间:2017-01-31 08:33:03

标签: vb.net

我在阅读串口通信的结果时遇到了问题。 如果有多个功能,它们在我的设备上执行不同的命令,并且以稍微不同的方式接收和处理发送的结果。

如果我一次性调用这些功能,它就不能100%可靠地运行。如果我只调用其中一个功能,那么所有功能都能完美运行。我假设,为慢速serialPort通信认为有点快,所以有些功能在调用所有“一次性”时不会得到propper结果

调用3个函数的代码(不同位置的错误:“(数组)范围内的索引”或“CRC错误”)

Private Sub btn_initialize_Click(sender As Object, e As EventArgs) Handles btn_initialize.Click

    Dim actual_mw As Double
    Dim attenuated As Integer

    'open (virtual)COMport
    If rfid.init_serialport(cb_com.SelectedItem.ToString) = True Then
        flp_data.Visible = True

        lbl_snr_data.Text = rfid.get_serial_number()

        lbl_type_data.Text = rfid.get_reader_type

        Dim collection As MatchCollection = Regex.Matches(rfid.get_attennuation, "\d+")
        attenuated = collection(1).Value
        If attenuated = 0 Then
            lbl_leistung_data.Text = "100mW / 100mW"
        Else
            actual_mw = 100 - (10 ^ (attenuated / 10))
            lbl_leistung_data.Text = Math.Round(actual_mw, 2) & "mW / 100mW"
        End If
    Else
        MsgBox("Error! COM-Schnittstelle konnte nicht initialisiert werden!", MsgBoxStyle.Critical, "Error")
    End If

End Sub

初始化和处置SerialPort

Public Class RFE
    Private RF_CHECKSUM As Byte
    Private device_output() As Byte
    Private data() As Byte
    Private device_input() As Byte
    Private sp As SerialPort

    Public Function init_serialport(portName As String) As Boolean

        Try

            sp = New SerialPort

            sp.PortName = portName
            sp.BaudRate = 9600
            sp.Parity = Parity.None
            sp.DataBits = 8
            sp.StopBits = StopBits.One
            sp.Handshake = Handshake.None
            sp.ReadTimeout = 500
            sp.WriteTimeout = 500

            sp.Open()

            If sp.IsOpen Then
                Return True
            Else
                Return False
            End If

        Catch ex As Exception

            MsgBox(ex.Message)

        End Try

    End Function
    Public Function disp_serialport()

        Try

            sp.Dispose()

        Catch ex As Exception

            MsgBox(ex.Message)

        End Try

    End Function

功能

Public Function get_serial_number() As String

        Try

            Dim serial_number As String


            ReDim data(8)
            data(0) = RF_START_BYTE0
            data(1) = RF_START_BYTE1
            data(2) = RF_START_BYTE2
            data(3) = RF_COMMAND_START_BYTE
            data(4) = RF_READER_COMMON
            data(5) = RF_GET_SERIAL_NUMBER
            data(6) = RF_LENGTH_START_BYTE
            data(7) = &H0
            data(8) = RF_CHECKSUM_START_BYTE

            RF_CHECKSUM = Hex(calc_xor(data, 1))


            device_input = data
            Array.Resize(device_input, data.Length + 1)
            device_input(9) = "&H" & RF_CHECKSUM

            sp.Write(device_input, 0, device_input.Length)

            ReDim device_output(sp.BytesToRead)

            sp.Read(device_output, 0, device_output.Length - 1)


            If calc_xor(device_output, 3) <> device_output(14) Then
                MsgBox("CRC incorrect!", MsgBoxStyle.Critical, "Error")
                Form1.Close()
            End If


            serial_number = Hex(device_output(9)).PadLeft(2, "0") & "-" & Hex(device_output(10)).PadLeft(2, "0") & "-" & Hex(device_output(11)).PadLeft(2, "0") & "-" _
                & Hex(device_output(12)).PadLeft(2, "0")



            device_output = Nothing
            data = Nothing
            RF_CHECKSUM = Nothing
            sp.DiscardInBuffer()
            sp.DiscardOutBuffer()

            Return serial_number

        Catch ex As Exception

            MsgBox(ex.Message)

        End Try

    End Function

其他函数结构相同,只发送字节和处理结果略有不同。

正如我所说,我认为问题是信号的交易(可能在两个方向上),因此SerialPort和/或设备无法快速处理事情。

我需要做什么才能确保/顺序地进行通信?

也许DataReceivedHandler可以修复这个问题?但是对于我调用的每个函数,结果的处理都是不同的,我如何在DataReceivedHandler中实现它(如果Handler可以修复问题)?

谢谢大家!

0 个答案:

没有答案
相关问题