Shop OBEX P1 Docs P2 Docs Learn Events
Connecting to C++ — Parallax Forums

Connecting to C++

ArchiverArchiver Posts: 46,084
edited 2002-05-01 20:43 in General Discussion
Can Anyone please please please please help me with this....I am
really in a deep trouble with thisthing.!!![noparse]:([/noparse]

I got my Basic Stamp to send me a character whenever a light is
blocked on a led...but I am not able to receive this character in my
C++ program....I am on a UNIX system. What I need to do is, i need
to find the time difference between two LED emissions...upon which,
I have made the Basic stamp to send a character to the serial port
everytime the LED emits a voltage. Can anyone help me out on how I
can change the input device on the C++ program from the keyboard to
the serial port ?

Thanks
PEACE!!
MShrestha

Comments

  • ArchiverArchiver Posts: 46,084
    edited 2002-05-01 13:38
    What you want is in sys/termios.h

    You need to know the device name of your serial port
    (/dev/tty? Maybe? Some systems uses /dev/cua?). Use open() to open it.
    You may need flags like O_NONBLOCK (keeps it from waiting
    For carrier detect and O_NOCTTY so that the port can't become a control
    terminal).

    You can use tcgetattr() to get the current settings of the port. It is
    best to modify the existing settings instead of building a termios
    structure from scratch. Read the docs on termios - you'll want to set up
    a bunch of parameters there. You can use cfsetispeed() and cfsetospeed()
    to set the baud rates. I theory they don't have to match, but in
    practice...

    Use tcsetattr to set device modes. I've heard you may want to use fcntl
    to make sure O_NONBLOCK off again, but I've not had any problems.


    Then you can use read/write or you could use fdopen or something similar
    to wrap the file handle into a FILE * (or use an iostream constructor
    that takes a file handle).

    In the termios flags, you'll probably want all flags in c_iflag and
    c_oflag =0. The c_cflag strucutre requrires you to "merge" your bit size
    in:

    tios.c_cflag &= ~CSIZE;
    tios.c_cflag |= CS8;

    You probably also want to set CREAD and HUPCL -- read the man page.

    c_lflag has ICANON (line processing - you want this off), ECHO (off) and
    ISIG (off).

    Other functions:
    tcflush (you want to call this before close()).
    tcdrain
    tcsendbreak
    tcflow

    Here are some references. Keep in mind that this is one place where
    systems vary quite a bit:

    http://publibn.boulder.ibm.com/doc_link/en_US/a_doc_lib/files/aixfiles/t
    ermios.h.htm

    http://www.etek.chalmers.se/~e5arta/LINUX/TERMIOS/termios.html

    A search for termios and your OS name would be a good idea.

    Al Williams
    AWC
    * Easy RS-232 Prototyping
    http://www.al-williams.com/awce/rs1.htm


    >
    Original Message
    > From: manish_shrestha [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=FkD2rPvj_l1yEmuy9MWU5r2oB1i_EYYx4H4e7OnvyEj4EmPD17FKSS4mKKWmI0JeCKQyrqPWJrBQtkljbEXD3A]manish_shrestha@y...[/url
    > Sent: Wednesday, May 01, 2002 7:04 AM
    > To: basicstamps@yahoogroups.com
    > Subject: [noparse][[/noparse]basicstamps] Connecting to C++
    >
    >
    > Can Anyone please please please please help me with this....I am
    > really in a deep trouble with thisthing.!!![noparse]:([/noparse]
    >
    > I got my Basic Stamp to send me a character whenever a light is
    > blocked on a led...but I am not able to receive this character in my
    > C++ program....I am on a UNIX system. What I need to do is, i need
    > to find the time difference between two LED emissions...upon which,
    > I have made the Basic stamp to send a character to the serial port
    > everytime the LED emits a voltage. Can anyone help me out on how I
    > can change the input device on the C++ program from the keyboard to
    > the serial port ?
    >
    > Thanks
    > PEACE!!
    > MShrestha
    >
    >
    >
    > To UNSUBSCRIBE, just send mail to:
    > basicstamps-unsubscribe@yahoogroups.com
    > from the same email address that you subscribed. Text in the
    > Subject and Body of the message will be ignored.
    >
    >
    > Your use of Yahoo! Groups is subject to
    > http://docs.yahoo.com/info/terms/
    >
  • ArchiverArchiver Posts: 46,084
    edited 2002-05-01 14:09
    Thanks a lot for the help...but I still have a
    problem...I am not able to figure out which port my
    circuit is connected to...I tried opening all of the
    following ports...and this is what I got:

    PORT: cua0
    MESSAGE: unable to open /dev/cua0 -: Deviec or
    resource busy

    PORT cua1
    MESSAGE: tty_io.c: process 1214(a.out) used obsolete
    /dev/cua1 - update software to use /dev/ttys1

    PORT cua2
    MESSAGE: tty_io.c: process 1204(a.out) used obsolete
    /dev/cua2 - update software to use /dev/ttys2

    PORT TTYS0
    MESSAGE: Unable to open /dev/ttys0 -: Input/Output
    Error

    PORT TTYS1
    MESSAGE: Unable to open /dev/ttys1 -: Input/Output
    Error

    PORT TTYS2
    MESSAGE: Unable to open /dev/ttys2 -: Input/Output
    Error

    So which port is my circuit device connected to??

    Thanks Bye

    Manish
    --- Al Williams <alw@a...> wrote:
    > What you want is in sys/termios.h
    >
    > You need to know the device name of your serial port
    > (/dev/tty? Maybe? Some systems uses /dev/cua?). Use
    > open() to open it.
    > You may need flags like O_NONBLOCK (keeps it from
    > waiting
    > For carrier detect and O_NOCTTY so that the port
    > can't become a control
    > terminal).
    >
    > You can use tcgetattr() to get the current settings
    > of the port. It is
    > best to modify the existing settings instead of
    > building a termios
    > structure from scratch. Read the docs on termios -
    > you'll want to set up
    > a bunch of parameters there. You can use
    > cfsetispeed() and cfsetospeed()
    > to set the baud rates. I theory they don't have to
    > match, but in
    > practice...
    >
    > Use tcsetattr to set device modes. I've heard you
    > may want to use fcntl
    > to make sure O_NONBLOCK off again, but I've not had
    > any problems.
    >
    >
    > Then you can use read/write or you could use fdopen
    > or something similar
    > to wrap the file handle into a FILE * (or use an
    > iostream constructor
    > that takes a file handle).
    >
    > In the termios flags, you'll probably want all flags
    > in c_iflag and
    > c_oflag =0. The c_cflag strucutre requrires you to
    > "merge" your bit size
    > in:
    >
    > tios.c_cflag &= ~CSIZE;
    > tios.c_cflag |= CS8;
    >
    > You probably also want to set CREAD and HUPCL --
    > read the man page.
    >
    > c_lflag has ICANON (line processing - you want this
    > off), ECHO (off) and
    > ISIG (off).
    >
    > Other functions:
    > tcflush (you want to call this before close()).
    > tcdrain
    > tcsendbreak
    > tcflow
    >
    > Here are some references. Keep in mind that this is
    > one place where
    > systems vary quite a bit:
    >
    >
    http://publibn.boulder.ibm.com/doc_link/en_US/a_doc_lib/files/aixfiles/t
    > ermios.h.htm
    >
    >
    http://www.etek.chalmers.se/~e5arta/LINUX/TERMIOS/termios.html
    >
    > A search for termios and your OS name would be a
    > good idea.
    >
    > Al Williams
    > AWC
    > * Easy RS-232 Prototyping
    > http://www.al-williams.com/awce/rs1.htm
    >
    >
    > >
    Original Message
    > > From: manish_shrestha
    > [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=nsxO0wR-_lKaFy6ahM25Dj_sG4vQeGb9pE1DNgCljAetB9OUinBWm57c268ECbzDXCxZLPhXqQNBLPqb93ec]manish_shrestha@y...[/url
    > > Sent: Wednesday, May 01, 2002 7:04 AM
    > > To: basicstamps@yahoogroups.com
    > > Subject: [noparse][[/noparse]basicstamps] Connecting to C++
    > >
    > >
    > > Can Anyone please please please please help me
    > with this....I am
    > > really in a deep trouble with thisthing.!!![noparse]:([/noparse]
    > >
    > > I got my Basic Stamp to send me a character
    > whenever a light is
    > > blocked on a led...but I am not able to receive
    > this character in my
    > > C++ program....I am on a UNIX system. What I need
    > to do is, i need
    > > to find the time difference between two LED
    > emissions...upon which,
    > > I have made the Basic stamp to send a character to
    > the serial port
    > > everytime the LED emits a voltage. Can anyone help
    > me out on how I
    > > can change the input device on the C++ program
    > from the keyboard to
    > > the serial port ?
    > >
    > > Thanks
    > > PEACE!!
    > > MShrestha
    > >
    > >
    > >
    > > To UNSUBSCRIBE, just send mail to:
    > > basicstamps-unsubscribe@yahoogroups.com
    > > from the same email address that you subscribed.
    > Text in the
    > > Subject and Body of the message will be ignored.
    > >
    > >
    > > Your use of Yahoo! Groups is subject to
    > > http://docs.yahoo.com/info/terms/
    > >
    >
    >
    > To UNSUBSCRIBE, just send mail to:
    > basicstamps-unsubscribe@yahoogroups.com
    > from the same email address that you subscribed.
    > Text in the Subject and Body of the message will be
    > ignored.
    >
    >
    > Your use of Yahoo! Groups is subject to
    > http://docs.yahoo.com/info/terms/
    >
    >


    __________________________________________________
    Do You Yahoo!?
    Yahoo! Health - your guide to health and wellness
    http://health.yahoo.com
  • ArchiverArchiver Posts: 46,084
    edited 2002-05-01 14:50
    I can't tell where your device is connected. However, you should disable
    login from holding that port open. This is handled by getty and there is
    usually a /etc/conf.getty (or /etc/conf.getty.line). You need to remove
    the port you want to use from that file or else you won't be able to
    open it.

    I'd hook a modem or other serial device to the port and try using a
    termial program to talk to it. Then you'd know which port you were on
    and that it was free for use.

    Al Williams
    AWC
  • ArchiverArchiver Posts: 46,084
    edited 2002-05-01 20:43
    Hi, I was able to open the port and close the serial
    port successfully...but wasnt able to read from the
    serial port...how do I make the computer wait for the
    serial port to send a character before the program
    continues? Just like the cin>> we use for input
    through the keyboard.

    Thanks

    Manish
    --- Al Williams <alw@a...> wrote:
    > I can't tell where your device is connected.
    > However, you should disable
    > login from holding that port open. This is handled
    > by getty and there is
    > usually a /etc/conf.getty (or /etc/conf.getty.line).
    > You need to remove
    > the port you want to use from that file or else you
    > won't be able to
    > open it.
    >
    > I'd hook a modem or other serial device to the port
    > and try using a
    > termial program to talk to it. Then you'd know which
    > port you were on
    > and that it was free for use.
    >
    > Al Williams
    > AWC
    >
    >
    > To UNSUBSCRIBE, just send mail to:
    > basicstamps-unsubscribe@yahoogroups.com
    > from the same email address that you subscribed.
    > Text in the Subject and Body of the message will be
    > ignored.
    >
    >
    > Your use of Yahoo! Groups is subject to
    > http://docs.yahoo.com/info/terms/
    >
    >


    __________________________________________________
    Do You Yahoo!?
    Yahoo! Health - your guide to health and wellness
    http://health.yahoo.com
Sign In or Register to comment.