I try to do my first project with TCP/IP. I tried all samples but nothing working. FOr example the code below. Ping working but UDP communication not. I check communication with the Easy TCP/IP
[code:1:d05356db4f]$Regfile="m1284pdef.dat"
$Crystal=8000000
$hwstack=80
$swstack=64
$framesize=80
$baud = 19200
Config Spi = Hard , Interrupt = Off , Data Order = Msb , Master = Yes , Polarity = Low , Phase = 0 , Clockrate = 4 , Noss = 0
Spiinit
Config PinB.2 = Output
W5200_nreset Alias PortB.2
Set W5200_nreset
Config PinB.3 = Input
W5200_nint Alias PortB.3
Reset W5200_nreset
Waitms 1
Set W5200_nreset
Waitms 350
Print "Init TCP"
Enable Interrupts
Config Tcpip = Noint , _
Mac = 0.11.22.33.44.55 , _
Ip = 172.18.0.200 , _
Submask = 255.255.255.0 , _
Gateway = 172.18.0.1 , _
Localport = 5000 , _
Chip = W5200 , _
Spi = 1 , _
Cs = PortB.4
Dim Idx As Byte ' socket number
Dim Result As Word ' result
Dim S(255) As Byte
Dim Sstr As String * 255
Dim Temp As Byte , Temp2 As Byte ' temp bytes
Const Showresult = 1
Print "UDP demo"
Dim Ip As Long
Ip = Maketcp(172.18.0.51) 'assign IP num
'like with TCP, we need to get a socket first
'note that for UDP we specify sock_dgram
Idx = Getsocket(idx , $02 , 5000 , 0) ' get socket for UDP mode, specify port 5000
Print "Socket " ; Idx ; " " ; Idx
'UDP is a connection less protocol which means that you can not listen, connect or can get the status
'You can just use send and receive the same way as for TCP/IP.
'But since there is no connection protocol, you need to specify the destination IP address and port
'So compare to TCP/IP you send exactly the same, but with the addition of the IP and PORT
Do
Temp = Inkey() ' wait for terminal input
If Temp = 27 Then ' ESC pressed
Sstr = "Hello"
Result = Udpwritestr(ip , 5000 , Idx , Sstr , 255)
Elseif Temp = 32 Then 'space
Do
Waitms 200
Dim Tel As Long : Incr Tel
Sstr = "0000000000111111111122222222223333333333 " + Str(tel)
Result = Udpwritestr(ip , 5000 , Idx , Sstr , 255)
Loop
End If
Result = Socketstat(idx , Sel_recv) ' get number of bytes waiting
If Result > 0 Then
Print "Bytes waiting : " ; Result
Udpreadheader Idx ' read the udp header
#if Showresult
Print
Print Peersize ; " " ; Peeraddress ; " " ; Peerport ' these are assigned when you use UDPREAD
Print Ip2str(peeraddress) ' print IP in usual format
#endif
If Peersize > 0 Then ' the actual number of bytes
Print "read" ; Peersize
Temp = Udpread(idx , S(1) , Peersize) ' read the result
#if Showresult
For Temp = 1 To Peersize
Print S(temp) ; " " ; ' print result
Next
Print "done"
#endif
Result = Udpwrite(ip , Peerport , Idx , S(1) , Peersize) ' write the received data back
End If
End If
Loop
'the sample above waits for data and send the data back for that reason temp2 is subtracted with 8, the header size
End[/code:1:d05356db4f]
↧