Retrieve Weight From Mettler Toledo Scale via RS232

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 11239 Views - Last Post: 17 October 2012 - 10:50 PM Rate Topic: -----

#16 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Retrieve Weight From Mettler Toledo Scale via RS232

Posted 15 October 2012 - 11:19 PM

I gave up on the example code, and tried using a couple of Code Snippets (available in VS 2010), and managed to read serial data from my BlueTooth GPS. I didn't have anything handy to test sending, but I added the sendData routine anyway, but it's untested. I've provided two lines in the sendData subroutine. One sets the baud rate, and the other doesn't. I recommend first trying the one that doesn't set it, just for the heck of it.

My reasoning is that many USB-Serial adapters are autobaud, and will take any standard rate, auto-setting to the serial side. Note that in the readData routine, I did not set any serial parameters at all, yet it worked anyway.

Start a new project, make a form with 1 TextBox, 1 ComboBox, and three buttons. I've added code to the Snippet to stop the transfer, place the data into the TextBox, and to clear the TextBox.

Give it a try and see if you have any better luck. If this doesn't work, you might want to try setting serial port parameters.

Imports System.Text
Imports System.IO.Ports

Public Class Form1
    Private keepgoing As Boolean = True

    Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        For Each portName In My.Computer.Ports.SerialPortNames
            comboPort.Items.Add(portName)
        Next
        comboPort.Text = comboPort.Items.Item(0)
    End Sub

    Private Sub readData()
        Dim buffer As New StringBuilder
        Using comPort = My.Computer.Ports.OpenSerialPort(comboPort.Text)
            Do
                Dim line = comPort.ReadLine()
                If line Is Nothing Then
                    Exit Do
                Else
                    buffer.AppendLine(line)
                    TextBox1.Text &= line
                End If
                Application.DoEvents()
                If Not keepgoing Then
                    comPort.Close()
                    Exit Do
                End If
            Loop
        End Using
    End Sub

    Private Sub sendData(txt As String)
        Using comPort = My.Computer.Ports.OpenSerialPort("COM1", 2400)
        ' Using comPort = My.Computer.Ports.OpenSerialPort("COM1", 2400)
            comPort.DtrEnable = True
            comPort.Write("S" & vbCrLf)
            ' All data transfer code goes here.
        End Using
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        keepgoing = True
        readData()
    End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        keepgoing = False
    End Sub

    Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
        TextBox1.Clear()
    End Sub
End Class


Was This Post Helpful? 0
  • +
  • -

#17 mikyerii  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 10-October 12

Re: Retrieve Weight From Mettler Toledo Scale via RS232

Posted 16 October 2012 - 12:42 AM

I tried the code, but the form stopped responding when reaching this particular line of the code:

Dim line = comPort.ReadLine()


It was not able to proceed to the IF statement...when should i call the senddata sub routine?
Was This Post Helpful? 0
  • +
  • -

#18 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Retrieve Weight From Mettler Toledo Scale via RS232

Posted 16 October 2012 - 05:11 AM

Take a look at this, specifically the bottom post, it's seems awful close to how I remember doing this but as I stated earlier, it's been over close to 15 years :(
Was This Post Helpful? 0
  • +
  • -

#19 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Retrieve Weight From Mettler Toledo Scale via RS232

Posted 16 October 2012 - 06:37 AM

View Postmikyerii, on 16 October 2012 - 12:42 AM, said:

I tried the code, but the form stopped responding when reaching this particular line of the code:

Dim line = comPort.ReadLine()


It was not able to proceed to the IF statement...when should i call the senddata sub routine?


When I tested, it did the same thing when no data was arriving on the port. It should probably be in a thread of its own.

Try the sendData() to start the process, I think, then listen using readData().

oops! I see I misinterpreted what you sent to start the process. It should be an ASCII STX character, which is Chr(2), rather than the "S" I put in the data to be sent. So try sending that: comPort.Write(Chr(2) & vbCr), or perhaps, as is shown in the link that CharlieMay just sent, comPort.Write(Chr(2) & "P" & vbCr)

I'll see if I can dig up my USB 4-port hub and a null-modem cable for further testing.
Was This Post Helpful? 0
  • +
  • -

#20 mikyerii  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 10-October 12

Re: Retrieve Weight From Mettler Toledo Scale via RS232

Posted 17 October 2012 - 06:12 PM

It's still the same problem, it would not proceed to the next line. :(

By the way in this particular manual that CharlieMay sent previously,

Mettler Toledo IND310 Technical Manual

I used the continuous mode of the port, If incase it would help us in determining the problem.. it is found on page E-1 of the manual/ page 169 of the PDF file.
Was This Post Helpful? 0
  • +
  • -

#21 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Retrieve Weight From Mettler Toledo Scale via RS232

Posted 17 October 2012 - 08:26 PM

View Postmikyerii, on 17 October 2012 - 06:12 PM, said:

It's still the same problem, it would not proceed to the next line. :(

Could be because there is no NewLine, and the operation is a ReadLine().

I've changed it to a ReadChar() in my test program, and it's obvious by the TextBox display that it is reading one character at a time. See if that changes things. Here is a screen shot of my test:

Posted Image

And here is the changed code. I expect that if this works, the data will still be unreadable, but then at least we can change the code to display the hexadecimal values, and that might help us to see what's wrong.
    Private keepgoing As Boolean = True

    Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        For Each portName In My.Computer.Ports.SerialPortNames
            comboPort.Items.Add(portName)
        Next
        comboPort.Text = comboPort.Items.Item(0)
    End Sub

    Private Sub readData()
        Dim buffer As New StringBuilder
        Using comPort = My.Computer.Ports.OpenSerialPort(comboPort.Text)
            Do
                Dim line = comPort.ReadChar
                buffer.Append(Chr(line))
                TextBox1.Text &= Chr(line)
                Application.DoEvents()

                If Not keepgoing Then
                    comPort.Close()
                    Exit Do
                End If
            Loop
        End Using
    End Sub

    Private Sub sendData()
        Using comPort = My.Computer.Ports.OpenSerialPort(comboPort.Text)
            comPort.DtrEnable = True
            'comPort.Write(Chr(2) & vbCrLf)
            comPort.Write(Chr(2) & "P" & vbCrLf)
            ' All data transfer code goes here.
            comPort.Close()
        End Using
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        keepgoing = True
        readData()
    End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        keepgoing = False
    End Sub

    Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
        TextBox1.Clear()
    End Sub

    Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click
        sendData()
    End Sub



Quote

I used the continuous mode of the port, If incase it would help us in determining the problem.. it is found on page E-1 of the manual/ page 169 of the PDF file.

Yes, I saw that. Did you try sending an STX followed by a "P" and a NewLine? I provided two send lines, one of which is commented out
Was This Post Helpful? 0
  • +
  • -

#22 mikyerii  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 10-October 12

Re: Retrieve Weight From Mettler Toledo Scale via RS232

Posted 17 October 2012 - 09:50 PM

Here, it worked but still the same values like before. Yes, I tried using the two send lines but it gives me this values anyway..

Attached Image
Was This Post Helpful? 0
  • +
  • -

#23 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Retrieve Weight From Mettler Toledo Scale via RS232

Posted 17 October 2012 - 10:50 PM

View Postmikyerii, on 17 October 2012 - 09:50 PM, said:

Here, it worked but still the same values like before. Yes, I tried using the two send lines but it gives me this values anyway..


OK... so now we can look at the hexadecimal values returned. Just change the TextBox assign line to TextBox1.Text &= Format(line, "X2") & " " then post the results here. what we see may tell us what's happening (though there's no guarantee).
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2