Shop OBEX P1 Docs P2 Docs Learn Events
SERIN — Parallax Forums

SERIN

arnold113arnold113 Posts: 43
edited 2015-06-28 06:47 in BASIC Stamp
I've got a position control module sold by Dimension Engineering that I'm having trouble getting data into my BS2 from. I give it a command of SEROUT 14, 84, ["1,getp", CR] ( requesting the position of #1 axis) The control module answers with the position. A 1 followed by a , followed by a p (for position) followed by the position data xxxx then an enter key. My problem is trying to extract the position data. When I try SERIN 13, 84, [DEC BAND_POS] the BS2 inputs a 1 (#1 axis number). I tried SERIN 13, 84, [WAIT ("p"), DEC BAND_POS] but the program just waits and waits and waits. I've tried every combo of the WAIT command I can think of but it still locks up waiting. My program is very dependent on knowing the position of #1 and #2 axis. Any help you can give will be appreciated.
Thanks, Arnold
«1

Comments

  • SapphireSapphire Posts: 496
    edited 2015-06-02 22:06
    It's sometimes hard for the BS2 to keep up with 9600 bps with the WAIT modifier, and the DEC formatter.

    If the expected incoming data is 1,p####<CR> always, try:
    SERIN 13,84,[SKIP 3,DEC4 BAND_POS]
    

    This might work, but maybe not if the data is very fast.

    In that case, try this:
    array VAR Byte(8)
    
    SERIN 13,84,[STR array\8\CR]
    

    which will read in up to 8 bytes into array or stop at a <CR>. Then DEBUG the array, one character at a time to confirm what you expect:
    FOR x = 0 TO 7
      DEBUG array(x)
    NEXT
    

    if that works, then you will have to convert the digits in the array to your number:
    BAND_POS = (array(3)-"0" * 1000) + (array(4)-"0" * 100) + (array(5)-"0" * 10) + (array(6)-"0")
    

    This assumes that valid data is in the array. You should probably check it first, array(0) = "1" array(1) = "," and array(2) = "p" to make sure.

    Once the SERIN command is done, you can re-use the array() bytes for other things until the next time you use the SERIN command again.
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2015-06-03 13:23
    You might also try

    char VAR BYTE
    SERIN 13,84,[char,DEC4 BAND_POS]

    Processing a single ascii character is faster than processing a DECimal value. The Stamp will ignore the leading p in the ensuing p#### .

    Many devices have only one stop bit between bytes and that does not give enough time for the Stamp to complete any fancy processing. Can you select two stop bits for the device?

    I agree totally with Sapphire about the STRing method. After the initial setup time, it is by far the fastest way for the BS2 to capture serial input.
  • arnold113arnold113 Posts: 43
    edited 2015-06-03 16:03
    I left out some much needed info concerning the SERIN and the Kangaroo (the position control module). The "p" in the data package indicates if the axis is in position or still moving. A lower case p for still moving and an upper case P for in position. What I am trying to do is send a get position command and if it is a lower case p I continue scanning the matrix keypad and then get position again until the axis is within 1 inch of the commanded position and then I stop scanning the keypad and just do get position until the P is an upper case P then I can start axis 2 movement. I'm going to need a lot more program space than the BS2 has so I'm going to get another Basic Stamp. Is there one that is fast enough to handle the SERIN [WAIT] command? I tried the code that Sapphire gave but the BS2 couldn't handle it. I tried to slow down the Kangaroo to 2400 baud but it wouldn't let me and I cannot add stop bits either so it looks like this BS2 isn't going to handle this project.
    Thanks, Arnold
  • SapphireSapphire Posts: 496
    edited 2015-06-03 19:51
    I'm surprised that the STR array didn't work, I've used that up to 38400 baud with no problems. Exactly what output did you get from the DEBUG? If it was all garbage, then you may have true instead of inverted data, and need to change the baud flag to accommodate that. Add 16384 for true (non-inverted) data (i.e. 16468).

    There are faster BS2 stamps, namely the BS2sx and BS2p which will work at 9600 baud using WAIT and both have 8x as much program space. The BS2p also let you read data from SERIN into scratch pad memory, which you can then decode without using the array() technique. And there is a BS2px which is the fastest, but there's no way you need that for 9600 baud.

    Post your code if you need more help.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2015-06-03 23:35
    I'm going to jump in here and offer one more suggestion...if you can somehow show a terminal capture or some other example of exactly what is sent, byte-by-byte, I can program a BASIC Stamp 2 or Propeller to emulate that device and then test code on the BS2. I've done this many time over the years when faced with a piece of hardware I don't have access to. Often using the datasheets or an example capture I can emulate the device to help debug code on my end. Does the device have a datasheet or documentation you can link to that shows the output?
  • arnold113arnold113 Posts: 43
    edited 2015-06-04 04:52
    Sapphire, I'll try the STR array again today. I didn't get any output on debug screen. I must not have gotten the code right. Will the BS2p40 work? I need the program storage and the extra I/O pins. Will it have more variables available? I see where it has more I/O RAM but it has the same 26 variables as the BS2. I need more variables. I have designed and built a portable band saw sawmill that will be computer controlled. It will have an extensive program. I've got it about finished. Being fairly new at Basic programing, my code is made up of a lot of IF THEN statements. I'm sure someone else could have done a much better job, but I'm all I've got and I'm too old a dog to be taught new tricks.
    Thanks for your help, Arnold
  • arnold113arnold113 Posts: 43
    edited 2015-06-04 05:26
    Chris, There isn't a data sheet listed on their website but there is a manual for the kangaroo. Here's a link to their web site. http://www.dimensionengineering.com/products/kangaroo
    When given a get position command the kangaroo responds with 1 or 2 (axis number) followed by a comma then a P or a p (P for axis in position and p for axis moving) followed by a four or five digit number for position (depending on which axis) then all this is followed by return newline (CR). Com is 9600 baud, 8N1 but can be increased in baud rate but cannot be decreased in baud rate because the kangaroo can’t distinguish between serial and servo signals at 2400 baud.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2015-06-04 13:54
    I was about to sit down and write the emulation test for this when I realized there's a parameter I am missing which could affect the BS2 ability to receive the response properly. I found something like this while troubleshooting code for a customer using the Emic 2 TTS with a BASIC Stamp. What was happening in this case was that the Emic 2 was responding to the command before the BS2 was ready to receive the response resulting in the BS2 waiting forever for a response that had already passed. So I am wondering if you have a scope or logic analyzer so you can see how long between the end of the command (SEROUT) and the response from the device?
  • tomcrawfordtomcrawford Posts: 1,129
    edited 2015-06-04 14:52
    What was happening in this case was that the Emic 2 was responding to the command before the BS2 was ready to receive the response resulting in the BS2 waiting forever for a response that had already passed. So I am wondering if you have a scope or logic analyzer so you can see how long between the end of the command (SEROUT) and the response from the device?

    uOLED-128 has exactly this problem with BS-2. I was able to just ignore the responses, but that is obviously not a solution here.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2015-06-04 15:18
    That's why I want to rule that out before troubleshooting the code since I would basically be writing an emulator. It could work on my end and not his.
  • arnold113arnold113 Posts: 43
    edited 2015-06-04 21:06
    Sorry, my o scope is broke right now. High voltage out. I just haven't had the time to fix it yet. I tried SERIN 13,84,[STR array\8\CR] again today but still couldn't get any thing reliable from it. Sometimes I got garbage but most times I got nothing, just locked up. The only thing that has been reliable was SERIN 13, 84, [DEC4 BAND_POS]. It would give 0001 every time except when changed to #2 axis. Then it gave 0002 then so I'm sure it was the first digit it was reading. I tried SERIN 13, 84, [SKIP 3, DEC4 BAND_POS] but it just locked up again. It would appear to me that the BS2 is able in this case to respond fast enough seeing it can catch the first number in the data stream 1,pxxxx, CR
  • SapphireSapphire Posts: 496
    edited 2015-06-04 21:56
    It sounds like you don't have the protocol right. The STR will work if the DEC works, but it is expecting 8 bytes, ending with <CR>. If you don't have 8, or no <CR> it will hang. Try a lower number of bytes, say 6, as in [STR array\6] and no final terminator to see what you get. Is xxxx always 4 digits?
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2015-06-05 09:54
    arnold113 wrote: »
    It would appear to me that the BS2 is able in this case to respond fast enough seeing it can catch the first number in the data stream 1,pxxxx, CR

    Not necessarily. If the BS2 is missing the initial bytes it may be catching one from after the "p". If you do like Sapphire mentioned and dump the entire string into an array you should be able to see all characters received, regardless of what they are. To prevent the "lock-up" you could also put a time-out on the SERIN and then display what you did get. In doing so I would preload the array with null values so you know what values are from the most recent try. Actually if you pre-load the array with CRs you shuld be able to DEBUG the array as text with one statement.
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2015-06-05 11:16
    The Kangaroo manual is explicit that the commands will end with a CRLF, but the length may vary depending on the command and various error codes that may occur. I believe you have it operating in the "simplified serial" mode rather than the "binary packet" mode, right (a dip switch setting)?

    I agree that the fact that the "1" or "2" was received with the DEC modifier does give hope to receive the full response using the STR modifier. The big unknown is as Chris pointed out time it takes between issuing the command and the point at which the Stamp has interpreted the SERIN command and is actually ready to receive the data. It takes the Stamp a little less than a millisecond to do that and to be ready. If the Kangaroo starts to send back the response with only 100 microseconds of delay, the Stamp will get off on the wrong foot and won't receive any of the data correctly. Once the Stamp is ready, the STR method is by far the fastest. Any other modifiers or things like flow control or timeouts will lengthen the time it takes the Stamp to interpret the command and to be ready.

    Definitely if you can, the BS2p is the way to go. It is substantially faster and has more memory. Also, it has the SPSTR command, which directs the serial input quickly with timeout to the scratchpad RAM, to be retrieved using the GET command. You asked about the limited amount of RAM memory. The BS2p too has only 26 bytes of regular RAM, but it also has 128 bytes of scratchpad RAM. That is great for values that change less often, PUT and GET. There is also 32kbytes of EEPROM organized into 8 slots of 2kbytes each, where you can store program modules and/or data.
  • arnold113arnold113 Posts: 43
    edited 2015-06-07 03:55
    I haven't had the time the last couple days to work on this problem and won't be able to for a few more days due to heavy work load. I'm going to switch to a BS2p40 so hopefully I won't have a problem with SERIN anymore. I've got to have more program space anyway. The only reason I was programing in Simplified serial instead of packet serial was to save space. With packet serial I'll have parity check which will help insure the commands are reaching the Kangaroo intact. When you're sawing a log you sure don't want miscommunication with the hardware. It'll probably be a few days before I can order the BS2p40 but when I do get programed I'll be back with the results of this problem and some more questions I'm sure.
    Thanks for everyone's help, Arnold
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2015-06-08 07:45
    When you do get back into this and get the new module, remember that the BS2p40 has different values for the same baud rate. There are a few commands that have different values for the same parameters due to timing differences between the different models.
  • arnold113arnold113 Posts: 43
    edited 2015-06-11 20:57
    Thanks Chris, I would have had to find that out the hard way. Thanks for your help.
    Arnold
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2015-06-12 08:06
    Arnold,

    No problem. You have inspired me to do a test though to find the response time of the BS2 on the SERIN. I was thinking I would create a quick test that used a Propeller to receive a command from the BS2 and respond each time narrowing the response window until the BS2 misses the response. This would give me some ideas on the minimum response time. I would probably try the test with a few various formatters to see how things vary.
  • arnold113arnold113 Posts: 43
    edited 2015-06-23 03:31
    I have received my BS2p40 but I have a problem. My laptop only has USB outputs. No DB9 serial output, so I can't program the BS2p. Is there a way to use the USB port?
  • PublisonPublison Posts: 12,366
    edited 2015-06-23 04:12
    Parallax makes an adapter just for that:

    https://www.parallax.com/product/28030
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2015-06-23 23:29
    28031 if you need the USB cable. =)
  • arnold113arnold113 Posts: 43
    edited 2015-06-24 04:54
    Thanks, Chris. I just got it ordered.
  • PublisonPublison Posts: 12,366
    edited 2015-06-24 08:44
    28031 if you need the USB cable. =)

    I'd gladly post for free one of the USB cables that I got with my 8 Radio Shack Quick Starts. :)

    They are multiplying!
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2015-06-24 08:48
    On my bench you can never have too many USB cables. I often have 3 or 4 connected at the same time. Add the Saleae Logic Analyzer and there's another one. =)
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2015-06-24 15:30
    Arnold,

    I wanted to let you know that I did some empirical testing to try and determine what the minimal response time would be for a BS2 SERIN command following the SEROUT command. So what I did was wrote a program to simulate the BS2 sending a command to a controller and getting a response back from it. The controller is just a Propeller chip programmed to respond to the command and return a response. I used your position command as an inspirational guideline. Hopefully this information will help others. I am planning on doing more testing with various formatters and other options, but for now this is how it works.

    The BS2 sends out the following command:
    SEROUT TX, Baud, ["!POS", channel, CR]
    

    The channel is actually cycling each time through the loop from 0 through 3. The Propeller is waiting for the "!POS" and then grabs the next character as the channel and then the CR. It then waits a predetermined amount of time and then responds with the following information, "p<channel><delay low byte><delay high byte><checksum>" where the channel is a single byte from 0-3, the delay is the number of microseconds (sent as low byte then high byte) and the checksum is a single byte additive checksum.

    The BASIC Stamp 2 then receives this data and if it is good it sends an ACK to the Propeller chip. If not it sends a NAK to the Propeller chip. When the Propeller chip gets an ACK it subtracts 1 from the delay value and then waits for the next command. If it gets a NAK it adds one. In this manner we determine at what point the BS2 becomes unresponsive to the response from the Propeller.

    In my testing I found that the delay that caused a NAK was at 380 microseconds, while the previous value which generated an ACK was at 425 microseconds. Granted there is a small gap in there that I still need to hone in on, but for now it is safe to say that the BS2 requires about 425 microseconds to prepare for a SERIN of the 5 bytes I am receiving in the above example. 380 is too short a time. And I know I have seen devices that respond in the <100 microsecond time range causing the BS2 to miss their response.

    For example a customer contacted us stating that his Emic 2 TTS was hanging up and not responding to the volume command. It turns out that the code is designed such that when a command is issued the BS2 waits for the prompt ":" before executing any more commands. When you're sending speech it takes time to say things and therefore the BS2 will happily wait for the prompt and issue the next command. However when you send a volume command the Emic 2 TTS responds in approximately 300 microseconds, which as we saw above is too fast for the BS2 to respond.

    This stuff is easy to see on a scope or logic analyzer if you have one. Not so much without. Parallax carries the Saleae Logic 4 to help find such issues. I hope this helps.

    https://www.parallax.com/product/32307

    Attached is an screen capture of the Saleae Logic Analyzer being used to determine the response time of the Emic 2 TTS.

    Edit: Strike that. The image won't attach.
  • arnold113arnold113 Posts: 43
    edited 2015-06-24 15:43
    Chris, did you by chance try the BS2p40 for its response time?
    Thanks, Arnold
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2015-06-24 15:48
    I did not. The BS2p40 is faster than the BS2, however the timing for the SERIN/SEROUT are different so additional modifications would have to be made. I can do this tomorrow. I was also planning on adding some formatters and testing the minimum response time with those, as it should be slightly less since the SERIN command should take longer to set up.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2015-06-26 12:00
    It turns out there is some weird glitch I am experiencing on the Propeller side which may have caused my results to be off. I am looking more into this. I discovered it while modifying the program for the BS2p24/40. I will follow up once I know why my variable delay isn't functioning as expected.
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2015-06-27 20:15
    That's a nice testing method Chris. I'm interested in how it turns out.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2015-06-28 06:47
    Tracy,

    Thanks. Once the Propeller gets the command it is supposed to wait a set delay and then transmit the response. Each time it gets an ACK it reduces the delay. However, while the delay seems to be functioning properly, my Logic Analyzer is saying the delay each time is all over the place. I can only guess it is related to the FullDuplexSerial4Port object in some way, like FIFO buffers or something. I am thinking of switching to another method since I am only using 9600 bps.
Sign In or Register to comment.