Results 1 to 12 of 12

Thread: Serial data outputs nonsense

Threaded View

  1. #2
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,945

    Re: Serial data outputs nonsense

    Quote Originally Posted by Vizier87 View Post
    Hi guys,

    I'm attempting at just displaying the raw values from a COM serial port from a sensor, which gives around 4000 rows of data per cycle.

    Here's the code:

    Code:
    Imports System
    Imports System.IO.Ports
    Imports System.Threading
    
    Public Class Form1
    
        Dim readThread As Thread = New Thread(AddressOf ReadFromCom)
        Dim abortThread As Boolean
    
        Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            SerialPort2.Open()
            readThread.Start()
    
        End Sub
    
        Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            With SerialPort2
                .PortName = "COM9"
                .BaudRate = 2000000
                .Parity = Parity.None
                .DataBits = 8
                .StopBits = 1
                .ReadTimeout = Nothing
                .DtrEnable = True
                .RtsEnable = True
            End With
        End Sub
    
        Public Sub ReadFromCom()
    
            While abortThread = False
                Try
                    Dim message As String = SerialPort2.ReadExisting
                    updateStatus("Received: " & message)
                Catch ex As TimeoutException
                    updateStatus(ex.ToString)
                End Try
            End While
        End Sub
    
        Public Delegate Sub updateStatusDelegate(ByVal newStatus As String)
    
        Public Sub updateStatus(ByVal newStatus As String)
            If Me.InvokeRequired Then
                Dim upbd As New updateStatusDelegate(AddressOf updateStatus)
                Me.Invoke(upbd, New Object() {newStatus})
            Else
                TextBox1.Text = newStatus & vbCrLf & vbCrLf & TextBox1.Text
            End If
        End Sub
    End Class
    The output textbox gives out a string of ????????? most of the time, sometimes numerical. Sometimes it's nothing.

    Anything I should read more into? It seems not many thread discusses this at length so I'm tweaking about other people's code hoping for the best.

    Anyhow an independent Python code with MatPlotLib, with the same settings for the serial port outputs the data quite nicely.

    Thanks.
    Vizier87
    Hello Vizier87,

    What kind of data are you expecting from that sensor? If it is plain text, your problem might be an encoding issue. If it isn't plain text you probably shouldn't be reading it straight into a string but rather a byte array and display it's contents as a series of hexadecimal values. Try this page: https://social.msdn.microsoft.com/Fo...orum=vbgeneral

    yours,
    Peter Swinkels

    EDIT:
    Perhaps you could upload that Python code? It would make understanding the issue easier.
    Last edited by Peter Swinkels; Jul 16th, 2020 at 09:06 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width