Shop OBEX P1 Docs P2 Docs Learn Events
Sending ascii codes outside the normal 0-127 range. — Parallax Forums

Sending ascii codes outside the normal 0-127 range.

highplainsbubbahighplainsbubba Posts: 34
edited 2012-10-14 16:39 in Accessories
In my previous post, I discovered what I was doing wrong. See "Using Javascript "charCodeAt" to read string characters sent by Spinneret".
The other half of the issue is to find a way to fix it. I am trying to send the string "i0ABC where "i stands for input from a DIO card, "0" is the card address and A,B,C are 3 groups of 8 inputs each and vary from 0 ( all 8 inputs off) to 255( all inputs on). My code works fine when the A,B, and C are within the 0-127 ascii code range but fails miserably when attempting to send the codes in the 128-255 range. I thought of converting the hex value of (for example) 255 (ÿ) to a string of 2,5,5 (one byte each) using the str.toString but don't know what to use as the destination pointer ( PUB ToString(integerToConvert, destinationPointer) ). I would really like to keep the original string intact and send it instead of breaking it up. If anyone knows of a way of sneaking an ascii control code (128-255) through, as a response header, please let me know. I tried to add the % character to the string and that did not help.

Comments

  • SRLMSRLM Posts: 5,045
    edited 2012-10-08 12:44
    If you use the HEX function from a serial object it will convert the hex representation of the number to a string (the '2', '5', '5' in your example). BTW, that's the decimal representation, not the hex representation (which is FF).

    It looks like you'll have to convert the numbers to strings, and back again. At least based on your previous post. Take a look at the various functions in Serial objects (such as FullDuplexSerialPlus) called "dec" and "hex".
  • Mike GMike G Posts: 2,702
    edited 2012-10-08 14:28
    using the str.toString but don't know what to use as the destination pointer ( PUB ToString(integerToConvert, destinationPointer) )

    destinationPointer is a point to HUB memory. It's the location of the result of the integer to string conversion.

    You should be able to send anything in the message body. Ultimately, you should convert the byte types to strings.
  • highplainsbubbahighplainsbubba Posts: 34
    edited 2012-10-08 15:59
    I have included 2 screen shots of a 255,255,255 ascii code response header and a normal ascii 41,42,43 response header. The alert box for each shows the ascii codes received by the browser. The fffdfffdfffd response is received for all values between 128 an 255. I have included 2 screen shots although I don't know how well you can see them. They are pretty small. I don't know how to make them larger, they shrunk on upload. If you really want to see them I can email them.

    Control character over 127.jpg
    control character for ABC.jpg


    For the ToString(integerToConvert, destinationPointer) ) should the variable and the pointer be the same ie

    variable := str.ToString(currentState,variable) or should I make them different ie
    variable := str.ToString(currentState,temp)

    Thanks for the help
    1024 x 297 - 58K
    1024 x 297 - 55K
  • Mike GMike G Posts: 2,702
    edited 2012-10-08 20:23
    It is a little hard to see but It appears that the values were sent to the browser just fine. The weird symbols are the ASCII representation of the values sent. Using charCodeAt() is not going to work because the the values are not characters they are byte types. If you want to view the byte type as a string of characters then you must ASCII encode the byte value. That means the byte value 255 must be converted to three bytes 0x32, 0x35, 0x35 which are the ASCII encoded values for the number 255. In JavaScript try using the toString() method.

    http://www.w3schools.com/jsref/jsref_tostring_number.asp
    var num = 15;
    var n = num.toString(); 
    
  • highplainsbubbahighplainsbubba Posts: 34
    edited 2012-10-12 16:15
    I found the answer to the problem below, so you can ignore this post. I was declaring the variable "dog" in the PUB statement and it was defaulting to a long when it should have been declared a byte in the VAR block. Once that was fixed the problem went away.


    I wrote a code snippet to split the value of a byte (for example 255) into 3 bytes of 2,5,5, but can not get it to work consistently. I think I may be overwriting the memory. I have included the code below. It takes 3 bytes (211,212,213) and splits them into nine bytes Cat[0-8], which are then displayed on the PST. The first 2 bytes (211,212) convert correctly but the last byte does not. I cannot find an error so I think it must be overwriting memory. If I comment out the code for the first byte, the second 2 bytes are converted correctly. If I comment out the second byte, the first one is correct but the third is still wrong. I don't have much experience working with variable memory, so perhaps one of you guys would be good enough to load this up and tell me where I went wrong.

    [code]
    CON _clkmode = xtal1 + pll16x
    _xinfreq = 5_000_000
    VAR
    long StackSpace[20]
    byte Group[2]
    byte Cat[8]
    OBJ
    pst : "Parallax Serial Terminal"
    'vp : "Conduit"
    PUB SplitUp |dog,currentState,direction,addr
    ' vp.share(@Relay,@Total)
    pst.Start(115_200)
    {{ Two DIO cards are set up : One to output to 24 relays, the other to read (input) those outputs to see which relays
    are already on when the web page loads.This program checks the Input card to see which relays are already on and puts
    that into an array (Cat[]).This was necessary because the input card sends a serial string to the spinneret which
    would then normally forward the string to my webpage. Since the serial string sends values outside the ascii range of
    0-127 which browsers cant handle, I needed to convert it to seperate bytes and send that. What I am doing is
    sending a string of value Cat[] which should always fall in the 0-127 range.
    }}




    currentState := string ("i0
  • Mike GMike G Posts: 2,702
    edited 2012-10-13 07:28
    Here's how to decode the values using JavaScript.

    [html]
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
    <html xmlns="http://www.w3.org/1999/xhtml"&gt;
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>

    <body>
    <div id="packet">i ÿîª </div>
    <hr />
    <div id=id>null</div>
    <div id=io>null</div>
    <div id=value1>null</div>
    <div id=value2>null</div>
    <div id=value3>null</div>
    <script language="javascript" type="application/javascript">
    //Get the packet value
    var packet = document.getElementById("packet").innerHTML;

    // Debug
    //var t1 = parseInt(packet.charCodeAt(2));
    //alert(t1.toString());

    // Write the first char
    document.getElementById("id").innerHTML = packet[0];
    // Get the 1 or 0
    document.getElementById("io").innerHTML = packet.charCodeAt(1).toString();
    //Get byte value A
    document.getElementById("value1").innerHTML = packet.charCodeAt(2).toString();
    //Get byte value B
    document.getElementById("value2").innerHTML = packet.charCodeAt(3).toString();
    //Get byte value C
    document.getElementById("value3").innerHTML = packet.charCodeAt(4).toString();
    </script>
    </body>
    </html>
    [/html]
  • highplainsbubbahighplainsbubba Posts: 34
    edited 2012-10-13 15:50
    Thanks for the help. I will look over that when I get the chance but I am still struggling with the spin code.I can't seem to pass a string from the server software to the above file. I want to spend more time on that so don't help me with that just yet. I know it has to be passed by address (prop manual page 289) but the call to the above file just seems to go into the ether. I put a pst.str(string(13, "Leaving Checkstate",13)) where I leave the server software and a pst.str(string(13, " Arriving SplitUp")) and I never get the "Arriving Message.I am sure it is something stupid, I just need to look a bit longer. Once again, thanks for the help.
  • Mike GMike G Posts: 2,702
    edited 2012-10-13 16:57
    Thanks for the help. I will look over that when I get the chance but I am still struggling with the spin code.I can't seem to pass a string from the server software to the above file. I want to spend more time on that so don't help me with that just yet. I know it has to be passed by address (prop manual page 289) but the call to the above file just seems to go into the ether. I put a pst.str(string(13, "Leaving Checkstate",13)) where I leave the server software and a pst.str(string(13, " Arriving SplitUp")) and I never get the "Arriving Message.I am sure it is something stupid, I just need to look a bit longer. Once again, thanks for the help.
    Honestly, I read your post several times and I have no idea what you're talking about.

    You are trying to write to a file? Read a file? What is your expected outcome? How does this file stuff relate to converting a byte value to ASCII?
  • highplainsbubbahighplainsbubba Posts: 34
    edited 2012-10-14 06:56
    As Maxwell Smart would say,, "Sorry about that Chief". I should get my terminology straight.

    I have a method in your "httpserversrestfull" spin file called "checkstate' that calls a method "splitup" in a different spin file (object) called ' splitstring1a". I can't get the call to connect with the method in the other spin file. I intentionally put an error in" splitup", to see if the compiler would connect and catch it, and it did.

    I moved the return statement to the top of the 'splitup" method in splitstring1a ,,below the pst.str(string(13, " Arriving SplitUp")). This makes the pst.str statement the only statement in that method to execute. The program seemed to go to splitstringa1a and return to restfull, but the pst.str(string(13, " Arriving SplitUp")) doesn't show up on the PST.

    I copied the checkstate method, and moved it to the "splitstring1a" spin file and set the spin file up to run independent as a test file. The call from check state to splitup worked fine and all the PST outputs appeared.

    I probably just need to stare at it a bit longer to see what is going on. Once again, thanks for all the help.

    When I am not playing with the Spinneret, I am on lynda.com taking their online javascript, ajax and jquery classes. So little time,,, so much to learn.
  • Mike GMike G Posts: 2,702
    edited 2012-10-14 16:39
    highplainsbubba, I'm still lost. Please explain what you are trying to do at a high level.

    I gleamed from post 10 the following. You are successfully calling a method (PUB) in a child object. However, the result of the PUB is not expected.

    Please post the source code.
Sign In or Register to comment.