Maybe I don't understand network communication. I try send a string from PC to atmega with UDP protocol. If I press ESC, atmega send the string Hello to PC. It is ok but then I think that the program stay before condition Result because Result = 0. When will value of Result > 0 ?
[code:1:4771b8456e]$Regfile="m644pdef.dat"
$Crystal=8000000
$hwstack=128
$swstack=128
$framesize=128
$baud = 9600
Config Spi = Hard , Interrupt = Off , Data Order = Msb , Master = Yes , Polarity = Low , Phase = 0 , Clockrate = 4 , Noss = 0
'Init the spi pins
Spiinit
Print "Init , set IP to 172.18.0.200" ' display a message
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
Enable INTERRUPTS
Config Tcpip = Noint , Mac = 12.128.12.34.56.78 , Ip = 172.18.0.200 , Submask = 255.255.255.0 , Gateway = 172.18.0.1 , Localport = 1000 , Chip = W5200 , Spi = 1 , Cs = Portb.4
'config pinc.1 = output
'portc.0 = 1
Dim Idx As Byte ' socket number
Dim Result As Word ' result
Dim S(80) As Byte
Dim Sstr As String * 20
Dim Temp As Byte , Temp2 As Byte ' temp bytes
'--------------------------------------------------------------------------------------------
'When you use UDP, you need to dimension the following variables in exactly the same order !
'--------------------------------------------------------------------------------------------
Declare Function Ipnum(ip As Long) As String ' a handy function
'like with TCP, we need to get a socket first
'note that for UDP we specify sock_dgram
Idx = Getsocket(idx , Sock_dgram , 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
UDPREADHEADER idx
Temp = Inkey() ' wait for terminal input
If Temp = 27 Then ' ESC pressed
Sstr = "Hello"
Result = Udpwritestr(172.18.0.51 , 5000 , Idx , Sstr , 255)
print result
End If
Result = Socketstat(idx , Sel_recv) ' get number of bytes waiting
If Result > 0 Then
Print "Bytes waiting : " ; Result
Temp2 = Result - 8 'the first 8 bytes are always the UDP header which consist of the length, IP number and port address
Temp = Udpread(idx , S(1) , Result) ' read the result
For Temp = 1 To Temp2
Print S(temp) ; " " ; ' print result
Next
Print
Print Peersize ; " " ; Peeraddress ; " " ; Peerport ' these are assigned when you use UDPREAD
Print Ipnum(peeraddress) ' print IP in usual format
Result = Udpwrite(172.18.0.51 , Peerport , Idx , S(1) , Temp2) ' write the received data back
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
'this function can be used to display an IP number in normal format
Function Ipnum(ip As Long) As String
Local T As Byte , J As Byte
Ipnum = ""
For J = 1 To 4
T = Ip And 255
Ipnum = Ipnum + Str(t)
If J < 4 Then Ipnum = Ipnum + "."
Shift Ip , Right , 8
Next
End Function
End
[/code:1:4771b8456e]
↧