Alright, I'm pulling hair out of my head trying to figure this out.
I have Data coming in through a serial port and it consists of pretty much
all numerical values except for a few begining response characters. Its
a Load Cell which spits out Tension in Pounds. I'm making a simple terminal
program with the ability to log the Incoming Data for record keeping and data
plotting. I want the incoming Tension to be displayed on the screen real time.
I can get it into a text box and have it scroll indefinetly as it updates the data,
but I'd like to keep it localized in one Label. I can display it in the Label but
it wants to scroll down as it comes in. I want to keep it refreshed in one spot if that makes any sense. Heres what I have thus far. It will put it in the Label but wants to scroll like in the textbox.

For example if I send #00F0 followed by a carriage return it replys back with whatever is displayed on the Screen
which usually is the tension.

it says "01TK 00.074 LBS" no quotes.. for a reply.

I'm assuming it needs to be stripped down to just the numeric characters first.

'here is what gets sent to the Load Cell to tell it to output data fast as it can to the serial port.
Private Sub btnTransmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTransmit.Click
SerialPort1.Write("#00WI1" & vbCr)
End Sub

'Here is the Serialport incoming
Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
Dim msg As String
msg = SerialPort1.ReadExisting
lblDisplay.Text = "" & lblDisplay.Text & msg
End Sub

Any input on how I can go about doing this would be greatly appreciated.

Thanks.

Dani AI

Generated

The thread shows the right diagnosis from and : appending incoming text to the label causes the “scroll” effect. The real work is twofold — make sure each serial frame is read as a complete message, and update the UI from the GUI thread rather than the serial thread. The device reply example ("01TK 00.074 LBS") can be reduced to a single numeric value for display and formatting.

Recommended approach: set the serial port NewLine to match the indicator (carriage-return in this case) and read full lines instead of raw fragments. Extract the numeric token with a simple regex or TryParse, format it to the desired precision, and marshal the update onto the form thread with BeginInvoke/Invoke so the label is changed safely and non-blocking.

' Example (run on .NET/WinForms)
serialPort.NewLine = vbCr

Private Sub serialPort_DataReceived(...) Handles serialPort.DataReceived
    Dim rawLine As String = serialPort.ReadLine()
    Dim m = System.Text.RegularExpressions.Regex.Match(rawLine, "[+-]?\d+(\.\d+)?")
    If Not m.Success Then Return
    Dim v As Double
    If Double.TryParse(m.Value, Globalization.NumberStyles.Any, Globalization.CultureInfo.InvariantCulture, v) Then
        Dim s = v.ToString("F3", Globalization.CultureInfo.InvariantCulture) & " LBS"
        If lblDisplay.InvokeRequired Then
            lblDisplay.BeginInvoke(Sub() lblDisplay.Text = s)
        Else
            lblDisplay.Text = s
        End If
    End If
End Sub

UI tips: set the label AutoSize = False, TextAlign = MiddleCenter, and pick a large, fixed-size font (or a seven-seg style font) so the value looks like a digital indicator. If historical logging is needed, append readings to a separate buffer or file instead of the label. If updates still look jumpy, buffer incoming bytes until a full line arrives (DataReceived can fire with partial data).

Microsoft references: SerialPort.DataReceived behavior and thread-model (SerialPort.DataReceived) and safe control marshaling (Control.BeginInvoke / InvokeRequired).

Recommended Answers

All 5 Replies

It will put it in the Label but wants to scroll like in the textbox.

Maybe I didn't quite get that, but instead of appending new messages to the label you should replace previous text with the new message text(?):

lblDisplay.Text = msg

Or if you did mean that the whole message isn't shown, use a smaller font size in the label (second image showed you're using quite a big font size).

HTH

I dodnt quite understand.
You want that big multiline textbox the one in the left-down corner to be a label instead of a textbox?
And you want it to update as you get more data line by line but not let it autoscroll to the last entry?

oh or you want it to be a label and have the last entry first on the list and old ones go down hen new entry comes??
in that case you do something like this:

label.text = msg & vbnewline & label.text

Well, the Load Cell is hooked up to a indicator which displays the tension real time on it, like looking at a digital altimeter or something of that affect. I just want that same affect displayed in the Label. The unit is located 180 some odd ft away so thats the reason I need it displayed via onscreen computer in a decent font size so a room full of people can easily view it. I'd like to view it like this in the screen shot ive enclosed. It has to display like it does on the remote indicator, ie; digital speedometer.

I hope i understood what you want it to do :)
But i dont see the problem
this updates the textbox right?
lblDisplay.Text = "" & lblDisplay.Text & msg

why do you do that? just update the textbox
lblDisplay.Text = msg

this way it wont give the scrolling efect
If this way you were trying to kee a record of the values just store them in something separate...

I hope that helpes

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.