Shop OBEX P1 Docs P2 Docs Learn Events
Serial port usage, how? — Parallax Forums

Serial port usage, how?

ypapelisypapelis Posts: 99
edited 2012-10-16 08:45 in Propeller 1
I didn't realize something that simple can be so frustrating. I have been trying for hours to figure a way to simply use a serial port. Not one associated with pins 30/31, but with other pins, or possibly multiple sets of other pins. I want to be able to do high speed (115200 or more) baud, full duplex, and possible do it on more than one port using the same driver (like the spin drivers). Rather embarrasing, but I can't figure out how!

There are two posts discussion the serial port and FullDuplexSerial, but both refer to using it for stdio, and focus on non-blocking reads (which I also need to do). There is a post that shows a simple (and probably working) serial port driver. In the demos, there is a full duplex serial - heater version, but it won't compile as is, I believe it was constructed for an earlier version of proggcc. I have seen posts on the FullDuplexSerial 'stdio' driver, but can find nothing in the headers to help use it. There has to be a normal, built-in way of including a header file and calling serialOpen(txPin, rxPin, baud), or something like that. I could have probably written my own in the time it has taken, but I'd rather not at this point. And I am sure, there is a trivial way to do it, but for whatever reason, I haven't found it yet - any pointers would be greatly appreciated!

Comments

  • ersmithersmith Posts: 6,054
    edited 2012-09-21 06:12
    There's some documentation at http://propgcc.googlecode.com/hg/doc/Library.html#drivers. Put
    #include <driver.h>
    
    extern _Driver _SimpleSerialDriver;
     extern _Driver _FullDuplexSerialDriver;
      _Driver *_driverlist[] = {   &_FullDuplexSerialDriver,   NULL };
    
    in your code, and then you can do
    FILE *myserial = fopen("FDS:115200,9,10", "r+");
    
    to open a new FILE handle with the full duplex serial driver, 115200 baud, using pin 9 for receive and 10 for transmit.
  • RaymanRayman Posts: 14,670
    edited 2012-09-21 06:50
    Out of curiosity... How does that approach compare to using Spin2Cpp on fullduplexserial.spin?
  • ersmithersmith Posts: 6,054
    edited 2012-09-21 08:36
    Rayman wrote: »
    Out of curiosity... How does that approach compare to using Spin2Cpp on fullduplexserial.spin?

    The underlying code (the FullDuplexSerial code running on a COG) is the same, but the interface is completely different -- spin2cpp will produce an object with methods like "rx", "tx", "str", etc., whereas the FullDuplexSerialDriver interfaces to the C standard libraries so you can use the normal C functions like getchar, putchar, and printf.

    Eric
  • EnriqueEnrique Posts: 90
    edited 2012-09-25 00:13
    Hi,

    I am trying to use full duplex serial in a C program but I am having a tough time trying to figure out how to do something that was done with the FullDuplexSerial object in Spin.

    In Spin we had the rx method which when called would either give us a value of -1 if the buffer was empty or the first byte available in the buffer. This would allow the main program to do other things while data was being sent from another machine. I find however that the getchar function doesn't return unless it has received something from the PC.

    Is there something similar to rx in the C implementation of FullDuplexSerial?


    Thanks,
    Enrique
  • ypapelisypapelis Posts: 99
    edited 2012-09-25 12:19
    Enrique wrote: »
    Hi,

    I am trying to use full duplex serial in a C program but I am having a tough time trying to figure out how to do something that was done with the FullDuplexSerial object in Spin.

    In Spin we had the rx method which when called would either give us a value of -1 if the buffer was empty or the first byte available in the buffer. This would allow the main program to do other things while data was being sent from another machine. I find however that the getchar function doesn't return unless it has received something from the PC.

    Is there something similar to rx in the C implementation of FullDuplexSerial?

    Thanks,
    Enrique

    Hi Enrique,

    There was a thread earlier that showed how to do this: http://forums.parallax.com/showthread.php?141659 I have confirmed that this approach works.

    And as mentioned in a post above, Spin2Cpp can be used to generate C code from the good-old FullDuplexSerial. Somehow I have not mustered the courage to try this; it just seems so wrong (too complicated for something that obvious). Maybe someone can post the final output files so we can use them directly.

    Keep in mind, the approach of including FullDuplexSerial in the stdio driver table and then invoking it through fopen() wastes one COG, since it seems to reserve a cog for stdin, and another cog for every instance of fopen. This is a serious problem is you are using a lot of serial ports, as each one of them will use a different COG. Obex contains a FullDupliexSerial_r004 object that allows up to 4 serial ports at full speed, using only a single COG. It would be nice to have similar functionality accessible in C, without having to resort to stdio interfaces.
  • jazzedjazzed Posts: 11,803
    edited 2012-09-25 12:52
    Attached is a propeller-gcc version of my original full duplex serial C port from 2008.
  • ersmithersmith Posts: 6,054
    edited 2012-09-25 13:49
    ypapelis wrote: »
    Keep in mind, the approach of including FullDuplexSerial in the stdio driver table and then invoking it through fopen() wastes one COG, since it seems to reserve a cog for stdin, and another cog for every instance of fopen.
    It depends on how you use it. If you leave SimpleSerial as the first entry in the driver table then that will be used for stdin and stdout, and the FullDuplexSerial will be used only when you explicitly request it (by opening a file named "FDS:").
  • ypapelisypapelis Posts: 99
    edited 2012-09-25 15:12
    ersmith wrote: »
    It depends on how you use it. If you leave SimpleSerial as the first entry in the driver table then that will be used for stdin and stdout, and the FullDuplexSerial will be used only when you explicitly request it (by opening a file named "FDS:").

    Ok, that seems to work. So what exactly is the significance of the _driverlist table, is it a table of available I/O drivers, all of which are available for use through fopen(), and the convention is that the first one will be used for stdin/stdout/stderr?

    By the way, is this information listed anywhere? If not, I am sure a wiki page or other full summary of it would save a lot of people a lot of time.
  • jazzedjazzed Posts: 11,803
    edited 2012-09-25 16:36
    ypapelis wrote: »
    Ok, that seems to work. So what exactly is the significance of the _driverlist table, is it a table of available I/O drivers, all of which are available for use through fopen(), and the convention is that the first one will be used for stdin/stdout/stderr?

    By the way, is this information listed anywhere? If not, I am sure a wiki page or other full summary of it would save a lot of people a lot of time.

    Look here https://sites.google.com/site/propellergcc/documentation/libraries
    here http://code.google.com/p/propgcc/ here http://propgcc.googlecode.com/hg/doc/Library.html
    and here http://code.google.com/p/propgcc/source/browse/#hg%2Fdoc
  • ypapelisypapelis Posts: 99
    edited 2012-09-25 17:58
    Jackpot !!!
  • EnriqueEnrique Posts: 90
    edited 2012-09-26 12:24
    I’ll try the suggestions when I’m back from my vacation.

    Enrique
  • EnriqueEnrique Posts: 90
    edited 2012-10-16 08:45
    Jazzed,

    Have been using your driver for a while, it works beautiful.

    Thanks.
Sign In or Register to comment.