Shop OBEX P1 Docs P2 Docs Learn Events
Accepting variable data stream using SERIN — Parallax Forums

Accepting variable data stream using SERIN

PBasicFanPBasicFan Posts: 5
edited 2009-01-21 16:47 in BASIC Stamp
Hi, new at this so this may be a pretty basic question, I have a BS2 stamp and require data to be input at 19k2 from a PLC, the data stream can be up to 120 bytes long (I do realise that I will ulimately require a BS2p) there is no handshaking and no unique end character apart from a checksum which will obviously vary, embedded within the data is the data length but that will not be availabe until·all the data has been received.·I am aware of special formatter STR ByteArray \L (\E) but this requires either the data length or a unique end character, how can I receive the variable length data with for example:

SERIN 16,16416,[noparse][[/noparse]STR indata\120]

As it stands it requires 120 bytes input before it moves on, I have also tried using the Timeout function but this does not help, maybe I am using it incorrectly? Below is an example which I am using to test but once again, anything less than 22 bytes and it waits for the rest of the data

indata VAR Byte(22)··· 'Array populated by incoming data
counter VAR Byte······

main:

counter = 0

SERIN 16,16416,2000,no_data,[noparse][[/noparse]STR indata\22]

FOR counter = 0 TO·21
·SEROUT 1,16416, [noparse][[/noparse]HEX2 indata(counter)," "] 'Debug
·'SEROUT 1,16416, [noparse][[/noparse]indata(counter)] 'Docklight
NEXT

·SEROUT 1,16416,[noparse][[/noparse]CR]

GOTO main

no_data:
SEROUT 1,16416, [noparse][[/noparse]"No data",CR]
GOTO main

One other question, I ultimately want to use an OEM board, at the moment I have a PIC16C57c microcontroller fitted, if I use a BS2p24 is it just a matter of replacing it with the SX48AC (the speed of 20MHz is the same)

Apologies to everyone if some of these questions are nonsensical

Comments

  • stamptrolstamptrol Posts: 1,731
    edited 2009-01-19 13:18
    At 19K2 there is a good possibility that the Stamp will not be able to successfuly fill the array as the data comes along.

    Its more usual (and reliable) to work at 2400 or 4800 with the Stamp if you need to manipulate much data.

    The Timeout may seem to not work correctly because it resets whenever any data on the serial port is detected.

    As to variable length/no common characters, something will have to give. Suppose a short string arrives. If the PC remains quiet until the next transmisssion, I think you'll just grab enough characters to to fill the string and things will continue. You,of, course, have to figure out where the split is in the data.

    What kind of data are you receiving? Its really not common for so much data to be sent with no unique parts. Are there no repeatable spaces, brackets, commas, etc?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tom Sisk

    http://www.siskconsult.com
    ·
  • PBasicFanPBasicFan Posts: 5
    edited 2009-01-19 14:38
    Thanks for your quick response, the baud rate is set at 19200 from the PLC and I have to use this for compatability, realistically the general amout of data will be approx 40 to 50 bytes, the only common reference are the first two bytes CC AA, below will be a typical data string, which is the initial status request string:

    CC AA 00 00 00 01 00 00 00 01 AA 12 1B 00 CC 09 DB 00

    the 12th byte is the length of the string but of course it will be no use until the whole string has been received, what I was wondering was whether there was a way for the stamp to detect data flow/no data flow, alternately Timeout could be set to a value less than the quiet space between data strings if I could set it in the correct configuration
  • Mike GreenMike Green Posts: 23,101
    edited 2009-01-19 15:41
    It sounds like you will have to use a BS2p or BS2px to get better reliability with the high speed input. The Stamps are mostly ok at 9600 Baud and definitely marginal at 19200 Baud although having a fast Stamp will help there. The scratchpad ram is the only way you're going to handle the amount of data you're talking about.

    Using an SX48AC is not a drop-in replacement for a Stamp. SX/B is not the same as PBasic although there are a lot of features and syntax that are the same or close which makes conversion easier. You might also consider a Propeller since it very happily handles 19200 Baud with lots of processing power to spare and normally does buffering of the incoming data.
  • PBasicFanPBasicFan Posts: 5
    edited 2009-01-19 16:23
    Thanks Mike, I have had a quick look at the Propeller Spec, will study it further, at this moment in time though I would still like to use the Basic Stamps, the BS2px would certainly give me the better speed and as you say I would need the extra scratchpad ram. A couple of questions though, firstly, I am using a board of education to write and test my code with the Basic BS2, this might sound strange but what do I need to ultimately put my code in a "package" as a finished unit, the OEM kit I assumed was for the BS2 what would I use for the BS2px?

    The second question was the start of this topic, how do I detect the end of variable length data when I have no flow control and no unique EOF characters that I can trap, any advice would be much appreciated
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2009-01-19 17:33
    Hello,

    I have used the BS2P in the past to capture long strings of data from such devices as GPS modules and other consumer electronics equipment, but I don’t recall ever doing this at 19.2Kbps. Without testing it, I’m not sure if the BS2P can get the data at that rate, but I can answer the question of how to capture the variable length strings.

    If you look at the example codes for our PINK module you’ll see the BS2P examples use the SPSTR formatter for serial input. Since they don’t know how long the data will be they assume the maximum length of a variable on the PINK, which is 64 bytes. The SERIN command then uses a short timeout to break from input if nothing is received in that many milliseconds. It is not a perfect solution, but it does work.

    Of course, on the PINK the data is being requested, so the timeout is affected by simply waiting for data. You may or may not be able to use this depending on if your PLC is just sending data at random or fixed intervals. If that is the case you may have to use some clever tricks to get the data since the beginning of the data does not contain a fixed header that you can wait for. Take care.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Engineering
  • Mike GreenMike Green Posts: 23,101
    edited 2009-01-19 17:47
    Even with a BS2p or BS2px, you're really at the edge of what a Stamp can handle. If you were trying to handle a datastream that fits how the PBasic statements work (with a terminating character or fixed length), I'd say "go for it", but you're trying to manage something that doesn't fit how things work. You really need to use some other microcontroller, like an SX or Propeller that has some spare horsepower available.
  • PBasicFanPBasicFan Posts: 5
    edited 2009-01-20 11:44
    Thanks very much to you both, I have ordered a BS2px use special formatter SPSTR L and will see if that resolves the problem, the Propeller is a possibility for future projects but at the moment due to the different language it uses I will leave it, by the way Chris I do have a start of data header CC AA to look\wait for in the incoming code

    Regards and thanks
  • PBasicFanPBasicFan Posts: 5
    edited 2009-01-21 16:47
    Just as a matter of interest, I received and fitted the BS2px and with the following code I have acheived what I required, location 11 is the byte which contains the string length so that I use further on in the program, if it is still at zero then I loop back. I have also found that data input at 19200 seems to be ok although with long cable runs will have to be checked, Thanks for all your help

    waitData:
    PUT 11,0

    SERIN 16,16572,2000,skipnext,[noparse][[/noparse]SPSTR 126]

    skipnext:
    GET 11,temp
    IF temp = 0 THEN GOTO waitData

    temp = 0
    GET 11, temp1
Sign In or Register to comment.