dcsimg
Results 1 to 2 of 2

Thread: Live scale reading from serial port and converting to usable format

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2018
    Posts
    2

    Live scale reading from serial port and converting to usable format

    Hi,

    I am having a nightmare i have created an application that accesses a database for recipe info and have a scale reading from the serial port which will eventually compare to the database entry and confirm theweight is correct.

    Im not a wizz at vb programming and only started a few months ago, so i need help with this.

    My scale displays a live weight but i am using a timer to do this continously, i understand that i should use the data receive event but i dont know what i am doing with it. I think that because i am using a timer it is stopping me from being able to convert the serialport_scale.readline into any usable format...preferable doubble bacuse it keeps throing an exception saying input string is no in the correct format.....CAN ANYBODY HELP ME WITH THIS? ITS DRIVING ME INSANE Thank you....Code posted below ;

    Code:
    Imports BatchRight_BF_2018.Batching_Declerations
    Imports BatchRight_BF_2018.Product_Select
    Imports BatchRight_BF_2018
    Imports System.Data.SqlClient
    Imports System.Data
    Imports System.IO.Ports
    Imports System.Object
    Imports System.Windows.Forms
    Imports System.Timers
    Imports System
    
    
    
    Public Class Batching_ScreenLine6
    
        Dim Ingredient_Counter As String
        Dim ingredient_complete As String
        Dim weight_correct As String
        Dim LiveWeight As String
    
        Dim weight_decimal As Double
        Dim REQUIRED_WEIGHT As String
        Dim tolerance_check As String
    
        Dim WeightConversion As String
    
        'SET UP SCALE SERIAL COMMUNICATIONS
        Class PortDataReceived
    
            Public Shared Sub Main()
    
                Dim Serialport_Scale As New SerialPort("COM3")
    
                Serialport_Scale.BaudRate = 9600
                Serialport_Scale.Parity = Parity.None
                Serialport_Scale.StopBits = StopBits.One
                Serialport_Scale.DataBits = 8
                Serialport_Scale.Handshake = Handshake.None
    
                ' STARTS THE EVENT AND PASSES TO THE NAMED PROCEDURE
                AddHandler Serialport_Scale.DataReceived, AddressOf DataReceivedHandler
    
            End Sub
            Public Shared Sub DataReceivedHandler(serialport_Scale As Object, e As SerialDataReceivedEventArgs)
    
    
            End Sub
        End Class
    
    
    
    
        'CONNECTION TO DATABASE
        Public Shared Function sqlserverconncetion() As SqlConnection
            Return New SqlConnection("Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Buckley foods ltd\source\repos\BatchRight BF 2018\BatchRight BF 2018\BF Batching.mdf;Integrated Security=True;Connect Timeout=30")
        End Function
    
        Public Sub Batching_Screen_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            'DISPLAYS THE CURRENT SELECTED SCREEN LINE NUMBER AND PRODUCT
            Line_Selected.Text = batchingScreen_Selected
    
        End Sub
    
        Private Sub Switch_button_Click(sender As Object, e As EventArgs) Handles Switch_button.Click
            'Switch SCREEN CONTROL
            Switch_screen.Show()
            Me.Hide()
            Me.Close()
    
        End Sub
    
    
        Private Sub BatchingControl_Button_Click(sender As Object, e As EventArgs) Handles BatchingControl_Button.Click
    
            Ingredient_Counter = 1
    
            Dim ING1 As String
            Dim W_1 As String
            Dim RM_1 As String
    
            'DATABASE ENQUIRY FOR FIRST INGREDENT
    
            Dim connectionstring1 As String = ("Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Buckley foods ltd\source\repos\BatchRight BF 2018\BatchRight BF 2018\BF Batching.mdf;Integrated Security=True;Connect Timeout=30")
            Dim connectionING1 As New SqlConnection(connectionstring1)
            Try
                If Ingredient_Counter = 1 Then connectionING1.Open()
    
                Dim commandING1 As New SqlCommand
                commandING1.Connection = connectionING1
                commandING1.CommandText = "SELECT * FROM Ingredients WHERE QMF = '" & QMF_Label6.Text & "'"
    
                Dim readerING1 As SqlDataReader = commandING1.ExecuteReader
                If readerING1.HasRows Then
    
                    While readerING1.Read
                        ING1 = (readerING1("ING1"))
                        Actual_Ingrdient.Text = ING1
    
                        W_1 = (readerING1("W_1"))
                        Batching_RequiredWeight.Text = W_1
    
    
                        RM_1 = (readerING1("RM_1"))
                        RM_NUMBER_LABEL.Text = RM_1
    
                    End While
                End If
    
                readerING1.Close()
                readerING1.Dispose()
            Catch ex As Exception
                Console.Write(ex.Message)
            Finally
                connectionING1.Close()
            End Try
        End Sub
    
    
        '//////////////////////////// OPEN COM PORT///////////////////
    
    
    
        Public Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    
    
            If SerialPort_Scale.IsOpen = False Then
                SerialPort_Scale.Open()
            End If
    
            Timer1.Start()
            SerialPort_Scale.WriteLine("SIR")
    
    
      
    
    
            LiveWeight = SerialPort_Scale.ReadLine()
    
    
            Live_Weight.Text = LiveWeight
      
    
    
            SerialPort_Scale.Close()
    
    
    
    
    
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Dim text As String = LiveWeight
            Dim value As Double
    
            If Double.TryParse(text, value) Then
                Label8.Text = "True"
            Else
                MessageBox.Show("error")
            End If
    
    
        End Sub
    
    
    End Class
    Last edited by si_the_geek; Oct 12th, 2018 at 09:46 AM. Reason: added Code tags

  2. #2
    Addicted Member
    Join Date
    Nov 2011
    Posts
    178

    Re: Live scale reading from serial port and converting to usable format

    Try this and see if you can receive data from the scale.

    The format will not be right but that can be dealt with once you know data is coming in.

    The example is just a windows form containing a single rich text box

    Code:
    Imports System.IO.Ports
    
    Public Class Form1
    
    
        Public WithEvents Serialport_Scale As New SerialPort
        Public Delegate Sub myDelegate(ByVal sData As Byte)
    
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Serialport_Scale.PortName = "COM7"
            Serialport_Scale.Open()
        End Sub
    
    
        Private Sub Text_Out(ByVal sData As Byte)
            RichTextBox1.AppendText(sData.ToString & " ")
        End Sub
    
        Private Sub Serialport_Scale_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles Serialport_Scale.DataReceived
            Dim sData As Byte
            Serialport_Scale.ReadTimeout = 20
    
            Do While Serialport_Scale.BytesToRead > 0
                Try
                    sData = CByte(Serialport_Scale.ReadByte)
                    Me.BeginInvoke((New myDelegate(AddressOf Text_Out)), sData)
    
                Catch ex As Exception
    
                End Try
            Loop
    
        End Sub
    
    
    End Class

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width