Sub StartScale ' called from Resume...
sock.Initialize("Scalecontroller")
sock.Connect("10.131.199.3",10001, 10000) ' Try to connect for up to 10 seconds. If fail to connect, go to Plan B - SetWeight sub..
ToastMessageShow(" Connecting to Scale Controller... ",False)
End Sub
Sub Scalecontroller_Connected (Successful As Boolean)
Log($"Scalecontroller_Connected: (${Successful})"$)
If Successful Then
AStreams.Initialize(sock.InputStream, Null, "Scale") ' Init the IN stream - no out stream required
Else
ToastMessageShow(" Connection to Scale Controller Failed! ",True) ' If AP or something broken... Ask for manual input weight next
SetWeight
End If
End Sub
Sub Scale_NewData (Buffer() As Byte)
Dim w As String = ""
w = BytesToString(Buffer, 0, Buffer.Length, "UTF8") ' temp var for processing in buffer
Log("length of w: "&w.Length&" string: "&w) ' show the length and what is inside...
' Note: serial to ethernet device has a 2k buffer. This will be sent on first open but string is not 17 chars long (2017) - so ignore...
If w.Length = 17 Then ' A valid string IS 17 characters long (no more - no less)
For i = 0 To w.Length-1 ' this was used to see exactly what the string was made of (for testing). Many space characters in it...
Log(" i: "&i& " ["&w.SubString2(i,i+1)&"]") ' what is each character in string
Log(" i: "&i& " ["&Buffer(i)&"]") ' what is each buffer byte value
Next
wghtmsg = w.SubString2(3, 15) ' this is the center (valid weight) portion of the passed buffer. The rest are control - info characters
If w.SubString2(2,3) = Chr(34) Then ' if byte 3 is a " , then the value of weight is negative . ( you can't """ to do this, hence the Chr(34) )
wghtmsg = "-"& wghtmsg.Trim ' append a " - " (minus sign) to the trimmed weight value (get rid of spaces fore and aft)
End If
If w.SubString2(2,3) = "(" Then ' if this char is at location, it means the scale has not yet stabilized (truck is driving on and shaking it)
wghtmsg = "*"&wghtmsg.Trim&"*" ' Add " * " to the string to indicate Not Stable
End If
wghtmsg = wghtmsg.Trim ' trim this public var again
lblweight.Text = wghtmsg.Trim&" lb" ' add " lb" to the label text (not really needed)
End If
Log(" weight: "&lblweight.Text)
End Sub
Sub Scale_Error
ToastMessageShow("Scale Error: "&LastException.Message, True)
Log("Scale_Error: "&LastException.Message)
End Sub
Sub Scale_Terminated
Log("Scale_Terminated")
If AStreams.IsInitialized Then ' close everything if terminated. Also - close in Pause!
AStreams.Close
sock.Close
End If
End Sub
Sub SetWeight ' if mobile device can't reach AP, then have driver type in the weight
' from the digital display on the scale controller (which most always works - 99.8%)
Dim i As Int
Dim xlo As String
Dim kbrd As Phone
xlo = "" ' NumberFormat2(lo ,1,1,1,False)
Dim param As BD_InputBoxParams
Dim bd As BetterDialogs
param.Initialize
param.Default = xlo
param.InputType = param.INPUT_TYPE_DECIMAL_NUMBERS
param.InputTextSize = 24
param.Question = "Provide The Vehicle Weight"
param.QuestionTextSize = 28
param.SpaceBetween = 15dip
param.Multiline = False
i = bd.InputBox(" Controller Connection Failed! ",param,"OK","Cancel","",DefCM.BLC)
If i = DialogResponse.POSITIVE Then
wghtmsg = param.Answer
End If
kbrd.HideKeyboard(Activity)
End Sub