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