Click here to Skip to main content
15,886,823 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have application to receive weight from scale but weightscale contains meny values as
250kg,+GS,ST,....
I wont the result is double
very thanks

What I have tried:

VB.NET
imports system.io.ports
	Public Class Form1
    Public Delegate Sub myDelegate()
	    Public Sub updateTextBox()
	        Dim strarr(8) As String
	        Dim str1 As String
	        str1 = port.ReadLine
	        W1.AppendText(str1)
	        strarr =str1.Split(",")
	        Dim x1 As Double
	        x1 = Val(strarr(0))
	        'MsgBox(x1)
	        W1.ScrollToCaret()
	    End Sub
    Private Sub port_DataReceived(ByVal sender As Object, ByVal e AsSystem.IO.Ports.SerialDataReceivedEventArgs) Handles port.DataReceived
        W1.Invoke(New myDelegate(AddressOf updateTextBox), New Object() {})
    End Sub
private sub btn_connect
        With SerialPort1 'Connection for my serial port
	            .Close()
	            .PortName = "COM3"
	            .BaudRate = 9600
	            .Parity = Parity.None
	            .DataBits = 8
	            .StopBits = StopBits.One
	            .DtrEnable = True
	            .RtsEnable = True
	            .ReceivedBytesThreshold = 1
	        End With
	        SerialPort1.Open()
	        Timer1.Interval = 1000
	        Timer1.Enabled = True
	    End Sub
private sub btn_disconnect
seriaport1.close
Posted
Updated 6-Mar-21 4:50am
v2
Comments
Member 15040912 23-Feb-21 4:38am    
ماذكرته لا أستطيع تنفيذه برمجيا رجاء التكرم بالمساعده في تعديل الكود حتى يعمل بشكل جيد ولكم جزيل الشكر
Al Halabi Ahmad 25-Apr-23 9:58am    
هل استطعت تنفيذه؟

First off, don't use DataRecieved for this, and don't automatically Invoke everything back to the UI thread.

You don't get a DataRecieved event for "complete messages" - you can get one for each character, because Serial data is considerably slower than your processor (9600 baud translates to "less than one thousand characters per second at best".

So what you need to do is set up a second processing thread - a BackgroundWorker is a good idea here - which pulls data off teh serial port as it becomes available by p[olling for data, buffers it until you have a complete "Message" (you'll need to look in your manual to see what that is exactly) and then passes each message up to your UI thread via the progress reporting mechanism so that you can them "Pick it apart" and extract the info you need - probably using a Regex to isolate the strings you are interested in.
You can then use the various TryParse methods to convert those string to numeric values and handle them in your code.

It sound complex, but it's pretty simple - it's just breaking the work down into discrete chunks which "know" about that "level" of data and passing validated data on up to higher level processing.
 
Share this answer
 
Comments
Member 15072853 6-Mar-21 10:45am    
sometimes the project dose not work sometimes work will i do not know the resuon
imports system.io.ports
	Public Class Form1
    Public Delegate Sub myDelegate()
	    Public Sub updateTextBox()
	        Dim strarr(8) As String
	        Dim str1 As String
	        str1 = port.ReadLine
	        W1.AppendText(str1)
	        strarr =str1.Split(",")
	        Dim x1 As Double
	        x1 = Val(strarr(0))
w1.text=(x1)
 W1.ScrollToCaret()
	    End Sub
 Private Sub port_DataReceived(ByVal sender As Object, ByVal e AsSystem.IO.Ports.SerialDataReceivedEventArgs) Handles port.DataReceived
        W1.Invoke(New myDelegate(AddressOf updateTextBox), New Object() {})
    End Sub
private sub btn_connect
        With SerialPort1 'Connection for my serial port
	            .Close()
	            .PortName = "COM3"
	            .BaudRate = 9600
	            .Parity = Parity.None
	            .DataBits = 8
	            .StopBits = StopBits.One
	            .DtrEnable = True
	            .RtsEnable = True
	            .ReceivedBytesThreshold = 1
	        End With
	        SerialPort1.Open()
	        Timer1.Interval = 1000
	        Timer1.Enabled = True
	    End Sub
private sub btn_disconnect
seriaport1.close
end sub
 
Share this answer
 
Comments
Ralf Meier 7-Mar-21 3:55am    
What are you trying to show us with this code-snippet ?
For me it makes less sense and isn't an answer to the question ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900