1

我正在创建一个 vb.net windows 应用程序来做很多事情,包括从 EEProm 下载一堆数据,然后将其保存到文件中。我正在使用 DataReceived 例程来捕获传入的数据,然后尝试将每 16 个字节写入一个二进制文件。我可以正常写入文件,但是我似乎遇到了我不太熟悉的交叉线程等问题。进度条工作正常,所以我现在需要做的就是将 16 个字节写入文件。

Private Sub btnTestCom_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTestCom.Click

    Dim BaseFolder As String = "c:\"
    CurrentFileName = BaseFolder + "byteArray1.dat"
    Dim fStream As New FileStream(CurrentFileName, FileMode.Truncate, FileAccess.Write, FileShare.ReadWrite)
    fStream.Close()

    uart_operation = 2
    DayNum = 0

    If mySerialPort.IsOpen() Then mySerialPort.Close()

    mySerialPort.BaudRate = 256000
    mySerialPort.Parity = Parity.None
    mySerialPort.StopBits = StopBits.One
    mySerialPort.DataBits = 8
    mySerialPort.Handshake = Handshake.None
    mySerialPort.Open()

    AddHandler mySerialPort.DataReceived, AddressOf mySerialPort_DataReceived

    'This is my start byte to the microprocessor
    Dim b() As Byte = New Byte() {254}
    mySerialPort.Write(b, 0, b.Length)
    mySerialPort.BaseStream.Flush()

End Sub

Private Delegate Sub UpdateProgressBar1(ByVal value As Integer)

Public Sub SetProgress(ByVal paramvalue As Integer)
    If Me.InvokeRequired Then
        Me.Invoke(New UpdateProgressBar1(AddressOf Me.SetProgress), paramvalue)
    Else
        Me.ProgressBar1.Value = paramvalue
    End If

End Sub

Private Sub mySerialPort_DataReceived(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs)

    Dim tmpbyte As Byte
    fStream = New FileStream(CurrentFileName, FileMode.Append, FileAccess.Write, FileShare.ReadWrite)
    Dim bw As New BinaryWriter(fStream)

    'Try

    While mySerialPort.BytesToRead > 0
        Dim msgCharArray(255) As Char
        If mySerialPort.BytesToRead >= 256 Then
            mySerialPort.Read(msgCharArray, 0, 256)
        Else
            'mySerialPort.Read(msgCharArray, 0, mySerialPort.BytesToRead)
            Select Case uart_operation
                Case 2
                    If mySerialPort.ReadByte() = &HFF Then
                        uart_operation = 22
                    End If
                Case 22
                    tmpbyte = mySerialPort.ReadByte()
                    bytearray(bytecount) = tmpbyte
                    bytecount += 1

                    If bytecount = 16 Then
                        bw.Write(bytearray)
                        bytecount = 0
                        DayNum += 1
                        Dim b() As Byte = New Byte() {255}
                        mySerialPort.Write(b, 0, b.Length)
                        mySerialPort.BaseStream.Flush()

                        If DayNum = 366 Then
                            Me.SetProgress(DayNum)
                            MsgBox("Done")
                            Me.SetProgress(0)

                            bw.Close()
                            fStream.Close()
                            bw = Nothing
                            fStream = Nothing

                        Else
                            Me.SetProgress(DayNum)
                        End If

                    End If

            End Select

        End If
    End While
    'Catch ex As Exception
    '   MessageBox.Show("Exception Occured: " + ex.Message)
    'End Try
End Sub

结束类

4

0 回答 0