Shop OBEX P1 Docs P2 Docs Learn Events
Simple_Serial not so simple for me — Parallax Forums

Simple_Serial not so simple for me

redrockerredrocker Posts: 10
edited 2009-05-20 22:29 in Propeller 1
Hello, I am trying to receive transmission from a digital compass to no avail.

The compass outputs an rs232 signal. I currently have it configured to to output the compass heading value at 10hz. The format goes like xxx.x<cr><lf>. I am running the signal through the RS232 port on the PPDB and into some I/O pins of the propeller. My first thought is that it may be a wiring mistake so I will take it to school and look at the output from the max232 driver on an osciliscope to be sure.

However in the mean time I thought I would test the spin code.

To do this I thought I would write a simple program utilizing Simple_Serial.spin object. My thinking was that I could use a COG and an I/O pin to transmit a a character twice a second. Then I would use a 2nd COG and I/O pin to continually receive output and record the value in a global variable "xx". To check my work I tried to use a third COG to transmit the value of "xx" to the parrallax serial terminal. But I dont ever see anything on the terminal. Though I have sent debug data to the terminal to test it out. This is my first spin program so try not to laugh:

CON
_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000
baud=9600 'speed of the I/O serial
tbaud=19200 'Baud of the Parrallax Serial Terminal
TR=31 'Recieving pin for Terminal
TT=30 'Transmit pin to Terminal
SR=21 'Recieving I/O pin
ST=20 'Trans I/O pin
mode=1 'Not needed for Simple_Serial

Var
long xx
long Stack[noparse][[/noparse]9]
long Stack2[noparse][[/noparse]9] 'not sure if I need 2 stack variables
OBJ
SERT: "Simple_Serial"
PUB Serialtest
cognew((StartTrans),@Stack ) 'Cog1
cognew((RecTrans), @Stack2 ) 'Cog2
SERT.init(TR,TT,tbaud) 'This should be the Cog0 sending helloA to the termial
repeat
SERT.str(string("hello"))
SERT.str(xx)
waitcnt(clkfreq/2+cnt)

PUB StartTrans
SERT.init(sR,sT,baud)
repeat
SERT.tx(65)
waitcnt(clkfreq/2+cnt)

PUB RecTrans
SERT.init(sR,ST,baud)
repeat
xx:=SERT.rx

Any help on this would be greatly appreciated. I have been banging my head against the wall days.
Thanks

Adam

Comments

  • morris4019morris4019 Posts: 145
    edited 2009-05-20 17:53
    Hi, i just ordered my first PROP and waiting till tomorrow to actually recieve it. I have yet to code anything with it but I am familiar with alot of coding. I took a look at your basic code (which is what i do when learning a new language), and one thing popped in to my head. Most of the languages are CASE Sensative, but i'm not sure if spin is. It looks like your "Recieveing PIN" = CON SR right. Well in your StartTrans function your are using sR instead of SR and sT instead of ST, also once in your RecTrans function.

    Don't know if this helps but I know most of the languages are CASE sensative, just thought i would shoot that out there.

    -Mike

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ======

    ······ I'll try everything once [noparse]:)[/noparse]
  • T ChapT Chap Posts: 4,223
    edited 2009-05-20 18:13
    Redrocker. If you post your code using [noparse][[/noparse] code ] and [noparse][[/noparse] /code ] before and after your code it will retain your indention. Remember that when using brackets in your code, some brakceted numbers are reserved in the html code for font size, so putting a space between the brackets and the numbers/text will allow users to see what you really coded, else the certain bracketed numbers will not be shown and will affect the font size in your post. The case should not be an issue. Any drawing that you can post will help out greatly, since your problem may just as likely be related to wiring as it could be to code.

    Look at the 4port full duplex serial object, run 4 serial devices with one object, all at different baud if needed.

    Post Edited (TChapman) : 5/20/2009 6:32:26 PM GMT
  • TimmooreTimmoore Posts: 1,031
    edited 2009-05-20 18:21
    The use of the sert object is going to be a problem. You are using it in 3 cogs at the same time, some have different pins and baud rates. Given what you are doing I would define 3 serial objects, 1 for the debug port, 1 for tx to compass and 1 for rx for compass - i.e. each cog is using a different object
  • KyeKye Posts: 2,200
    edited 2009-05-20 18:29
    Hey red rocker, tryout the Full Serial Port Driver or Lite version in the obex under protocols. The object contains the diagrams on how your pins should be configured for simple TTL communication.

    Then you can use a max232 to turn the TTL signals into rs232 signals. The objects have a pretty good documentation on how they work.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Nyamekye,
  • AribaAriba Posts: 2,690
    edited 2009-05-20 18:34
    redrocker

    You need at least 2 instances of SimpleSerial for 2 Transmitter and 1 Receiver. Always when you call SERT.init(..) you overwrite the Pin- and Baudrate settings of the SimpleSerial object.
    As a first try: Comment out the 2 cognew(..) lines, then the text "hello" should appear on the terminal.

    Then insert a second SimpleSerial object for the StartTrans and RecTrans cogs with another name, and init this only one time (the init can be in the main methode (Serialtest).

    And finally: The receice methode of SimpleSerial has a bug, you need to add 1 line:
    PUB rx: rxByte | t, b
    {{ Receive a byte; blocks caller until byte received. }}
    
      if rxOkay
        dira[noparse][[/noparse]sin]~                                          ' make rx pin an input
        b~                                                  '<-- ADD THIS
        waitpeq(inverted & |< sin, |< sin, 0)               ' wait for start bit
        t := cnt + bitTime >> 1                             ' sync + 1/2 bit
        repeat 8
          waitcnt(t += bitTime)                             ' wait for middle of bit
          b := ina[noparse][[/noparse]sin] << 7 | b >> 1                       ' sample bit 
        waitcnt(t + bitTime)                                ' allow for stop bit 
    
        return (b ^ inverted) & $FF                         ' adjust for mode and strip off high bits
    
    



    Andy
  • redrockerredrocker Posts: 10
    edited 2009-05-20 20:14
    Thanks for all of the help. Here is a better look at the code if you have further suggestiongs let me know. In the meantime I will look at obex for suggested files. Oh and just to Clarify, I have disconnected the compass all together. Now I am just sending a few characters from PINa to PinB on the Propeller. As far as wiring I just have a straight wire jumping them together.

    Andy,

    Yes when I comment the Cognew lines I do get a hello through the terminal. However I added the suggested changes (and fixed the Simple_Serial.spin object) and I still get nothing with the cognews uncommented, which means something is still being overwritten?

    Anyway here is the cod
    CON
    _clkmode = xtal1 + pll16x
    _xinfreq = 5_000_000
    baud=9600
    tbaud=19200
    TR=31
    TT=30
    SR=21
    ST=20
    mode=1
    
    Var
      long xx
      long  Stack[noparse][[/noparse]9]
      long Stack2[noparse][[/noparse]9]
    OBJ      
      Sert:  "Simple_Serial"
      Sterm: "Simple_Serial"
    PUB Serialtest
      cognew((StartTrans),@Stack )                          'StartTrans with Cog1?
      cognew((RecTrans), @Stack2 )                          'StartTrans with Cog2?
      Sert.init(SR,ST,baud)                                 'Initialise serial communication between I?O Pins
      waticnt(clkfreq +cnt)                                 'wait one second 
      Sterm.init(TR,TT,tbaud)                               'Initialize Terminal Comm.
        repeat
          Sterm.str(string("hello"))
          Sterm.str(xx)
          waitcnt(clkfreq/2+cnt)
    
    PUB StartTrans
      
        repeat
          Sert.tx(65)                                        'should transmit "A" ASCII
          waitcnt(clkfreq/2+cnt)
    
    PUB RecTrans
      
        repeat
          xx:=Sert.rx
    
    
  • StefanL38StefanL38 Posts: 2,292
    edited 2009-05-20 21:01
    Hello Redrocker,

    simple_serial is so simple PROGRAMMED and has so many limitations that it is complicated to USE

    switch over to FullDuplexSerialPlus or Kyes serial object

    This objects runs it its own cog and can do sending and receiving at the same time

    best regards

    Stefan
  • redrockerredrocker Posts: 10
    edited 2009-05-20 21:20
    Thanks Stefan,

    By FullDuplexSerialPlus, do you mean Extended Full Duplex Serial? I am assuming so because I saw that you gave it a 5 star rating. I am not sure where to find the Kyes oject however.

    Adam
  • AribaAriba Posts: 2,690
    edited 2009-05-20 21:42
    redrocker

    I've tried your code now. The stack size was to little, and the parentheses around the methode name in cognew are not allowed (don't know why). This code works on my propeller. Caution: I have changed the pins for the 2. serial port.
    CON
    _clkmode = xtal1 + pll16x
    _xinfreq = 5_000_000
    baud=9600
    tbaud=19200
    TR=31
    TT=30
    SR=1        '<--- Pins changed
    ST=0
    mode=1
    
    Var
      long xx
      long  Stack[noparse][[/noparse]16]
      long Stack2[noparse][[/noparse]16]
    OBJ      
      Sert:  "Simple_Serial"
      Sterm: "Simple_Serial"
    PUB Serialtest
      cognew(StartTrans,@Stack )                            'StartTrans with Cog1?
      cognew(RecTrans,@Stack2 )                             'StartTrans with Cog2?
      Sert.init(SR,ST,baud)                                 'Initialise serial communication between I?O Pins
      Sterm.init(TR,TT,tbaud)                               'Initialize Terminal Comm.
      waitcnt(clkfreq +cnt)                                 'wait one second 
      repeat
         Sterm.str(string("hello"))
         Sterm.tx(xx)
         Sterm.tx(" ")
         waitcnt(clkfreq/2+cnt)
    '     xx := 0                                           'synchronize to receive
    '     repeat until xx>0                                 'instead of waitcnt above
    
    PUB StartTrans
      repeat
         Sert.tx(65)                                        'should transmit "A" ASCII
         waitcnt(clkfreq/2+cnt)
    
    PUB RecTrans
      
      repeat
         xx:=Sert.rx
    
    


    Shown is also a possible synchronization between the rx cog and the output to the terminal (remove the waitcnt before).

    Andy
  • redrockerredrocker Posts: 10
    edited 2009-05-20 21:58
    Andy,

    You are a stud! Working like a champ! Can I ask how you found out the stack was too small? Thanks for the wonderful synchronization addition as well.

    Thanks

    Adam

    Post Edited (redrocker) : 5/20/2009 10:08:26 PM GMT
  • Steph LindsaySteph Lindsay Posts: 767
    edited 2009-05-20 22:29
    Hi Redrocker,

    The FullDuplexSerialPlus object·is bundled in with the Propeller Tool version 1.2.6, in the Examples > PE Kit > 6 - Objects folder. You might find the Spin Tutorial in the Help and the PE Kit Labs book pdf which is linked in the Help Resources page to be useful.

    To determine how much stack space you need, check out the Stack Length.spin object that is included in the Propeller Tool v1.2.6 folder.·There is a Webinar video demonstrating how to use it, called How can you determine how much stack space is needed for Spin code launched into another cog?··posted on this page. under Memory Manangement:

    http://www.parallax.com/tabid/766/Default.aspx·

    Have fun!
    -Stephanie Lindsay
    Editor
    Parallax Inc.
Sign In or Register to comment.