Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
After main Window Freeze while updating File using following code. Data Received every 200msec. Only freeze hen stored in File other wise application work ok.

VB
Private Sub initCommPort()
    If SerialPort1.IsOpen = False Then
        SerialPort1.PortName = ListBox1.SelectedItem
        SerialPort1.BaudRate = 115200
        SerialPort1.Parity = Parity.None
        SerialPort1.DataBits = 8
        SerialPort1.StopBits = StopBits.One
        SerialPort1.ReadTimeout = 100


        'SerialPort1.ReceivedBytesThreshold = 100
        SerialPort1.DiscardNull = True

        Try
            SerialPort1.Open()
            'rs232Timer.Interval = 1
            'rs232Timer.Start()
            vcapMin1 = 9999
            vcapMax1 = 0
            vcapMin2 = 9999
            vcapMax2 = 0
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End If
    ' ONCE PORT IS INITIALISED, RECEIPT OF DATA KICKS EVERYTHING OFF ...
End Sub

' This is the method to run when the rs232Timer is raised.
'Private Sub TimerEventProcessor(ByVal myObject As Object, ByVal myEventArgs As EventArgs) Handles rs232Timer.Tick
Private Sub SerialPort_DataReceived(sender As Object, e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
    'rs232Timer.Stop()
    Do
        Try
            Dim Incoming As String = SerialPort1.ReadLine()
            If Incoming Is Nothing Then
                Exit Do
            Else
                Dim packet As String = Incoming
                Incoming = Nothing
                If (RequestCmd > 0) Then
                    If SerialPort1.IsOpen = True Then
                        SerialPort1.Write(Chr(RequestCmd))
                        System.Diagnostics.Debug.WriteLine("CMD: " & RequestCmd)
                        RequestCmd = 0
                    Else
                        System.Diagnostics.Debug.WriteLine("CMD: " & RequestCmd)
                    End If
                End If
                Me.BeginInvoke(New StringSubPointer(AddressOf ProcessData), packet)

            End If
        Catch ex As Exception
            Exit Do
        End Try
    Loop

    'rs232Timer.Start()
End Sub

Private Sub ProcessData(ByVal Buffer As String)
    Dim rawLine As String
    If Buffer.Length > 0 Then
        Select Case Mid(Buffer, 1, 1)
            Case "P" 'PON registers
                processPonRegs(Buffer)
                If (Mid(Buffer, 2, 1) = "1") Then
                    If (p1Active = True) Then
                        rawLine = Mid(Buffer, 1, 2) & " " & Mid(Buffer, 3, 16) & " " & Mid(Buffer, 19, 16) & " " & Mid(Buffer, 35, 16) & " " & Mid(Buffer, 51, 16)
                        storeRawDataLine(rawLine, 2)
                        rawLine = "   " & Mid(Buffer, 67, 16) & " " & Mid(Buffer, 83, 16) & " " & Mid(Buffer, 99, 16) & " " & Mid(Buffer, 115, 16)
                        storeRawDataLine(rawLine, 3)
                    End If
                Else
                    If (p2Active = True) Then
                        rawLine = Mid(Buffer, 1, 2) & " " & Mid(Buffer, 3, 16) & " " & Mid(Buffer, 19, 16) & " " & Mid(Buffer, 35, 16) & " " & Mid(Buffer, 51, 16)
                        storeRawDataLine(rawLine, 4)
                        rawLine = "   " & Mid(Buffer, 67, 16) & " " & Mid(Buffer, 83, 16) & " " & Mid(Buffer, 99, 16) & " " & Mid(Buffer, 115, 16)
                        storeRawDataLine(rawLine, 5)
                    End If
                End If
            Case "T" 'Trace Buffer Data
                'saveTraceBufferLine(Buffer, TBConfiguration)
                My.Computer.FileSystem.WriteAllText("TraceBuffer.txt", Buffer, True)
                rawLine = Buffer
                filedata = Buffer
                storeRawDataLine(rawLine, 6)
                TBLine = TBLine + 1
            Case Else 'Status data
                If Mid(Buffer, 4, 1) = "S" Then
                    processStatus(Buffer)
                    rawLine = Mid(Buffer, 4, 1) & "  " & Mid(Buffer, 5, 16) & " " & Mid(Buffer, 21, 16) & " " & Mid(Buffer, 37, 16) & " " & Mid(Buffer, 53, 16)
                    storeRawDataLine(rawLine, 1)
                    displaySerialNumber()
                    displayFirmware()
                    displayAlarms()
                    increment(nscans1)
                    ProgressBar1.Value = nscans1
                End If
        End Select
    End If
End Sub


What I have tried:

Used removed timed data extraction
Posted
Updated 29-Jan-21 23:10pm
v4
Comments
CHill60 28-Jan-21 12:34pm    
What are p1Active and p2Active and where are they set?
What does storeRawDataLine do?
On which line does the application "freeze"? (Use debugging to determine this) - the fact you say it only freezes when you are writing to a file would suggest that that is where the problem is
[no name] 28-Jan-21 12:34pm    
"data received every 200ms" ... ReadTimeout = 100 (ms).

1 solution

I suppose that the "freeze" comes in the SerialPort_DataReceived-Method inside the line "Dim Incoming As String = SerialPort1.ReadLine()"
Readline awaits a CR at it's end - I think that there isn't one when the method itself is called. Please realize that this method is called with the first incomming character on the Serialport - I suggest you first should check the count of characters inside the buffer of the Serialport ...
I also think that Readline is the complete wrong approach at this point ...
If you know how many characters belong to a complete line it will be better you check if the buffer contains them and after this read it out (and not with readline).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900