HEX-Werte an RS232 Schnittstelle auslesen

  • VB.NET

Es gibt 57 Antworten in diesem Thema. Der letzte Beitrag () ist von iOS78.

    Probier einfach ma folgendes:

    VB.NET-Quellcode

    1. Option Explicit On
    2. Public Class frm_GO
    3. Dim BytesPerLine As Integer
    4. Dim SerialPortData As String
    5. Private thread As New Threading.Thread(New Threading.ThreadStart(AddressOf HandleSerialData))
    6. Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
    7. 'Me.Invoke(Sub() HandleSerialData(SerialPort1.ReadExisting))
    8. thread.Start()
    9. End Sub
    10. Private Sub frm_EHZMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    11. Me.SerialPort1.Open()
    12. BytesPerLine = 1
    13. End Sub
    14. Private Sub HandleSerialData()
    15. Dim SerialData As String = SerialPort1.ReadExisting
    16. Dim str2Append As String = ""
    17. Dim data As Integer
    18. Dim ldata(9) As Integer
    19. Dim i As Integer
    20. While Me.SerialPort1.BytesToRead >= 0
    21. data = Me.SerialPort1.ReadByte()
    22. ldata(9) = data
    23. For i = 1 To 8
    24. ldata(i) = ldata(i + 1)
    25. Next
    26. 'Me.txt_Stream.AppendText(Microsoft.VisualBasic.Right("0" & Hex(data), 2) & " ")
    27. str2Append &= Microsoft.VisualBasic.Right("0" & Hex(data), 2) & " "
    28. BytesPerLine = BytesPerLine + 1
    29. If BytesPerLine >= 16 Then
    30. BytesPerLine = 1
    31. 'Me.txt_Stream.AppendText(vbCrLf)
    32. str2Append &= vbCrLf
    33. End If
    34. If ldata(1) = 27 And ldata(2) = 27 And ldata(3) = 27 And ldata(4) = 27 And ldata(5) = 26 Then
    35. 'Me.txt_Stream.AppendText("ENDE")
    36. 'Me.txt_Stream.AppendText(vbCrLf)
    37. 'Me.txt_Stream.AppendText(vbCrLf)
    38. 'Me.txt_Stream.AppendText(vbCrLf)
    39. 'Me.txt_Stream.AppendText(vbCrLf)
    40. str2Append &= "ENDE" & vbCrLf & vbCrLf & vbCrLf & vbCrLf
    41. BytesPerLine = 1
    42. End If
    43. If ldata(1) = 27 And ldata(2) = 27 And ldata(3) = 27 And ldata(4) = 27 And _
    44. ldata(5) = 1 And ldata(6) = 1 And ldata(7) = 1 And ldata(8) = 1 Then
    45. 'Me.txt_Stream.AppendText(vbCrLf)
    46. 'Me.txt_Stream.AppendText("Start")
    47. str2Append &= vbCrLf & "Start"
    48. BytesPerLine = 1
    49. 'Me.txt_Stream.AppendText(vbCrLf)
    50. str2Append &= vbCrLf
    51. End If
    52. End While
    53. Me.txt_Stream.Invoke(Sub() Me.txt_Stream.AppendText(str2Append))
    54. End Sub
    55. Private Sub frm_EHZMain_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs)
    56. Me.SerialPort1.Close()
    57. End Sub
    58. End Class


    @RodFromGermany: Lass ihn das einfach ma testen
    In general (across programming languages), a pointer is a number that represents a physical location in memory. A nullpointer is (almost always) one that points to 0, and is widely recognized as "not pointing to anything". Since systems have different amounts of supported memory, it doesn't always take the same number of bytes to hold that number, so we call a "native size integer" one that can hold a pointer on any particular system. - Sam Harwell
    Aaarg...hätt ich bedenken müssen. Sry
    Problem ist folgendes: Ich starte einen Thread und gleich danach wieder den selben -> geht nicht.
    Setz mich gleich daran das Problem zu lösen
    In general (across programming languages), a pointer is a number that represents a physical location in memory. A nullpointer is (almost always) one that points to 0, and is widely recognized as "not pointing to anything". Since systems have different amounts of supported memory, it doesn't always take the same number of bytes to hold that number, so we call a "native size integer" one that can hold a pointer on any particular system. - Sam Harwell
    Das hier soll angeblich funktionieren (Form mit 2 Textboxen):

    VB.NET-Quellcode

    1. Public Class frm_EHZMain
    2. Dim BytesPerLine As Integer
    3. Dim OutputEnable As Boolean
    4. Dim StartRecording As Boolean
    5. Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e
    6. As System.IO.Ports.SerialDataReceivedEventArgs) Handles
    7. SerialPort1.DataReceived
    8. 'SerialPortData = SerialPort1.ReadExisting '//oder SerialPort1.ReadLine
    9. Me.Invoke(New EventHandler(AddressOf HandleSerialData))
    10. End Sub
    11. Private Sub frm_EHZMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    12. Me.SerialPort1.Open()
    13. BytesPerLine = 1
    14. OutputEnable = True
    15. StartRecording = False
    16. DataInit()
    17. End Sub
    18. Private Sub HandleSerialData()
    19. Dim data As Integer
    20. Dim ldata(9) As Integer
    21. Dim i As Integer
    22. While Me.SerialPort1.BytesToRead > 0
    23. data = Me.SerialPort1.ReadByte()
    24. ldata(9) = data
    25. For i = 1 To 8
    26. ldata(i) = ldata(i + 1)
    27. Next
    28. If OutputEnable Then
    29. Me.txt_Raw.AppendText(Microsoft.VisualBasic.Right("0" & Hex(data), 2) & " ")
    30. BytesPerLine = BytesPerLine + 1
    31. If BytesPerLine >= 16 Then
    32. BytesPerLine = 1
    33. Me.txt_Raw.AppendText(vbCrLf)
    34. End If
    35. If ldata(1) = 27 And ldata(2) = 27 And ldata(3) = 27 And ldata(4) = 27 And ldata(5) = 26 Then
    36. Me.txt_Raw.AppendText("ENDE")
    37. Me.txt_Raw.AppendText(vbCrLf)
    38. Me.txt_Raw.AppendText(vbCrLf)
    39. Me.txt_Raw.AppendText(vbCrLf)
    40. Me.txt_Raw.AppendText(vbCrLf)
    41. Me.txtSML.AppendText(vbCrLf)
    42. BytesPerLine = 1
    43. If StartRecording Then
    44. ReadSMLFile()
    45. End If
    46. StartRecording = False
    47. End If
    48. If StartRecording Then
    49. If Not AddBytes(data) Then
    50. StartRecording = False
    51. End If
    52. End If
    53. If ldata(1) = 27 And ldata(2) = 27 And ldata(3) = 27 And ldata(4) = 27 And _
    54. ldata(5) = 1 And ldata(6) = 1 And ldata(7) = 1 And ldata(8) = 1 Then
    55. StartRecording = True
    56. DataInit()
    57. Me.txtSML.AppendText(vbCrLf)
    58. Me.txt_Raw.AppendText(vbCrLf)
    59. Me.txt_Raw.AppendText("Start")
    60. BytesPerLine = 1
    61. Me.txt_Raw.AppendText(vbCrLf)
    62. Me.txtSML.AppendText(vbCrLf)
    63. End If
    64. Else
    65. StartRecording = False
    66. End If
    67. End While
    68. End Sub
    69. Public Sub DisplaySMLText(ByVal s As String)
    70. Me.txtSML.AppendText(s)
    71. End Sub
    72. Private Sub frm_EHZMain_FormClosing(ByVal sender As System.Object,
    73. ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles
    74. MyBase.FormClosing
    75. Me.SerialPort1.Close()
    76. End Sub
    77. End Class


    Allerdings gibt es hier 3 nicht deklarierte Variablen:

    - DataInit()
    - ReadSMLFile()
    - AddBytes(data)

    Ich bekomme diesen Code nicht zum laufen......
    Ok probiers ma so:

    VB.NET-Quellcode

    1. Option Explicit On
    2. Public Class frm_GO
    3. Dim BytesPerLine As Integer
    4. Dim SerialPortData As String
    5. Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
    6. 'Me.Invoke(Sub() HandleSerialData(SerialPort1.ReadExisting))
    7. Private thread As New Threading.Thread(New Threading.ThreadStart(AddressOf HandleSerialData))
    8. thread.Start()
    9. End Sub
    10. Private Sub frm_EHZMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    11. Me.SerialPort1.Open()
    12. BytesPerLine = 1
    13. End Sub
    14. Private Sub HandleSerialData()
    15. Dim SerialData As String = SerialPort1.ReadExisting
    16. Dim str2Append As String = ""
    17. Dim data As Integer
    18. Dim ldata(9) As Integer
    19. Dim i As Integer
    20. While Me.SerialPort1.BytesToRead >= 0
    21. data = Me.SerialPort1.ReadByte()
    22. ldata(9) = data
    23. For i = 1 To 8
    24. ldata(i) = ldata(i + 1)
    25. Next
    26. 'Me.txt_Stream.AppendText(Microsoft.VisualBasic.Right("0" & Hex(data), 2) & " ")
    27. str2Append &= Microsoft.VisualBasic.Right("0" & Hex(data), 2) & " "
    28. BytesPerLine = BytesPerLine + 1
    29. If BytesPerLine >= 16 Then
    30. BytesPerLine = 1
    31. 'Me.txt_Stream.AppendText(vbCrLf)
    32. str2Append &= vbCrLf
    33. End If
    34. If ldata(1) = 27 And ldata(2) = 27 And ldata(3) = 27 And ldata(4) = 27 And ldata(5) = 26 Then
    35. 'Me.txt_Stream.AppendText("ENDE")
    36. 'Me.txt_Stream.AppendText(vbCrLf)
    37. 'Me.txt_Stream.AppendText(vbCrLf)
    38. 'Me.txt_Stream.AppendText(vbCrLf)
    39. 'Me.txt_Stream.AppendText(vbCrLf)
    40. str2Append &= "ENDE" & vbCrLf & vbCrLf & vbCrLf & vbCrLf
    41. BytesPerLine = 1
    42. End If
    43. If ldata(1) = 27 And ldata(2) = 27 And ldata(3) = 27 And ldata(4) = 27 And _
    44. ldata(5) = 1 And ldata(6) = 1 And ldata(7) = 1 And ldata(8) = 1 Then
    45. 'Me.txt_Stream.AppendText(vbCrLf)
    46. 'Me.txt_Stream.AppendText("Start")
    47. str2Append &= vbCrLf & "Start"
    48. BytesPerLine = 1
    49. 'Me.txt_Stream.AppendText(vbCrLf)
    50. str2Append &= vbCrLf
    51. End If
    52. End While
    53. Me.txt_Stream.Invoke(Sub() Me.txt_Stream.AppendText(str2Append))
    54. End Sub
    55. Private Sub frm_EHZMain_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs)
    56. Me.SerialPort1.Close()
    57. End Sub
    58. End Class


    Also so wie es nafets meinte
    In general (across programming languages), a pointer is a number that represents a physical location in memory. A nullpointer is (almost always) one that points to 0, and is widely recognized as "not pointing to anything". Since systems have different amounts of supported memory, it doesn't always take the same number of bytes to hold that number, so we call a "native size integer" one that can hold a pointer on any particular system. - Sam Harwell
    Schau dir ma folgendes Tut von @'ErfinderDesRades''s an: Threading mit BlockingList
    In general (across programming languages), a pointer is a number that represents a physical location in memory. A nullpointer is (almost always) one that points to 0, and is widely recognized as "not pointing to anything". Since systems have different amounts of supported memory, it doesn't always take the same number of bytes to hold that number, so we call a "native size integer" one that can hold a pointer on any particular system. - Sam Harwell

    iOS78 schrieb:

    Das hier soll angeblich funktionieren
    Hast Du meinen Code aus Post #39 probiert?
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!
    Ich hab ma den code noch ein klein wenig verändert, beim letzen war halt wieder das problem das mehrer threads auf ein objekt zugreifen und das sich ja nicht einfach so aufspalten kann..naja jedenfalls wäre ich dir sehr verbunden wenn du folgendes beispiel einmal ausporbieren und mir dann rückmeldung geben könntest:

    Spoiler anzeigen

    VB.NET-Quellcode

    1. Option Explicit On
    2. Public Class frm_GO
    3. Dim BytesPerLine As Integer
    4. Dim SerialPortData As String
    5. Private thread As New Threading.Thread(New Threading.ParameterizedThreadStart(AddressOf HandleSerialData))
    6. Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
    7. thread.Start(SerialPort1)
    8. End Sub
    9. Private Sub frm_EHZMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    10. Me.SerialPort1.Open()
    11. BytesPerLine = 1
    12. End Sub
    13. Private Sub HandleSerialData(SerialPort As Object)
    14. Dim port As IO.Ports.SerialPort = CType(SerialPort, IO.Ports.SerialPort)
    15. Dim str2Append As String = ""
    16. Dim data As Integer
    17. Dim ldata(9) As Integer
    18. Dim i As Integer
    19. While port.BytesToRead >= 0
    20. data = port.ReadByte()
    21. ldata(9) = data
    22. For i = 1 To 8
    23. ldata(i) = ldata(i + 1)
    24. Next
    25. str2Append &= Microsoft.VisualBasic.Right("0" & Hex(data), 2) & " "
    26. BytesPerLine = BytesPerLine + 1
    27. If BytesPerLine >= 16 Then
    28. BytesPerLine = 1
    29. str2Append &= vbCrLf
    30. End If
    31. If ldata(1) = 27 And ldata(2) = 27 And ldata(3) = 27 And ldata(4) = 27 And ldata(5) = 26 Then
    32. str2Append &= "ENDE" & vbCrLf & vbCrLf & vbCrLf & vbCrLf
    33. BytesPerLine = 1
    34. End If
    35. If ldata(1) = 27 And ldata(2) = 27 And ldata(3) = 27 And ldata(4) = 27 And _
    36. ldata(5) = 1 And ldata(6) = 1 And ldata(7) = 1 And ldata(8) = 1 Then
    37. str2Append &= vbCrLf & "Start"
    38. BytesPerLine = 1
    39. str2Append &= vbCrLf
    40. End If
    41. End While
    42. Me.txt_Stream.Invoke(Sub() Me.txt_Stream.AppendText(str2Append))
    43. End Sub
    44. Private Sub frm_EHZMain_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs)
    45. Me.SerialPort1.Close()
    46. End Sub
    47. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    48. End Sub
    49. End Class



    hierbei erstell ich einfach immer wieder ne kopie von deinem serialport - so lässt sich das zugriffsproblem lösen :D

    Lg Radinator
    In general (across programming languages), a pointer is a number that represents a physical location in memory. A nullpointer is (almost always) one that points to 0, and is widely recognized as "not pointing to anything". Since systems have different amounts of supported memory, it doesn't always take the same number of bytes to hold that number, so we call a "native size integer" one that can hold a pointer on any particular system. - Sam Harwell

    iOS78 schrieb:

    Funktioniert aber!
    Und friert das Programm noch ein?
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!
    @RodFromGermany
    Programm friert nicht mehr ein. Das heißt, ich muss die Zeichenkette gegen eine Variable tauschen und diese dann auf eine separate Textbox verweisen, damit ich die Daten dort angezeigt bekomme?



    @Radinator

    Gleiches Verhalten wie Anfangs. So funktioniert es aber auch nicht:

    VB.NET-Quellcode

    1. Option Explicit On
    2. Public Class frm_EHZMain
    3. Dim BytesPerLine As Integer
    4. Dim SerialPortData As String
    5. Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e
    6. As System.IO.Ports.SerialDataReceivedEventArgs) Handles
    7. SerialPort1.DataReceived
    8. [b]Dim thread As New Threading.Thread(New Threading.ParameterizedThreadStart(AddressOf HandleSerialData))
    9. thread.Start(SerialPort1)[/b]
    10. End Sub
    11. Private Sub frm_EHZMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    12. Me.SerialPort1.Open()
    13. BytesPerLine = 1
    14. End Sub
    15. Private Sub HandleSerialData(SerialPort As Object)
    16. Dim port As IO.Ports.SerialPort = CType(SerialPort, IO.Ports.SerialPort)
    17. Dim str2Append As String = ""
    18. Dim data As Integer
    19. Dim ldata(9) As Integer
    20. Dim i As Integer
    21. While port.BytesToRead >= 0
    22. data = port.ReadByte()
    23. ldata(9) = data
    24. For i = 1 To 8
    25. ldata(i) = ldata(i + 1)
    26. Next
    27. str2Append &= Microsoft.VisualBasic.Right("0" & Hex(data), 2) & " "
    28. BytesPerLine = BytesPerLine + 1
    29. If BytesPerLine >= 16 Then
    30. BytesPerLine = 1
    31. str2Append &= vbCrLf
    32. End If
    33. If ldata(1) = 27 And ldata(2) = 27 And ldata(3) = 27 And ldata(4) = 27 And ldata(5) = 26 Then
    34. str2Append &= "ENDE" & vbCrLf & vbCrLf & vbCrLf & vbCrLf
    35. BytesPerLine = 1
    36. End If
    37. If ldata(1) = 27 And ldata(2) = 27 And ldata(3) = 27 And ldata(4) = 27 And _
    38. ldata(5) = 1 And ldata(6) = 1 And ldata(7) = 1 And ldata(8) = 1 Then
    39. str2Append &= vbCrLf & "Start"
    40. BytesPerLine = 1
    41. str2Append &= vbCrLf
    42. End If
    43. End While
    44. Me.txt_Stream.Invoke(Sub() Me.txt_Stream.AppendText(str2Append))
    45. End Sub
    46. Private Sub frm_EHZMain_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs)
    47. Me.SerialPort1.Close()
    48. End Sub
    49. End Class


    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „iOS78“ ()

    iOS78 schrieb:

    ich muss
    mich zunächst entscheiden, ob ich die möglicherweise funktionierende Lösung von dem einen oder die möglicherweise nicht funktionierende Lösung von dem anderen Kollegen präferiere.
    Das bedeutet insbesondere, dass ich keinerlei Mischlösung implementieren werde.
    Jou.
    Was ist Dein Plan?
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!

    iOS78 schrieb:

    würde ich gerne darauf aufbauen.
    Dann tue das und lass die andere Lösung sein.
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!
    Was mir grad so auffällt ist folgendes:
    Warum schchreibst du in Post #37 in Zeile 21

    VB.NET-Quellcode

    1. While Me.SerialPort1.BytesToRead >= 0


    ?

    Wäre es nicht klüger wenn du

    VB.NET-Quellcode

    1. If Me.SerialPort1.BytesToRead >= 0

    machst?

    Bzw wo "entnimmst" du dem Stream die Bytes?

    Lg und fragend Radinator



    Edit: Foooo...sry hab Zeile #23 überlesen...jetz wirds auch klar warum ne While-Schleife

    In general (across programming languages), a pointer is a number that represents a physical location in memory. A nullpointer is (almost always) one that points to 0, and is widely recognized as "not pointing to anything". Since systems have different amounts of supported memory, it doesn't always take the same number of bytes to hold that number, so we call a "native size integer" one that can hold a pointer on any particular system. - Sam Harwell

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „Radinator“ ()