#Region Project Attribute
#AutoFlushLogs: True
#CheckArrayBounds: True
#StackBufferSize: 1000
#End Region
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, 11)
Private LocalIp() As Byte = Array As Byte(192, 168, 0, 12)
Private MacAddress() As Byte = Array As Byte(0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED)
Private DnsServer() As Byte = Array As Byte(192,168,0,1)
Private Gateway() As Byte = Array As Byte(192,168,0,1)
Private Subnet() As Byte = Array As Byte(255,255,255,0)
Private const serverPort As UInt = 51042
Private ser As B4RSerializator
Private timer1 As Timer
Dim PIND1, PIND2, PIND3, PIND4, PIND5, PIND6 As Pin
Dim PIND1PLC As Byte = 36
Dim PIND2PLC As Byte = 37
Dim PIND3PLC As Byte = 38
Dim PIND4PLC As Byte = 54
Dim PIND5PLC As Byte = 55
Dim PIND6PLC As Byte = 56
End Sub
Private Sub AppStart
Serial1.Initialize(115200)
Log("AppStart")
eth.InitCS(10)
eth.InitializeAdvanced(MacAddress,LocalIp,DnsServer,Gateway,Subnet)
timer1.Initialize("SEND0", 100)
Connect(0)
PIND1.Initialize(PIND1PLC, PIND1.MODE_OUTPUT)
PIND2.Initialize(PIND2PLC, PIND2.MODE_OUTPUT)
PIND3.Initialize(PIND3PLC, PIND3.MODE_OUTPUT)
PIND4.Initialize(PIND4PLC, PIND4.MODE_INPUT)
PIND5.Initialize(PIND5PLC, PIND5.MODE_INPUT)
PIND6.Initialize(PIND6PLC, PIND6.MODE_INPUT)
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")
timer1.Enabled = True
End Sub
Sub SEND0
If PIND4.DigitalRead=True Then
Dim output As String = "7seg0"
End If
If PIND5.DigitalRead=True Then
Dim output As String = "7seg1"
End If
If PIND6.DigitalRead=True Then
Dim output As String = "7seg2"
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 "OUT1 T"
PIND1.DigitalWrite(True)
Case "OUT1 F"
PIND1.DigitalWrite(False)
Case "OUT2 T"
PIND2.DigitalWrite(True)
Case "OUT2 F"
PIND2.DigitalWrite(False)
Case "OUT3 T"
PIND3.DigitalWrite(True)
Case "OUt3 F"
PIND3.DigitalWrite(False)
End Select
Next
End Sub
Sub Astream_Error
Log("Error")
timer1.Enabled = False
ethClient.Close
CallSubPlus("Connect", 1000, 0)
End Sub