none
Unterschiedliches Verhalten COM-Port Development - ausführbaresProgramm RRS feed

  • Allgemeine Diskussion

  • Hoi,

    ich habe mir ein kleines Proggi für das Einlesen von ASCII-Daten über eine USB Serielle Schnittstelle gecodet, das auch funktioniert (eigentlich).

    Wenn ich in der VBE IDE bin und das Programm laufen lasse, sieht alles OK und so aus:

    NACH dem kompilieren und starten der EXE sieht das dann SO aus:

    Hier ist der Code den ich in der Form PCDATAstream verwende:

    Imports System
    Imports System.IO.Ports
    Imports System.Threading
    Imports System.IO
    Imports System.Text
    
    Public Class PCDataStream
        Public Shared Function GetPortNames() As String()
        End Function
        Shared _continue As Boolean
        Shared _serialPort As SerialPort
    
        Dim SerielleDaten As Array
        Dim PCPORT As String
        Dim path As String = "\meinGeheimpfad\Data\PC_Daten.txt"
    
        Dim IsDraggingForm As Boolean = False 
        Private MousePos As New System.Drawing.Point(0, 0)
        Delegate Sub AddText(ByVal Text As String)
    
        Private Sub PCDataStream_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Me.MdiParent = Form1
    
            ' Get a list of serial port names.
            Dim ports As String() = SerialPort.GetPortNames()
    
            ' PCTextBox.Text = "The following serial ports were found:" & vbCrLf & vbCrLf
    
            ' Display each port name to the console.
            Dim port As String
            For Each port In ports
                PCCOMComboBox.Items.Add(port)
                'PCTextBox.Text = PCTextBox.Text & port & vbCrLf
            Next port
    
        End Sub
    
    
        Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
            Dim sw As StreamWriter = File.AppendText(path)
            sw.WriteLine(DateString & " " & TimeString)
            sw.WriteLine("")
            sw.WriteLine("Temperaturabfrage")
            sw.WriteLine("")
    
            Try
                ' Lese den aktuellsten String ein und stelle ihn im Fenster dar
                PCTextBox.Invoke(New AddText(AddressOf PCTextBox.AppendText), SerialPort1.ReadLine + Environment.NewLine)
                PCTextBox.Text = SerialPort1.ReadLine
                'MsgBox(SerialPort1.ReadLine, MsgBoxStyle.Critical, "Check")
                'Catch ex As Exception
                'End Try
    
                ' Lese jede einzelene Zeile
                ' .... und wenn die mit "Sensor #1 : " anfängt dann...
                If SerialPort1.ReadLine.StartsWith("Sensor #1") Then
                    MsgBox(SerialPort1.ReadLine, MsgBoxStyle.Critical, "Check")
                    Try
                        sw.WriteLine("Sensor #1 : " & SerialPort1.ReadLine.Substring(0, 2))
                    Catch ex As Exception
                    End Try
                End If
    
                If SerialPort1.ReadLine.Contains("Temperaturabfrage") Then
                    MsgBox(SerialPort1.ReadLine, MsgBoxStyle.Critical, "Check")
                    Try
                        sw.WriteLine("Sensor #1 : " & SerialPort1.ReadLine.Substring(2, 4))
                    Catch ex As Exception
                    End Try
                End If
                '
                If PCTextBox.Text.Contains("Sensor #1") Then
    
                    '' .... und wenn die mit "Sensor #2 : " anfängt dann...
                    'If SerialPort1.ReadLine.StartsWith("Sensor #2 : ") Then
                    Try
                        sw.WriteLine("Sensor #2 : " & SerialPort1.ReadLine.Substring(4, 8))
                    Catch ex As Exception
                    End Try
                End If
    
    
    
            Catch ex As Exception
            End Try
    
    
            sw.Flush()
            sw.Close()
        End Sub
        Function ReceiveComData() As String
            ' Receive strings from a serial port.
            Dim returnStr As String = ""
    
            Dim com2 As IO.Ports.SerialPort = Nothing
    
    
            Try
                com2 = My.Computer.Ports.OpenSerialPort(PCPORT)
                com2.ReadTimeout = 10000
                Do
                    Dim Incoming As String = com2.ReadLine()
    
    
                    If Incoming Is Nothing Then
                        Form1.PCLED.Image = My.Resources.redledoff
                        Exit Do
                    Else
                        returnStr &= Incoming & vbCrLf
                    End If
                Loop
            Catch ex As TimeoutException
                returnStr = "Error: Serial Port read timed out."
    
            Finally
                If com2 IsNot Nothing Then com2.Close()
                Form1.PCLED.Image = My.Resources.redledoff
            End Try
    
            Return returnStr
            'Terminal.Items.Add(returnStr)
            PCTextBox.Text = (returnStr)
            PCTextBox.Refresh()
    
            Dim availableSerialPorts As System.Collections.ObjectModel.ReadOnlyCollection(Of String) = My.Computer.Ports.SerialPortNames
            If availableSerialPorts.Contains(PCPORT) Then
                Form1.PCLED.Image = My.Resources.redledon
                'MessageBox.Show("COM1 is available")
            Else
                Form1.PCLED.Image = My.Resources.redledoff
                'MessageBox.Show("No COM1 on this computer")
            End If
    
        End Function
    
        Private Sub PCCOMComboBox_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles PCCOMComboBox.SelectedIndexChanged
            PCPORT = PCCOMComboBox.SelectedItem
            '        MsgBox(IDEPORT, MsgBoxStyle.Information, "Selected " & IDEPORT)
            If File.Exists(path) = False Then
                ' Create a file to write to.
                Dim sw As StreamWriter = File.CreateText(path)
                sw.WriteLine(DateString & " " & TimeString)
                sw.Close()
            End If
            Try
                SerialPort1.BaudRate = 9600
                SerialPort1.Parity = Parity.None
                SerialPort1.StopBits = StopBits.One
                SerialPort1.DataBits = 8
                SerialPort1.PortName = (PCPORT)
                SerialPort1.Open()
                Form1.PCLED.Image = My.Resources.redledon
            Catch ex As Exception
                MessageBox.Show("Fehler COM-Port !")
                Form1.PCLED.Image = My.Resources.redledoff
            End Try
    
    
        End Sub
    
    End Class

    Hat jemand eine Idee was da los ist ?

    Der Input ist immer derselbe, der Port auch ... habe schon andere Proggis in der Art gemacht und (eigentlich) nichts Anderes gemacht wie sonst auch - blicke aber nicht (mehr) durch was da faul sein könnte.

    Hat jemand eine Idee ?

    Doei
    Franz


    Be a good forum member - mark posts that contain the answers to your questions or those that are helpful


    • Bearbeitet Trixi-N Sonntag, 2. Februar 2014 15:02
    • Typ geändert Ionut Duma Freitag, 21. Februar 2014 09:34 Keine Rueckmeldung des Fragenstellender
    Sonntag, 2. Februar 2014 15:00

Alle Antworten