Sub Process_Globals
'These global variables will be declared once when the application starts.
'Public variables can be accessed from all modules.
Public Serial1 As Serial
Dim eth As Ethernet
Private ethClient As EthernetSocket
Private astream As AsyncStreams
Private serverIp() As Byte = Array As Byte(192, 168, 0, 16)
Private MacAddress() As Byte = Array As Byte(0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED)
Private const serverPort As UInt = 51042
Private ser As B4RSerializator
Private timer1 As Timer
Private PIND0, PIND1, PIND2, PIND3, PIND4, PIND5, PIND6, PIND7 As Pin
End Sub
Private Sub AppStart
Serial1.Initialize(115200)
Log("AppStart")
If eth.InitializeDHCP(MacAddress) = False Then
Log("Error connecting to network.")
Return
Else
Log("Connected to network. My ip address: ", eth.LocalIp)
End If
timer1.Initialize("SEND0", 100)
Connect(0)
End Sub
Sub Connect(unused As Byte)
If ethClient.ConnectIP(serverIp, serverPort) = False Then
Log("trying to connect again")
CallSubPlus("Connect", 1000, 0)
Return
End If
Log("Connected to server")
astream.Initialize(ethClient.Stream, "Astream_NewData", "Astream_Error")
End Sub
Sub SEND0
If PIND0.DigitalRead=True Then
Dim output As String = "7seg0"
End If
If PIND1.DigitalRead=True Then
Dim output As String = "7seg1"
End If
If PIND2.DigitalRead=True Then
Dim output As String = "7seg2"
End If
If PIND6.DigitalRead=True Then
Dim output As String = "7seg3"
End If
If PIND7.DigitalRead=True Then
Dim output As String = "7seg4"
End If
astream.Write(ser.ConvertArrayToBytes(Array(output)))
End Sub
Sub astream_NewData (Buffer() As Byte)
Dim be(10) As Object 'used as a storage buffer.
Dim objects() As Object = ser.ConvertBytesToArray(Buffer, be)
' Log("Recieved:")
For Each o As Object In objects
Log(o)
Select Case o
Case "OUTPUT1 T"
PIND3.DigitalWrite(True)
Case "OUTPUT1 F"
PIND3.DigitalWrite(False)
Case "OUTPUT2 T"
PIND4.DigitalWrite(True)
Case "OUTPUT2 F"
PIND4.DigitalWrite(False)
Case "OUTPUT3 T"
PIND5.DigitalWrite(True)
Case "OUTPUT3 F"
PIND5.DigitalWrite(False)
End Select
Next
End Sub
Sub Astream_Error
Log("Error")
timer1.Enabled = False
ethClient.Close
CallSubPlus("Connect", 1000, 0)
End Sub