Results 1 to 3 of 3

Thread: Serial Port: Read data and update TextBox content

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2024
    Posts
    2

    Serial Port: Read data and update TextBox content

    Hi there,

    I need some help with serial port data readings from a scale connected to my pc.

    I found a very simple way to read data from the serial port and show the results each time the serial port receives data:

    First, I open the serial port with a click on a button:

    Code:
    Private Sub Button1_Click_2(sender As Object, e As EventArgs) Handles Button1.Click
            SerialPort1.Open()
    End Sub
    Then, each time the scale sends data, I can display it it in the console window:

    Code:
    Private Sub SerialPort1_DataReceived(sender As Object, e As IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
            Console.WriteLine(SerialPort1.ReadLine())
    End Sub

    I would like to simply display the data in a textbox, but get errors

    Code:
      Private Sub SerialPort1_DataReceived(sender As Object, e As IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
          Dim returnStr As String = ""
          returnStr = SerialPort1.ReadLine()
          TextBoxWaage_Read.Text = returnStr
          Console.WriteLine(returnStr)
      End Sub
    Has anyone a SIMPLE solution for this?

    Thank you,

    Thomas
    Last edited by jmcilhinney; Apr 27th, 2024 at 03:01 AM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Serial Port: Read data and update TextBox content

    If only there was some way we could know what those errors were. Do you know anyone who could tell us?

    Seriously though, this is not something we should have to ask for. If you'd like us to diagnose the issue, you need to provide us with the diagnostic information the system gave to you. There's nothing that jumps out at me from your code so it's we need to know what problem we're actually trying to solve. That's what the error message is for. Tell us whether it's a compilation error or a run-time exception, where it occurs and what the message is. You should also be using the debugger to see what data is in use at the time.

    BTW, this is a bit silly:
    Code:
    Dim returnStr As String = ""
    returnStr = SerialPort1.ReadLine()
    What's the point of assigning an empty String to a variable if you're going to replace it with something else on the very next line? If you want that variable to contain the data from the serial port, just assign that:
    Code:
    Dim returnStr = SerialPort1.ReadLine()
    You don't even need to specify the data type because it will be inferred from the return type of the initialising expression, although you can specify it if it makes your code clearer to you.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Serial Port: Read data and update TextBox content

    I have also formatted all your code snippets. Please always do so yourself, because it makes the code far more readable. It's primarily the indenting that helps but many, myself included, find the fixed-width font easier to read for code too, probably because that's what we're used to in VS or other environment.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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