Advertisement

10.31.2007 at 05:43AM PDT, ID: 22929499
[x]
Attachment Details

vb.net SerialPort DataReceived data encoding jumbled garbage from RS232

Asked by pdschuller in .Net Editors & IDEs, Microsoft Visual Basic.Net, Microsoft Development

Tags: , ,

I have created a custom windows software in vb.net, VS 2005.  I am reading data from a Pennsylvania scale (model 7300) via my development computer's RS232 port.  I am using the SerialPort object and Handling the DataReceived event.  I first send the scale a command and then immediately use SerialPort.ReadExisting to get the weight from the scale.  Each time I click the GetWeight button I get different results AND it is garbage, like  ????, then ?/-=, then ?-=?, etc  - usually different every time.  Please advise me on how to get the weight out of the characters returned.  Here is the code.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' open the serial port   
        ' I have dragged and dropped the SerialPort obj to the form
        If mySerialPort.IsOpen Then
            mySerialPort.Close()
        End If
        Try
            With mySerialPort
                .PortName = myPortName
                .BaudRate = 2400
                .Parity = 0
                .DataBits = 8
                .StopBits = IO.Ports.StopBits.One
                .Handshake = IO.Ports.Handshake.None
                .DtrEnable = True
                .RtsEnable = True
            End With
            mySerialPort.Open()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub cmdGetWeight_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGetWeight.Click
        ' this command makes the scale immediately return the weight  
        Control.CheckForIllegalCrossThreadCalls = False
        mySerialPort.Write("SGW" & vbCrLf)
    End Sub

    Private Sub myDataReceived(ByVal sender As Object, ByVal e As IO.Ports.SerialDataReceivedEventArgs) Handles mySerialPort.DataReceived
        ' this fires right after the Write(sgw) thing. the app gets here fine
        Dim myScaleOutput As String = ""
        myScaleOutput = mySerialPort.ReadExisting
        ' characters are written to the textbox
        boxMultiLine.Text = myScaleOutput
    End SubStart Free Trial
 
Loading Advertisement...
 
[+][-]10.31.2007 at 05:48AM PDT, ID: 20184861

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: .Net Editors & IDEs, Microsoft Visual Basic.Net, Microsoft Development
Tags: serialport, datareceived, rs232
Sign Up Now!
Solution Provided By: TheLearnedOne
Participating Experts: 1
Solution Grade: A
 
 
[+][-]10.31.2007 at 06:24AM PDT, ID: 20185196

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628