Shop OBEX P1 Docs P2 Docs Learn Events
Prop+ethernet example... — Parallax Forums

Prop+ethernet example...

KamPuttyKamPutty Posts: 48
edited 2008-07-20 21:35 in Propeller 1
Hi all,

Well, I'm trying to get a simple "hello world" working showing how to create a tcp/ip server. I've taken code from around, and have created my first test showing a server waiting on port 80 (web)...and just waits for a connect...then I can expand that.

Unfortunately, it does not wait, it always "connects"... shakehead.gif ...me thinks (me knows! nono.gif ) I've done something wrong!

Here is the sample code

' simple ethernet test

CON

  _clkmode = xtal1 + pll16x
  _xinfreq = 5_000_000

OBJ

  term          : "TV_Text"
  settings      : "settings"
  nic           : "driver_socket"
   
DAT

MAC           BYTE      $de, $ad, $be, $ef, $ca, $fe
IP            BYTE      10, 200, 1, 142
MASK          BYTE      255, 255, 255, 0
GATEWAY       BYTE      10, 200, 1, 1
DNS           BYTE      10, 0, 0, 1

VAR

  long stack[noparse][[/noparse]100]
  
PUB init | i

  settings.start
  settings.setData(settings#NET_MAC_ADDR,@MAC,6)  
  settings.setData(settings#NET_IPv4_ADDR,@IP,4)
  settings.setData(settings#NET_IPv4_MASK,@MASK,4)
  settings.setData(settings#NET_IPv4_GATE,@GATEWAY,4)
  settings.setData(settings#NET_IPv4_DNS,@DNS,4)
  
  ' Start the TV Terminal
  term.startWithMode(12,term#MODE_NTSC)

  if settings.getData(settings#NET_MAC_ADDR,@stack,6)
    term.str(string("MAC     : "))
    repeat i from 0 to 5
      if i
        term.out("-")
      term.hex(byte[noparse][[/noparse]@stack][i],2)
    term.out(13)  

  if settings.getData(settings#NET_IPv4_ADDR,@stack,4)
    term.str(string("IP      : "))
    repeat i from 0 to 3
      if i
        term.out("-")
      term.hex(byte[noparse][[/noparse]@stack][i],2)
    term.out(13)      

  if settings.getData(settings#NET_IPv4_MASK,@stack,4)
    term.str(string("Mask    : "))
    repeat i from 0 to 3
      if i
        term.out("-")
      term.hex(byte[noparse][[/noparse]@stack][i],2)
    term.out(13)      

  if settings.getData(settings#NET_IPv4_GATE,@stack,4)
    term.str(string("Gateway : "))
    repeat i from 0 to 3
      if i
        term.out("-")
      term.hex(byte[noparse][[/noparse]@stack][i],2)
    term.out(13)

  if settings.getData(settings#NET_IPv4_DNS,@stack,4)
    term.str(string("DNS     : "))
    repeat i from 0 to 3
      if i
        term.out("-")
      term.hex(byte[noparse][[/noparse]@stack][i],2)
    term.out(13)

' lets start the nic services

' p0 = cs
' p1 = sck
' p2 = si
' p3 = so
' p4 = int
' p5 = *NOTHING CONNECTED*

  if not \nic.start(0, 1, 2, 3, 4, 5)
    term.str(string("Unable to start networking!"))
    waitcnt(clkfreq*10000 + cnt)
    reboot

  term.str(string("Networking Started"))
  term.out(13)

  term.str(string("Listening on port 80"))
  term.out(13)

  ' lets wait for a connetion
  repeat while \nic.listen(80) == -1

  term.str(string("Connected!"))
  term.out(13)
[/i][/i][/i][/i][/i]



Looking at the code for the

  repeat while \nic.listen(80) == -1





it does not look like it blocks, hence the repeat...

All I have set my IP's...etc...

Thoughts?

~Kam (^8*

Comments

  • Harrison.Harrison. Posts: 484
    edited 2008-07-18 00:35
    You need to loop on isConnected(...). You can view the documentation for driver_socket by clicking the 'Documentation' radio button at the top of the PropTool. It'll tell you how to use the methods and which ones are blocking/nonblocking.

    So do something like this:

    PUB main | handle
      repeat
        handle := \sock.listen(80)
        repeat until \sock.isConnected(handle)
    
        ' your normal handler stuff here
        sock.writeByte(handle, "H")
        
        \sock.close(handle)
    
    



    EDIT: Changed writeByte example. I forgot it only writes one byte...

    Post Edited (Harrison.) : 7/18/2008 12:47:17 AM GMT
  • KamPuttyKamPutty Posts: 48
    edited 2008-07-18 00:54
    Okay, I checked out the docs (if it where a snake it would have bit me!), changed my code to this

    ' lets start the nic services
    
    ' p0 = cs
    ' p1 = sck
    ' p2 = si
    ' p3 = so
    ' p4 = int
    ' p5 = *NOTHING CONNECTED*
    
      if not \nic.start(0, 1, 2, 3, 4, 5)
        term.str(string("Unable to start networking!"))
        waitcnt(clkfreq*10000 + cnt)
        reboot
    
      term.str(string("Listening on port 80"))
      term.out(13)
    
      repeat
        handle := \nic.listen(80)
        repeat until \nic.isConnected(handle)
    
        ' your normal handler stuff here
        nic.writeByte(handle, string("<html><body>Hello World!</body></html>"))
        nic.close(handle)
    
    



    Now it just sits and waits... shakehead.gif

    The plot thickens!

    ~Kam (^8*
  • Harrison.Harrison. Posts: 484
    edited 2008-07-18 01:53
    You should only use driver_socket.spin in your code. driver_socket uses driver_enc28j60.spin internally for the low level interface.

    Also, writeByte(...) only writes a single byte. You need api_telnet_serial.spin if you want to be able to use FullDuplexSerial style methods.

    EDIT: You might want to add some more debug statements to see where it gets stuck.

    Post Edited (Harrison.) : 7/18/2008 2:01:30 AM GMT
  • KamPuttyKamPutty Posts: 48
    edited 2008-07-18 15:46
    Hi all,

    Well, smaller test program, same non-working issue shakehead.gif

    Here is the smaller code

    ' simple ethernet test
    
    CON
    
      _clkmode = xtal1 + pll16x
      _xinfreq = 5_000_000
    
    OBJ
    
      term          : "TV_Text"
      settings      : "settings"
      nic           : "driver_socket"
       
    DAT
    
    MAC           BYTE      $10, $00, $00, $00, $00, $01
    IP            BYTE      10, 200, 1, 142
    MASK          BYTE      255, 255, 255, 0
    GATEWAY       BYTE      10, 200, 1, 1
    DNS           BYTE      10, 0, 0, 1
    
    VAR
    
      long stack[noparse][[/noparse]100]
      long handle
      
    PUB init | i
    
      settings.start
      settings.setData(settings#NET_MAC_ADDR,@MAC,6)  
      settings.setData(settings#NET_IPv4_ADDR,@IP,4)
      settings.setData(settings#NET_IPv4_MASK,@MASK,4)
      settings.setData(settings#NET_IPv4_GATE,@GATEWAY,4)
      settings.setData(settings#NET_IPv4_DNS,@DNS,4)
      settings.setData(settings#NET_DHCPv4_DISABLE, string("true"), 4)  
      ' Start the TV Terminal
      term.startWithMode(12,term#MODE_NTSC)
    
    ' lets start the nic services
    
    ' p0 = cs
    ' p1 = sck
    ' p2 = si
    ' p3 = so
    ' p4 = int
    
      if not \nic.start(0, 1, 2, 3, 4, -1)                  ' -1 = use external clock
        term.str(string("Unable to start networking!"))
        waitcnt(clkfreq*10000 + cnt)
        reboot
    
      term.str(string("Listening on port 80"))
      term.out(13)
    
      repeat
        handle := \nic.listen(80)
        repeat until \nic.isConnected(handle)
    
        ' your normal handler stuff here
        term.str(string("Conected on port 80"))
        term.out(13)
    
        'push "A" 10 times to client
        repeat 10
          nic.writeByte(handle, 65) 
        nic.close(handle)
    
    



    Looking thru the code, it looks like it will use dhcp if we tell it *NOT* to use it...I think I did the setting correctly...

    So I want my ip to be 10.200.1.142...

    I have not change any code in the driver etc...

    Any thoughts why it does not connect?

    ~Kam (^8*
  • Harrison.Harrison. Posts: 484
    edited 2008-07-18 23:00
    It looks like you are using darco's implementation. His code supports ping, so you should try pinging your device to see if it responds. If it does then we at least know the network settings are correct.

    You could also add a check to make sure nic.listen(80) returns a valid handle. You can check for negative values (socket allocation errors) or just call nic.isValidHandle(handle) to let the code do all the internal checks.
  • KamPuttyKamPutty Posts: 48
    edited 2008-07-20 21:35
    Hi all,

    Well, no luck so far...I've had nothing but computer problems all day! shakehead.gifshakehead.gifshakehead.gif

    So this is my latest code, it connects to the network (should use dhcp)

    I try to connect to http://74.125.19.104 (one of googles servers) and just print the recieved data

    But nothing! BLA!

    Here is the complete code...

    ' simple ethernet test
    
    CON
    
      _clkmode = xtal1 + pll16x
      _xinfreq = 5_000_000
    
    OBJ
    
      term          : "TV_Text"
      settings      : "settings"
      nic           : "driver_socket"
       
    DAT
    
    MAC           BYTE      $10, $00, $00, $00, $00, $01
    IP            BYTE      10, 200, 1, 142
    MASK          BYTE      255, 255, 255, 0
    GATEWAY       BYTE      10, 200, 1, 1
    DNS           BYTE      10, 0, 0, 1
    
    REMOTEIP      BYTE      74, 125, 19, 104        ' www.google.com
    
    VAR
    
      long stack[noparse][[/noparse]100]
      long handle
      byte rxChar
      
    PUB init | i
    
      settings.start
      settings.setData(settings#NET_MAC_ADDR,@MAC,6)        ' set MAC  
      settings.setData(settings#NET_IPv4_ADDR,@IP,4)        ' set MY IP
      settings.setData(settings#NET_IPv4_MASK,@MASK,4)      ' set MASK
      settings.setData(settings#NET_IPv4_GATE,@GATEWAY,4)   ' set Gateway
      settings.setData(settings#NET_IPv4_DNS,@DNS,4)        ' set DNS
      
    
      ' Start the TV Terminal
      term.startWithMode(12,term#MODE_NTSC)
    
    ' lets start the nic services
    
    ' p0 = cs
    ' p1 = sck
    ' p2 = si
    ' p3 = so
    ' p4 = int
    
      if not \nic.start(0, 1, 2, 3, 4, -1)                  ' -1 = use external clock
        term.str(string("Unable to start networking!"))
        waitcnt(clkfreq*10000 + cnt)
        reboot
    
      term.str(string("Connecting to http server..."))
      term.out(13)
    
      handle:=nic.connect(@REMOTEIP, 80, 80)
      term.str(string("Handle = "))
      term.dec(handle)
      term.out(13)
    
      ' wait to connect
      term.str(string("Waiting to connect..."))
      term.out(13)
      repeat until nic.isConnected(handle)
    
      ' read in all the chars forever
      repeat 
       rxChar:=nic.readByte(handle)
       term.out(rxChar) 
    
    



    Any thoughts people?

    AAHHHHHHHHHHHHH smhair.gifsmhair.gifsmhair.gifsmhair.gifskull.gifskull.gifskull.gifskull.gif

    ~Kam (^8*
Sign In or Register to comment.