Sending ascii codes outside the normal 0-127 range.
highplainsbubba
Posts: 34
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.
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
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".
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.
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
http://www.w3schools.com/jsref/jsref_tostring_number.asp
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
[html]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<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]
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?
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.
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.