0

I'm trying to code in VB.net a SAG105 METTLER TOLEDO that is connected to a pc using the Cable LC-RS9 RS232 9 pin for IBM PCXT. I have researched on the internet but was unable to find a template code that can assist me to send data to the weighting scale device using VB.net. I had a look at the code for serial port, but as this is a RS-232 for IBM PCXT, this serial port method didn't work. I had contacted the company as I didn't have any documentation for scale, unfortunately they said they don't have any template code for the device.

Could someone please help me to get started on opening the communication channel and sending the basic commands by guiding me to the right website or sharing some of your knowledge. I have spend days already on this and have gone no where with it.

Many thanks in advance

I managed to find a decent code for VB that works, however I'm having some problems. So the commands that I will use are "Z" - setting the scale to zero, "SIR" read unstable value continually, "S" getting stable value, "SI" - read unstable value once.

However, the problem I'm having is that the routine to send command runs once, but the receive command runs twice. When it runs the first time I get the correct value but second time I get ES (means syntax error the balance didn't recognize the command). I put some breaks in the see this.

My code is :

Dim myWeight As String, myWeightValue As Single
Delegate Sub SerialDataIn()
Public myDelegate As SerialDataIn

Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    CloseSerialPort() ' Close the port
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    'This opens the serial Port
    OpenSerialPort()
    'Assign the delegate for the Serial Communications handling
    myDelegate = New SerialDataIn(AddressOf SerialDataInProc)
End Sub

'HANDLE SERIAL PORT COMMUNICATIONS IN TO THE PC FROM THE BALANCE:
Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
    'Get the weight value string from the balance
    myWeight = SerialPort1.ReadLine
    'Call a new Thread Procedure
    Me.Invoke(myDelegate)
End Sub

'The delegate...
Sub SerialDataInProc()
    'Display the received string in the text box
    Me.TextBox1.Text = myWeight

    'If any string is received that is not prefixed with an 'S' then it is not a weight value, so don't try to process the value
    If myWeight.Substring(0, 1) <> "S" Then GoTo HopIt
    'Convert the string received from the balance into a value
    myWeightValue = Convert.ToSingle(myWeight.Substring(4, 10))
    'If the weight on the balance exceeds 50g then reset the balance (stop data transmission) and display the message "Stopped"
    If myWeightValue > 50 Then
        SerialPortOut("@")
        MsgBox("Stopped")
    End If
HopIt:
End Sub

'Write any command to the balance port
Sub SerialPortOut(ByVal myText)
    Try
        SerialPort1.WriteLine(myText & vbCrLf)
    Catch ex As Exception
        MsgBox("Transmission to Serial Port Failed. Error: " & Err.Description, MsgBoxStyle.Critical, "Comms Failed")
    End Try
End Sub

'Send the Balance the SIR Command (Send current weight value and repeat) if the [SEND SIR] button has been pressed
'Send the @ Command to the balance (Cancels all previous commands, so resets the balance and stops data transmission) if the [Send @] button is clicked
Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonReset.Click, ButtonStart.Click
    Select Case sender.name
        Case Is = "ButtonReset"
            SerialPortOut("@")
        Case Is = "ButtonStart"
            SerialPortOut("S")
    End Select
End Sub

'Set the serial Port Parameters and open the Port
Private Sub OpenSerialPort()

    With SerialPort1
        .PortName = "Com1"
        .BaudRate = 9600
        .DataBits = 8
        .Parity = IO.Ports.Parity.None
        .StopBits = IO.Ports.StopBits.One
        .Handshake = IO.Ports.Handshake.XOnXOff
        Try
            .Open()
        Catch ex As Exception
            MsgBox("Could not open the Serial Port! Error: " & Err.Description, MsgBoxStyle.Critical, "Port Error")
        End Try
    End With
End Sub

'Close the Serial Port
Private Sub CloseSerialPort()
    SerialPort1.Close()
End Sub

lease can someone have a look at this and tell why the above described problem is occurring

Satvir Singh
  • 61
  • 4
  • 16
  • Is there on one can provide any help in this form. Anyone please – Satvir Singh Oct 16 '15 at 13:42
  • I got a decent code that works but has some problems which is described in the edited post – Satvir Singh Oct 29 '15 at 11:14
  • Please could anyone help me. Due to the above problem described in the post, my code is getting more confusing since I need to handle too many things and doing so I still cannot stop some errors from occurring. To get a stable value, I need to the data twice using SIR command and then compare the values but this is not working at the moment. Please can anyone sort this out. Thank You – Satvir Singh Oct 29 '15 at 15:40

1 Answers1

0

The protocol for the SAG105 seems to be the MT-SICS Standard Interface Command Set

Using our Docklight software (even with the free evaluation) you can easily try this out on a "modern" PC with a standard USB-to-RS232 plug.

It seems the protocol consists of very simple text commands, so sending the following text command to the SAG105 should return a current balance value:

I2<CR><LF>

with "CR" = carriage return, ASCII decimal code 13. And "LF" = Line Feed, ASCII decimal code 10.

I couldn't locate the standard COM parameters (e.g 9600 baud, 8 data bits, 1 stop bit, no parity) for the SAGE105, but maybe you have this information already and this could be anyhow set to an application-specific setting by your old application. If you know the RS232 settings, I could actually send you a small example project for Docklight to manually talk to the SAG105.

Within .NET such text based simple protocols should be straightforward to implement, once you have confirmed the settings and the protocol commands you are using are correct.