Shop OBEX P1 Docs P2 Docs Learn Events
Sending bytes from Spinneret to Visual Basic — Parallax Forums

Sending bytes from Spinneret to Visual Basic

MoskogMoskog Posts: 554
edited 2013-03-03 11:52 in Accessories
I been working on finding a way to transfer bytes from the Spinneret to VB without having to depend on HTML. I have some experience with programming VB from the VB 5.0 back in time but have very little experience with VB.NET or -net related programming. But so far I have made an application where I can receive and display HTML-based information on VB just like a browser. My spinneret is currently loaded with a modified version of Mike's HTMLGraph.spin. (Just added my own html-code and a few additions to the ParseResource-method).
What I would like to do is to be able to receive data like single bytes or packets of bytes from the Spinneret into VB through the Internet.
Like if I send the request from VB: 10.0.0.130:5000/sendData, the Spinneret should respond with sending the byte or the chain of bytes I want, clean without any html-tags.
I really believe this is a quite simple task but I have to admit I'm stuck. No more ideas on how.
I do think I need another addition to the ParseResource but I have no ideas on how it should look like.
So here I come creepin', asking for a good advice or a little snippet that can push me back on the track again!

Spinneret: HTMLGraph.spin
VB: Visual Basic 2010 Express

Comments

  • doggiedocdoggiedoc Posts: 2,241
    edited 2013-02-20 09:53
    I think electromanj was working on something and VB.NET. Let me find the link.


    Here: http://forums.parallax.com/showthread.php/144918-VB.net-and-the-Spinneret
  • Mike GMike G Posts: 2,702
    edited 2013-02-20 09:56
    Use WebServer_5100.spin. It has excellent support for XML and the HTTP parsing is much better. All you do is make an HTTP request like normal. XML is returned as opposed to HTML. .NET loves XML and the libraries are vast. So all you do is parse the XML and display the text.

    I originally designed the Spinneret libraries for RESTful services. However, the community for some reason latched onto HTML. I guess I need to build another example.
  • Mike GMike G Posts: 2,702
    edited 2013-02-20 19:05
    Attached is a working example of how to retrieve values from the Spinneret using XML and a Windows app. This example uses the latest Spinneret Web Server with all the bells and whistles.
    http://code.google.com/p/propeller-w5200-driver/downloads/list

    Make sure you get the zip that goes with the Spinneret => WebServer_W5100_RTC - Archive [Date 2013.02.20 Time 07.00].zip <= if you have a Spinneret. It will also work with the yet to be released WizNet fo5200 for Quick Start.

    Unzip the attached .NET C# project and open the project in VS 2010. If you do not have Visual Studio or don't feel like installing VS for C# then simply drill into the folder HttpClientDemo\bin\Release.

    You must update the app.config to point to your Spinneret IP address.
        <applicationSettings>
            <HttpClientDemo.Properties.Settings>
                <setting name="serviceUrl" serializeAs="String">
                    <value>http://[B]192.168.1.110[/B]/pinstate.xml?led={0}&amp;value={1}</value>
                </setting>
            </HttpClientDemo.Properties.Settings>
        </applicationSettings>
    

    Execute (double click) HttpClientDemo.exe or run the project. It's pretty simple, turn LED 23 on and off. Play with the PIN and/or value textboxes.

    The .NET code is open and free - happy programming.
  • MoskogMoskog Posts: 554
    edited 2013-02-20 22:28
    Thank you, Mike, I really do appreciate this.
    Not much experience with C# here but I do have the Visual Studio with both VB and C# installed so maybe this is the time to convert, hehe. Should not be a too much of a difference.

    Anyway, I will check out this tonight when back again from work.


    Thanks again!
  • Mike GMike G Posts: 2,702
    edited 2013-02-21 05:50
    Visual Basic version attached
  • MoskogMoskog Posts: 554
    edited 2013-02-21 08:23
    Mike G wrote: »
    Visual Basic version attached

    Awesome!

    Repeating over and over... really do appreciate your help!
  • TumblerTumbler Posts: 323
    edited 2013-02-21 20:07
    I originally designed the Spinneret libraries for RESTful services.
    Which is cool Mike
    However, the community for some reason latched onto HTML. I guess I need to build another example.
    Plz no! ;)
    Use an xsl stylesheet for the xml. I use it all the time.
  • MoskogMoskog Posts: 554
    edited 2013-03-01 13:54
    Ok, Im playing with the WebServer_W5100_RTC.spin and so far I have done some progress with sending numbers to the Spinneret and getting a response back. Both from a web browser and Visual Basic. But I do have some problems with formatting the numbers, the original version allowed me to send a two-digit number to the Spinneret and also receive a two-digit number. I have done a few modifications to be able to send a 3-digit number and recieve this number x 2. Just for testing. But I am not able to figure out how to both send two-digit and three-digit numbers (or other numbers) without changing the spin-code. The current version only works for numbers from 100 and greater.
    Here is my additions or modifications to the .spin file:
    In DAT section:
    xmlPessmar    byte  "<root>", CR, LF, "  <Incoming>" 
      incoming      byte  $30, $30, $30,"</Incoming>", CR, LF,"<Returning>"
      returning     byte  $30, $30, $30, "</Returning>" , CR, LF,                            {
    }                     "</root>", 0
    

    In PRI RenderDynamic(id):
    if(strcomp(req.GetFileName, string("pessmar.xml")))
        BuildValue( req.Get(string("incoming")) )
        BuildAndSendHeader(id, -1)
        sock[id].Send(@xmlPessmar, strsize(@xmlPessmar))
        return true
    

    Added methode:
    PRI BuildValue(strvalue) |  value , newval
     
      value := StrToBase(strvalue, 10)
      value:= value*2 
      pst.str(string("Incoming: "))
      pst.str(strvalue)
      if(strsize(strvalue) > 1)
        bytemove(@incoming,strvalue, 3)
      else
        byte[@incoming] := $30
        byte[@incoming][1] := byte[strvalue]
        
      newval:= dec(value)
    
      pst.str(string(" Returning: "))
      pst.str(newval)
    
      if(strsize(newval) > 1)
        bytemove(@returning,newval, 3)
      else
        byte[@returning] := $30
        byte[@returning][1] := byte[newval]
    

    So if I type http://la3usa.com:5000/pessmar.xml/incoming/444
    I receive this from the spinneret:

    <root>
    <Incoming>444</Incoming>
    <Returning>888</Returning>
    </root>

    which is correct, but if I type things like http://la3usa.com:5000/pessmar.xml/incoming/45

    I recieve this:

    <root>
    <Incoming>45

    ..see, no returning value at all. In VB I also get an error message.

    Sorry for all this strange behaviors here but cant help for it!
  • Mike GMike G Posts: 2,702
    edited 2013-03-01 15:40
    Don't add a zero terminator to the end of the string $34, $35, $0. The original code added zeros chars in front of the number chars; ie. $30, $34, $35. I guess it's not like that anymore?

    You could also use a space character; ie $34, $35, $20. Regardless, you must update the spin source because a zero terminator in a text based protocol messes everything up.
  • Mike GMike G Posts: 2,702
    edited 2013-03-02 09:13
    Here's how to pad a string with whatever char you like.
    PRI BuildPinStateXml(strpin, strvalue) | pin, value, state, dir
      pin := StrToBase(strpin, 10)
      value := StrToBase(strvalue, 10)  
    
      SetPinState(pin, value)
      state := ReadPinState(pin)
    
      'Write the pin number to the XML doc
      PadLeft(@pinNum, strpin, 3, "0")
    
      'Write the pin value
      value := Dec(ReadPinState(pin))
      PadLeft(@pinState, value, 3, "0")
    
      'Write Pin direction
      dir := Dec(ReadDirState(pin))
      PadLeft(@pinDir, dir, 3, "0")
    
    PRI PadLeft(dest, source, len, padChar)
      'Fill the destination buffer with the padding char
      if(strsize(source) < len)
        bytefill(dest, padChar,  len)
    
      'Write the string offset from the start of the destination buffer
      bytemove(dest+len-strsize(source), source, strsize(source))
    
    PRI PadRight(dest, source, len, padChar)
      'Fill the destination buffer with the padding char 
      if(strsize(source) < len)
        bytefill(dest, padChar,  len)
        
      'Write to the start of the destination buffer
      bytemove(dest, source, strsize(source))
    
  • MoskogMoskog Posts: 554
    edited 2013-03-03 08:21
    Thanks again, the Padleft-methode removed much of the problems here and I now have the first VB to Spinneret application running pretty well.

    What's still confuses me a little is the hexadecimals, like in line 70 in your original code:
    pinNum        byte  $30, $30, "</pin>", CR, LF, "  <value>"
    

    I figure out they are some kind of placeholders and in this case they mean "0", "0". But I cant find a good explanation in the Propeller Manual (DAT-section) and it seems I can use any number here, also $32 or $33 and still get the same result. But I guess I have to stay in the range of $30- $39, anything else will just print the actual ascii-sign, right?
  • Mike GMike G Posts: 2,702
    edited 2013-03-03 11:52
    Moskog, 0x30 or $30 is the ASCII encoded character for "0". Use whatever character you like. Being the value is a fixed width integer string, left padded zeros work well when converting the string to an integer on the client.
Sign In or Register to comment.