SERIN and the scratchpad
Wrigly
Posts: 9
Have a B2SE, my question concerns using SERIN and the scratchpad area. I need to take a text string rather long and put it in scratchpad area and then send it back out (SEROUT) a little later . I have seen the PUT and GET commands but see that they are byte only. I am familar with the arrays and was wondering if it was possible to still do this with the scratchpad. Needles to say I am rather new to stamps but so far very excited by them so far, any help would be great.
Thanks Wrigly
Post Edited By Moderator (Dave Andreae (Parallax)) : 10/10/2005 8:23:03 PM GMT
Thanks Wrigly
Post Edited By Moderator (Dave Andreae (Parallax)) : 10/10/2005 8:23:03 PM GMT
Comments
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Wrigly
If the text stream is coming in pretty quickly this is much more likely to work on any of the 'P' series Basic Stamps. They can basically write directly to scratchpad from SERIN. If you try doing it byte by byte SERIN then PUT you may miss characters if you aren't using flow control.
As far as dealing with a variable length string that is pretty easy on the 'P' series. First thing to do is set the entire scratchpad ram to 0x00 in bytes.
Then perform the SERIN redirected to scratchpad of up to 126 bytes. Include a timeout in that call and have both a full buffer and a timeout go to the same section of code. In that code read the first word out of scratchpad, if it is equal to 0x00 then no data has arrived and you loop.
If it is not 0x00 you can find out how many bytes were received by checking the scratchpad till you hit 0x00. From there you can do whatever you need to do with the byte string you have received. After you are done clear scratchpad and start over.
An EOM (end of message) character is an excellent idea, so long as it's well chosen, and won't appear elsewhere in the data. Any other method of fielding a data stream of unknown length, which has no inherent length field within, that doesn't consider the incoming data is ripe for failure under the right set of circumstances.
Beside all that, knowing the end of data delimiter beforehand makes scanning the string much easier from a programming point of view. In fact, if possible, and depending on the application, you might want to consider stuffing the (binary) length in (only one byte required), next to the EOM character either on the incoming input data or in the output, if it's written back out for further processing down the line. This can used to validate the data, and also to ease some of the programming if the length of the incoming data is needed.
Just some thoughts.
Regards,
Bruce Bates
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
·· I wanted to make sure you understood how the SPSTR function works.· If you're using it to bring a fixed length string into Scratchpad RAM then you cannot rely on 0x00 to determine the end of the string, nor to terminate it, since 0x00 could be within the characters being stored.· The SPSTR function will input the number of characters you specify into the Scratchpad RAM starting at location 0 regardless of value.· The only thing you can do is use what you know, which is the number of characters brought in, since you specify that anyway.· Just wanted to clarify that for everyone, I got the impression you were referring to that command since you mentioned the BS2p in the sentence prior to that.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com
Chris,
Except that the length of characters he needs to grab is variable... IOW unknown. So you can't just specify a length for SPSTR and then work off that length as what may be received is less then that. Without a timeout you won't get the return from SERIN until enough packets come in to hit the length of the call. Likely in many situations waiting for x number of packets won't really work well.
Hence the need to use a timeout in the SERIN command along with SPSTR.
Once you introduce the timeout you then need a way to find out if you actually received any data or not. As you can hit the timeout with or without receiving any data. SERIN doesn't return how many bytes were actually received by the call.
Hence the need to set Scratchpad to some value that won't be received in SERIN and check against that.
If the first byte/word of scratchpad is that null value you then know no data was received on that SERIN call.
However if the first byte/word isn't that null value you know data has arrived. By then checking Scratchpad byte by byte till you hit the null value you then know the number of bytes returned by SERIN and can then act accordingly on the data.
Once you are finished with the received data and want to wait for the next data packet you reset Scratchpad completely to the null value and loop to the SERIN command again.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com
I was refererring to using the SPSTR function actually. Like you said if you deal with it byte by byte you already know what has been received.
I was using the SPSTR function because at higher baud rates that is the only way I was able to get reliable data reception. Trying to deal with data byte by byte as it came in resulted in lost bytes.
With the SPSTR I was getting reliable rececption up to 115,200 as long as the packet was 126 bytes or less.
·· I think I see what you are suggesting...Correct me if I am wrong here.· I figured if I was confused by what you meant, so would the OP, and others.· Let me know if I have this now...
·· You are using the SPSTR function to get data into the ScratchPad at high speed.· When it times out, you go to read what was stuffed in there, not knowing how many bytes.· So now what needs to be done is he will need to find a unique character to identify the end of the data.· 0x00 may not work.· Needs to be something that won't normally come in via serial.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com
That is pretty much it. You would want to fill the Scratchpad with a value you likely won't be receiving from SERIN. To further help avoid accidentally triggering this checking I ended up looking for the end in WORDs. In other words looking for a WORD value of 0x00 0x00. So even if I received a Byte of 0x00 in the middle of the SERIN string that wouldn't trigger the end of string checking because the word value wouldn't be 0x00 0x00.
There is also one additional check that if I haven't found the END by byte 126 of the Scratchpad ram then exit and work with what I have as the receive filled the Scratchpad.