Shop Learn P1 Docs P2 Docs
Serial Object — Parallax Forums

Serial Object

I would like to listen to a device that transmits 7-bit data, even parity, 1 stop bit at 1200 baud.
Is there a serial object that's easily configured to accomplish this task. It would come from a Db9 serial cable thru a FTDI usb converter.
I just want to receive, no need to transmit.


  • evanhevanh Posts: 13,852
    edited 2022-10-26 09:38

    There is hardware in the Prop2 for that. Every pin has an associated smartpin. A smartpin configured to P_ASYNC_RX mode is what you want.

    Parity isn't processed by the hardware, therefore receive the data as 8N1 then strip the parity bit in software. Here's example hardware setup:

    xreg := (clkfreq / 1200 & $ffff_fc00) | 7
    pinstart( rxpin, P_ASYNC_RX, xreg, 0 )

    In the main loop, check for new character arrived with pinr(rxpin) then read the smartpin with rdpin(rxpin)

  • JonnyMacJonnyMac Posts: 8,314

    As Evan points out, the standard serial objects (including mine) treat serial as 8N1. You can use any of these drivers and then test for 7E like this

    pub get_rx() : result
      result := serial.rxcheck()                                    ' is byte available?
      if (result < 0)                                               ' -1 if buffer empty
        return -1
      if (ones(result) & 1)                                         ' check parity
        return -2                                                   '  if odd, return error
      result &= $7F                                                 ' strip parity bit
  • Thanks Guys,

    if (ones(result) & 1)........hmmm interesting
    Nice homework....Haven't seen that ones directive yet. Thanks again.

  • AribaAriba Posts: 2,601

    The problem with the Smartpin Serial RX is that the baudrate divider is only 16 bits wide. So for your 1200 Baud, you need a system clock lower than 78 MHz.

    For such slow baudrates it may be simpler to just receive the serial bits with a Spin loop, like SimpleSerial on P1.


  • Thanks Andy,
    Hopefully next week I can get back to this project.
    I just wish windows would do it's job and read the port, and this wouldn't be neccessary.
    I'm trying to upgrade a windows 98 machine that reads the port just fine. But these newer computers won't.

  • JonnyMacJonnyMac Posts: 8,314
    edited 2022-10-28 21:52

    The ones() instruction appears in Spin2 and in PASM2. Interestingly, I am working on a P1 driver for a project and need that feature in Spin1, so I used this clever method that I found in a programming book.

    pub ones(value) : bc
      repeat while (value)
        value &= value-1

    When I tried to run on the P1 the compiler complained. It turns out that Chip had intended the ones() instruction for the P1, but it didn't make it in (though it is still on the list of reserved words used by the P1 compiler). I had to rename the method bitcount() for the P1.

    Per Andy's comment, you can't use smart pins for low baud rates with reasonable clock speeds on the P2. The attached RX-only driver gets around that using the bit-bang approach. Note that it still treats input as 8 bits and doesn't check parity. You'll have to do that after pulling a byte from the RX buffer.

    Updated with complimentary TX version. Demo runs at 1200 baud (using PST) with 200MHz P2 clock frequency.

  • Thanks Jon,
    I can't work with it if they are using it and they use the machine about everyday.
    When work slows I'll let you know how it went.

  • JonnyMacJonnyMac Posts: 8,314
    edited 2022-10-28 21:53

    I did a clean-up and added a complimentary TX object. See demo archive above. It seems to be working well, but do let me know if you run into any problems.

  • Thanks Jon, It works beautifully.
    The other objects where actually freezing the mouse up on my computer. Crazy..huh!
    I would have to flip the flash switch on the eval board to clear the port when I plugged it in long enough to reprogram the board.

    This question may need another thread.
    Due you know of an object yet that configures the serial Host and serial device boards for the eval board?

    With that I could simulate the machine and do all my work from home.
    I saw one in GET, but, it was for bluetooth

  • JonnyMacJonnyMac Posts: 8,314
    edited 2022-10-29 00:58

    Glad it works.

    Due you know of an object yet that configures the serial Host and serial device boards for the eval board?

    Are you talking about auto-baud? The P2 actually does this during the download process.

    I use XBees in several projects. The XCTU program will send a short query to the XBee -- using a list of possible baud rates -- until the XBee responds; from that point the baud rate of the XBee is known. This seems like an approach you could adopt; program the Propeller for the desired baud rate and the connected app figure out its baud rate.

    With that I could simulate the machine and do all my work from home.

    I have lost count of the number of times I've programmed a simulation into a background cog. If you no the protocol and baud it should be a simple exercise.

  • As for the simulation: Yes I agree, Simple. However, I'm referring to the code that will let my serial device show up in windows as a CommPort.
    Once I attach the board to the Machine at work I can use the programming port on the eval to overcome this. Programming at Home, I would like to keep pins 63 & 64 for the Propeller IDE and use the Serial Device Accessory Board(64006(f) rev b) to speak to my program running on the same computer I'm programming the Propeller with.

  • JonnyMacJonnyMac Posts: 8,314

    I'm not aware of any USB client driver for that board (64006). If you attach a low-cost USB-to-serial adapter, it will handle the USB side connection for you, and you'd be able to use any of the various serial drivers to talk to your PC through any pins you want to use.

  • Sounds like a plan.
    I can definitely do something with what I've got here.
    Thanks again.
    Andy, Evan, Thank you as well.

Sign In or Register to comment.