Sub Class_Globals
Dim mPort As String
Dim mObject As Object
Private sp As Serial
Private const BAUDRATE As Int = 9600 '256000
Private astream As AsyncStreams
Dim LogMap As Map
Dim mEmulation As Boolean, timEmul As Timer
Dim emulation_sensor_id As Int
Dim emulation_sensor_qty As Int = 1
End Sub
'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize (CallbackObject As Object, port As String, emul As Boolean, emul_id As Int) As Boolean
mObject = CallbackObject
mEmulation = emul
LogMap.Initialize
mPort = port
emulation_sensor_id = emul_id
If mEmulation Then
timEmul.Initialize("timEmul", 1500)
timEmul.Enabled = True
Return True
End If
sp.Initialize("sp")
Try
sp.Open(port)
sp.SetParams(BAUDRATE, 8, 1, 0)
astream.Initialize(sp.GetInputStream, sp.GetOutputStream, "astream")
Catch
Dim e As String = port & " busy, passed by"
Log(e)
Main.toast.Show(e)
CallSubDelayed2(mObject, "Show_Logs", e)
Return False
End Try
Log(port & " подключен")
others.AddToLog(Null, port & " connected")
Return True
End Sub
private Sub AStream_NewData (Buffer() As Byte)
Dim s As String = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
LogMessage(s)
others.Parse_RawData(s, mPort)
End Sub
private Sub AStream_Error
Log("Error: " & LastException)
Disconnect
AStream_Terminated
End Sub
private Sub AStream_Terminated
Log(mPort & ": " & "Connection is broken.")
CallSubDelayed2(mObject, "Show_Logs", mPort & ": " & "Connection is broken.")
others.AddToLog(Null, mPort & ": " & "Connection is broken.")
CallSubDelayed2(mObject, "Broken_Port_Connection", mPort)
End Sub
Sub Disconnect
astream.Close
sp.Close
End Sub
private Sub LogMessage(Msg As String)
CallSubDelayed2(mObject, "Show_Logs", mPort & ": " & others.AddToLog(LogMap, Msg))
End Sub
private Sub timEmul_Tick
Dim l As List
l.Initialize
Dim emulated As String = "<sensor>" & emulation_sensor_id & " "
l.Add(emulated)
Dim k As Int = Rnd(1, 5)
For i = 0 To emulation_sensor_qty - 1
emulated = Chr(13) & Round2((Rnd(20000, 25000)/100/k).As(Float),2) & " " & Chr(10) & Chr(13) & Chr(10) & Chr(10)
l.Add(emulated)
Next
emulated = "</sensor>"
l.Add(emulated)
For i = 0 To l.Size - 1
others.Parse_RawData(l.Get(i), mPort)
Next
'others.Parse_RawData(emulated, mPort)
End Sub
Sub Send (txt As String)
If txt.Length > 0 Then
astream.Write(txt.GetBytes("utf8"))
End If
End Sub