Hello Martin
This is the code doing the capture.
Public Function GetWeight() As Double
Dim TheWeight As Double
' See which protocol this weight bridge uses, reads the data, then
' return it.
' We are just hard coding this right now, a cleaner implementations is required
Try
TheWeight = CDbl(Mid(GetRawData(CommandType.cmdAscii, False, "5"), 4, 5))
'TheWeight = CDbl(Mid("x 09010kg", 3, 5))
Catch ex As Exception
TheWeight = 0
MsgBox("Error reading scale, try getting reading again. If error persists contact administrator")
End Try
Return TheWeight
End Function
''' This method basically sends a command to the weightbridge, then waits
''' for the reply back. We assume that validation has already
''' been done that if the command type is ASCII, then the proper
''' values has been sent. This is used to get raw data. While reading
''' and sending the data, it may time out because the weightbridge is not
''' ready, as such we have to check the time outs and retry.
''' </summary>
''' <param name="aCommandType">The type on which the command is, its either
''' an ascii character or a string character</param>
''' ''' <param name="aCommandText">The command to send to the weightbridge</param>
''' <param name="anIncludeCRLF">Whethter a neew line character should be added at the end of the command.</param>
''' <returns>the bytes converted as string returned by the weightbridge</returns>
''' <remarks></remarks>
Public Function GetRawData(ByVal aCommandType As clsWeightBridgeManager.CommandType, ByVal anIncludeCRLF As Boolean, _
ByVal aCommandText As String) As String
Dim MyCommandText As String
Dim MySerialData As String
Dim MyNumTrial As Integer
' We build the command to send
' If my command type is ascii, then we are receiving the command as a string
' representing an ascii char between 0 and 255
If (aCommandType = CommandType.cmdAscii) Then
MyCommandText = Chr(CInt(aCommandText))
Else
MyCommandText = aCommandText
End If
' Now we verify if we have to add a carriage return
If (anIncludeCRLF) Then
MyCommandText = MyCommandText & Microsoft.VisualBasic.vbCrLf
End If
' We ensure that the data received is empty
MySerialData = ""
' We open the port and try sending the command to the weightbridge
Try
TheSerialPort.Open()
TheSerialPort.DiscardOutBuffer()
TheSerialPort.DiscardInBuffer()
TheSerialPort.Write(MyCommandText)
Catch e As Exception
' soem stupd thing happened
MsgBox("Could not open port")
End Try
MyNumTrial = 4
MySerialData = TheSerialPort.ReadExisting
TheSerialPort.DiscardInBuffer()
' it could have been that we did not get any data yet, so we
' try a couple more times
While ((MyNumTrial > 0) Or (MySerialData = ""))
' we try the reading 4 times as .25 second interval to get the data
Thread.Sleep(250)
' we read again
MySerialData = MySerialData & TheSerialPort.ReadExisting
TheSerialPort.DiscardInBuffer()
MyNumTrial = MyNumTrial - 1
End While
' We flush the buffers and close
TheSerialPort.DiscardInBuffer()
TheSerialPort.DiscardOutBuffer()
TheSerialPort.Close()
Return MySerialData
End Function