Click here to Skip to main content
15,306,245 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Using VS2015 and Receiving data using Serial Port at 115200 baud rate.

Able to received all data using debug or release mode while running application from Visual Studio. But when using independent application .exe file, serial Data is missing.

Using Following Code


<pre>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 = 100
                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
        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
                    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
BASIC



What I have tried:

same .exe file works fine with Win7 or Old win Machine

Data look like this
T0000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
T0001FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
T0002FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
T0003FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
T0004FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
T0005FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
Posted
Updated 27-Jan-21 23:48pm
Comments
Richard MacCutchan 28-Jan-21 5:50am
   
Trying to read data in a timer is not a good method. You are assuming that all the data is present when the timer event occurs, but this may not be true. You should read continuously until no more data is present, and then assemble the contents of the buffer(s) into the format required. Using a single task that reads the data is a better way of doing it.
AAhsan24 28-Jan-21 8:21am
   
Thanks RickZeeland for help

After updating timer to event handler, my main Window Freeze while updating File.

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.Writ

1 solution

It's not a good idea to use a Timer, see the event driven solution here:
Basic serial port listening application[^]
   

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