Shop OBEX P1 Docs P2 Docs Learn Events
Propeller commands similar to Basic Stamp (BS2) functions — Parallax Forums

Propeller commands similar to Basic Stamp (BS2) functions

squaresquare Posts: 6
edited 2010-01-22 15:57 in Propeller 1
I am wondering if anyone knows how to execute commands on Propeller similar to that of Basic Stamp commands. On the OBEX, I downloaded a "BS2_Functions" file to use certain commands; most of them work, but I realized that the serin/serout commands were faulty in some of its uses. In other words, some pieces of the code were not full-proof; I had to touch up on some of them, and some serin/serout PUB routines could not be understood. I am wondering if anyone has some source code on SPIN to access and mimic BS2 commands. Another solution would be if someone out there knew a solution to the serin/serout commands on the "BS2_Functions" file so that I could possibly use them.

I want to send more than 8 bits through a serout (either through decimal, binary, or hexadecimal). I also want to receive more than 8 bits through a serin. If possible, I am wondering if there is a flag to set in order to execute a serin (i.e. not a character, but some sort of flag). Thank you very much to anyone out there who is considering a solution.

-square

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2010-01-21 01:47
    1) You need to describe more thoroughly what you want to do.

    2) The SERIN_CHAR and SEROUT_CHAR routines in the BS2_Functions object are designed to receive and transmit any number of bits. The I/O pin number, Baud, Inverted/Non-inverted mode, and number of bits are all specified as parameters. Note that, if you use anything other than 8 bits, you won't be able to communicate with most PCs and most other devices. If you need to transmit a value with more than 8 bits, you can break it up into two 8 bit values and reassemble it on the receive end.

    3) What do you mean by a "flag"?
  • Dr_AculaDr_Acula Posts: 5,484
    edited 2010-01-21 02:22
    I came from the picaxe world (which also uses serin and serout) and there is a very steep learning curve moving to Spin. One fundamental concept is converting bytes to 'longs' as the propeller works natively with longs (4 bytes in a row). So if you want to talk in bytes there is lots of >> and << code shifting things in and out of longs, and it takes a while to find out these strange combinations of characters actually means. I still constantly get confused between =, == and := (which are all = to each other in Basic). (or == if you are a C programmer *grin*)

    Anyway, I ended up building heater and cluso's CP/M emulation so I could program in Basic on the prop. eg
    A$="Hello World"
    for i=1 to len(a$)
    out(&H78),asc(mid$(a$,i,1))
    next i

    which does a serout of 'Hello World" to a serial port.

    And in doing so I had to learn Spin to write the emulation. And finally at the end of that, I found I didn't need the Basic emulation so much as I had learned Spin!

    So - although it is a steep learning curve, rather than trying to shoehorn BS2 into the Prop, I think it is worth trying to learn some Spin. It isn't that different to BS2, and my personal view is that Spin is a sort of hybrid of Basic and C and Pascal, so by learning Spin you also learn C and Pascal and that is useful too.

    Re flags, do you mean a flag to detect if a byte has arrived on a serial port?

    This is a *Very Big Problem* for the picaxe, as you can't test for a byte arriving (or at least not unless you get some of the very latest chips which cost more than the propeller). All you can do is execute a serin and the whole chip hangs till a byte arrives.

    On the prop, you can read bytes into a buffer in the background then check them when you are ready. This is where the parallel processing really shines. So on the CP/M emulation you can do an INP(&H7D) which returns 32 or 33 depending on whether there is a byte (or more than 1) available and if yes, then you read it in with INP(&H78).

    And then there is Bean's Basic Compiler which ought to provide a crossover for BS2 programmers.

    And the Catalina C compiler for those coming from Arduino.

    The best bit about this forum is there are many people willing to help out with the most simple questions - especially when starting out with Spin. All you need to do is desribe the problem a bit more, and almost invariably, within a few hours, someone has kindly posted some spin code.

    Most times it is then a matter of "see one, do one, teach one".

    So - do you want to send out "Hello World" from a serial port?

    Or, do you want to read "Hello World" from a serial port? And if so, do you know in advance when it will arrive, are you polling waiting for it to arrive, are the packets less or more than n characters (ie bigger than the buffer), and do you know in advance how many bytes in "Hello World"? The answers to those questions affects what sort of polling you do, whether you have timeouts etc.

    Can you describe the whole project a bit more, eg hardware, and I'm sure we can all get it working.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    www.smarthome.viviti.com/propeller

    Post Edited (Dr_Acula) : 1/21/2010 10:55:19 AM GMT
  • squaresquare Posts: 6
    edited 2010-01-21 04:09
    I'm sorry that was not thorough. For starters, I'm in a transition from BASIC STAMP 2 to Propeller; so, I'm new at programming in SPIN. I am in a transition from PBASIC to SPIN; so, for starters, I want to use the serout and serin commands from BASIC Stamp 2. I want to transmit a long from one pin and read the long in another pin either through hex or binary form. For serout, I can use something like "BS2.serout_dec([noparse][[/noparse]Pin1],[noparse][[/noparse]$FF],9600,1,8). Can I use the "serout_dec" to transmit hexadecimal values? Also, what does the 8 bits at the end of the argument mean in detail? Thank you very much. To transmit a long, I'll just add it all up in an array through four transmissions.

    However, for the serin, I notice that the "BS2_Function" does not have "serin_wait" for binary numbers, and I'm not quite sure what two of the arguments mean. The format goes x := serin_wait([noparse][[/noparse]Pin],Value,@mystring,char,BAUD,mode,bits). What does "@mystring" signify? I also undersand that "char" waits for a character, but where does the character have to reside in order to check it/be flagged? Thank you all for your time.

    -square
  • Mike GreenMike Green Posts: 23,101
    edited 2010-01-21 05:14
    Please describe what values you want to send and receive. Just like in PBASIC where the internal storage size for variables is a 16-bit word, in Spin the internal storage size is always a 32-bit long. That says nothing about the range of values you're dealing with and, in this case, wish to transmit and receive.

    SERIN_WAIT is like the WAIT() clause in the BS2 SERIN statement. @mystring is the address of the string to wait for. Spin does have a shortcut to produce an address from a literal string and you can substitute this in place of @mystring. It would be STRING("Hello World")

    SERIN_CHAR causes the cog to wait for any character to be received, then it returns the character. There's no "flag" involved. It's like writing

    SERIN <pin>,<Baud>,[noparse][[/noparse]byteChar]

    where "byteChar" is declared as a byte variable.
  • squaresquare Posts: 6
    edited 2010-01-21 05:57
    Thank you very much. So, let's say I want to send a hexadecimal value of $FFBCABFF, and I wish to use the serout to transmit on one pin (e.g. - 1) and serin_wait command to receive wait on another pin (e.g. - 7). What set of statements would I need to use as an example for me to follow? Any string to wait for as an example would be fine, (e.g. - "hello") but how would I designate/read its address; sorry if this sounds trivial. I'm a noob. Thank you for your time.

    -square
  • Mike GreenMike Green Posts: 23,101
    edited 2010-01-21 06:23
    There's no hex output routine except for DEBUG. I'm not sure why. You could copy the DEBUG_HEX routine, but substitute SEROUT_CHAR for DEBUG_CHAR.

    I made a mistake. Sorry. SERIN_WAIT isn't like the WAIT() clause in the SERIN statement. It does something different.

    Best thing is to read the source code for BS2_Functions.spin and look at the comments associated with each routine. Many of these comments include examples of the use of the particular routine including an explanation. Also look at BS2_Func_Test.spin which shows the use of many of the other routines in BS2_Functions.spin.
  • squaresquare Posts: 6
    edited 2010-01-21 06:28
    Thank you very much. I'll look into that Test source demo tomorrow. If I have problems tomorrow, I'll most likely post a reply again. Thank you for your time.

    -square
  • squaresquare Posts: 6
    edited 2010-01-21 06:36
    Sorry Mike. I looked at a previous link you posted about the BS2_Func_Test.spin file, and I did not see any line of code for "serin/serout". How does the "debug_" work? Does it display in the PST? How does that work? I looked at some of the examples, but I did not know how the "debug_" works.

    -square
  • Mike GreenMike Green Posts: 23,101
    edited 2010-01-21 15:11
    Look at the code for the DEBUG_ methods in the BS2_Functions.spin source file. You'll see that they all call the SERIN_ and SEROUT_ methods, but use the pin numbers passed to the START method and saved for later use. They also use a Baud and mode value set by constant declarations near the beginning of the source file.
  • squaresquare Posts: 6
    edited 2010-01-21 23:58
    Hello Mike. I'm still having problems using serin. I would like to use serin_str. Here is what I have so far.

    repeat
    BS2.serout_str(7,string("Hello World",13),9600,1,8)

    BS2.serin_str(5,string("Hello World"),9600,1,8)

    waitcnt(900_000_000 + cnt)

    The "serout" statement works perfectly, but all of the "serin" statements do not work (i.e. debug_, serin_char, etc.). When the SPIN executes the "serin_str" command, it idles. I am wondering what is wrong with my code. Thank you for your time.

    -square
  • HumanoidoHumanoido Posts: 5,770
    edited 2010-01-22 15:43
    I think the establishment of a source containing all the collected information in going from BASIC Stamp PBASIC to Propeller SPIN would be worthwhile, and make a good reference sticky. I see this type of question appearing on the Forum again and again...

    humanoido
  • Mike GreenMike Green Posts: 23,101
    edited 2010-01-22 15:57
    I may have gotten you off on the wrong foot with my earlier note. You can't use STRING() with BS2.serin_str because the 2nd parameter is the address of the byte array to receive the data, so the string constant "Hello World" will be altered. Since this parameter has to be a 50 byte array (see the comments in BS2_Functions), some other stuff will be overwritten as well. When you do the BS2.serin_str, the program will stop until some characters are received, ended by a return character ($0D) which will be returned in the byte array you supplied (terminated by a zero byte). The same thing happens with DEBUG_ since that calls the SERIN_ routines.
Sign In or Register to comment.