Take a look at my code and see if you can tell me what I'm doing wrong.
Code: Select all
Program TCPconnector
Device = 18F2620
Clock = 25
// important configurations...
#option ENC28J60_CS = PORTB.3
#option ENC28J60_RST = PORTB.4
#option NET_ICMP = true // enable ping
#option NET_DHCP = true // enable Dynamic Host Configuration Protocol (DHCP)
#option NET_NBNS = true // enable NETBIOS
#option NET_TCP = true // enable TCP
#option STACK_CLIENT_MODE = true
Include "NETApp.bas" // always first!
// TCPIP stack specifics...
Include "NETTypes.bas"
Include "NETUtils.bas"
Include "Tick.bas"
Include "TCP.bas"
Include "DNS.bas"
Include "ARP.bas"
Include "MAC.bas"
// program helpers...
Include "NETAppDisplay.bas"
Include "SUART.bas"
Include "convert.bas"
Dim MySocket As TCP_SOCKET
Dim RemoteHost As NODE_INFO
Dim RemotePort As Word
Dim IPString As String
Dim OnlineState As Boolean
Dim i As Word
Dim ConnectFlag As Boolean
ConnectFlag = False
IPString = "192.168.1.2"
RemotePort = 9090
// program start...
UART.SetBaudrate(sbr9600)
UART.SetTX(PORTC.6)
UART.SetMode(umInverted)
StringToIPAddress(@IPString, RemoteHost.IPAddr)
DelayMS(100)
OnlineState = false
MySocket = INVALID_SOCKET
// loop forever...
While true
// This task performs normal stack task including checking for incoming
// packet, type of packet and calling appropriate stack entity to process it.
NETApp.Task
If NETApp.DHCPEvent() Then
If MACIsLinked = true Then
OnlineState = true
Write("Network connection.",#13,#10)
Else
OnlineState = false
Write("Lost connection",#13,#10)
End If
End If
If OnlineState = True Then
If TCPIsConnected(MySocket) = False Then
ARPResolve(RemoteHost.IPAddr)
Repeat
NETApp.Task
DelayMS(20)
Until ARPIsResolved(RemoteHost.IPAddr, RemoteHost.MACAddr) = true
Write("MAC Resolved",#13,#10)
Repeat
MySocket = TCPConnect(RemoteHost, RemotePort)
Until MySocket <> INVALID_SOCKET
Repeat
NETApp.Task
TCPTick()
DelayMS(10)
Until TCPIsConnected(MySocket) = True
Write("TCP Connection",#13,#10)
Else
Repeat 'all is good. process traffic here
NETApp.Task
DelayMS(100)
Until TCPIsConnected(MySocket) = false Or MACIsLinked = false
Write("TCP Disconnect",#13,#10)
If MACIsLinked = False Then
OnlineState = False
End If
Clear(MySocket)
TCPFlush(MySocket)
End If
End If
Wend