Serial port programming for linux

Serial port programming for linux

Post by Wiseg » Sun, 17 Oct 2004 07:00:30




Quote:> Hello everyone:
> I am working on a school project where I need to read some data from a
> Basic Stamp micrprocessor via the serial port. I have the microprocessor
> connected to my notebook from the serial (microprocessor) to the USB in
> (notebook side which does not have a 9 pin serial port) via a serial to
> usb adaptor (is this an issue?). I wrote a piece of software that sends

Yes it is an issue.  You must have a driver for the USB to serial interface.  

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= East/West-Coast Server Farms - Total Privacy via Encryption =---

 
 
 

Serial port programming for linux

Post by Floyd L. Davids » Sun, 17 Oct 2004 06:03:55



>Hello everyone:
>I am working on a school project where I need to read some data
>from a Basic Stamp micrprocessor via the serial port. I have the

Hmmm... somebody else was fooling with one of those a few months
ago.  Hmmm... last July, in alt.os.linux.slackware.  You might
want to use google to read the discussion.  Do a search on
"Basic Stamp 2pe" and you'll find it.

Is this by any chance an "Engineering Design II" class?

Hmmmm... that was a person signing as "sonoman", but using the

same person, or just sitting in the same seat?

Quote:>microprocessor connected to my notebook from the serial
>(microprocessor) to the USB in (notebook side which does not
>have a 9 pin serial port) via a serial to usb adaptor (is this
>an issue?).

The serial usb adaptor is no big deal, or it wouldn't be for
someone who has everything else under control.  It does throw
one more mysterious black box into the equation that you have
to learn about.

Quote:>I wrote a piece of software that sends some data to
>the serial port from the Basic Stamp microprocessor, this part I
>have tested and I know it works with test data. Now I need to
>bring it in to the notebook via the serial (usb) port.

>I need some pointers on where to get started. I have already
>found some info using c and c++ but it is a bit over my
>head. Does anyone has or knows of a simple tutorial that can
>take me through the basics of serial programming so I can begin
>to understand the more advanced stuff? Do any of you have any
>other ideas on how to bring the data into the notebook? ANY help
>in this matter will be greatly appreciated.

There are two Linux HOWTO documents on serial ports.  One just
on configuring them for users, one is on programming.

   /usr/doc/Linux-HOWTOs/Serial-HOWTO
   /usr/doc/Linux-HOWTOs/Serial-Programming-HOWTO

Unfortunately the Programming HOWTO is filled with minor errors
and major bugs in the code.  It is good to read to get an idea,
so I don't mind suggesting that you read it at this point.  But
don't try to use the code.

Here is a technically astute guide that you'll want to keep
handy.  Much of this will initially be way over your head, but
as questions come up it will be a great reference.

 "The Serial Programming Guide for POSIX Operating System"
  http://www.easysw.com/~mike/serial/serial.html

The only documentation I know of for USB to RS-232 Serial devices
is in the Linux kernel source code distribution,

  .../Documentation/usb/usb-serial.txt

For some interesting source code for talking to a serial port,
my web page has 5 different example "terminal programs".  These
are similar to the code in the HOWTO, so the discussion there
can be used to understand what they do.

 http://web.newsguy.com/floyd_davidson/code/terminal/index.html

Sounds like a fun project, with a *lot* to learn in the process.

--
FloydL. Davidson           <http://web.newsguy.com/floyd_davidson>


 
 
 

Serial port programming for linux

Post by Sonoma » Sun, 17 Oct 2004 10:46:50




>>Hello everyone:
>>I am working on a school project where I need to read some data

>>from a Basic Stamp micrprocessor via the serial port. I have the

> Hmmm... somebody else was fooling with one of those a few months
> ago.  Hmmm... last July, in alt.os.linux.slackware.  You might
> want to use google to read the discussion.  Do a search on
> "Basic Stamp 2pe" and you'll find it.

> Is this by any chance an "Engineering Design II" class?

> Hmmmm... that was a person signing as "sonoman", but using the

> same person, or just sitting in the same seat?

>>microprocessor connected to my notebook from the serial
>>(microprocessor) to the USB in (notebook side which does not
>>have a 9 pin serial port) via a serial to usb adaptor (is this
>>an issue?).

> The serial usb adaptor is no big deal, or it wouldn't be for
> someone who has everything else under control.  It does throw
> one more mysterious black box into the equation that you have
> to learn about.

>>I wrote a piece of software that sends some data to
>>the serial port from the Basic Stamp microprocessor, this part I
>>have tested and I know it works with test data. Now I need to
>>bring it in to the notebook via the serial (usb) port.

>>I need some pointers on where to get started. I have already
>>found some info using c and c++ but it is a bit over my
>>head. Does anyone has or knows of a simple tutorial that can
>>take me through the basics of serial programming so I can begin
>>to understand the more advanced stuff? Do any of you have any
>>other ideas on how to bring the data into the notebook? ANY help
>>in this matter will be greatly appreciated.

> There are two Linux HOWTO documents on serial ports.  One just
> on configuring them for users, one is on programming.

>    /usr/doc/Linux-HOWTOs/Serial-HOWTO
>    /usr/doc/Linux-HOWTOs/Serial-Programming-HOWTO

> Unfortunately the Programming HOWTO is filled with minor errors
> and major bugs in the code.  It is good to read to get an idea,
> so I don't mind suggesting that you read it at this point.  But
> don't try to use the code.

> Here is a technically astute guide that you'll want to keep
> handy.  Much of this will initially be way over your head, but
> as questions come up it will be a great reference.

>  "The Serial Programming Guide for POSIX Operating System"
>   http://www.easysw.com/~mike/serial/serial.html

> The only documentation I know of for USB to RS-232 Serial devices
> is in the Linux kernel source code distribution,

>   .../Documentation/usb/usb-serial.txt

> For some interesting source code for talking to a serial port,
> my web page has 5 different example "terminal programs".  These
> are similar to the code in the HOWTO, so the discussion there
> can be used to understand what they do.

>  http://web.newsguy.com/floyd_davidson/code/terminal/index.html

> Sounds like a fun project, with a *lot* to learn in the process.

Thank you very much for the info. You have very good memory, I am the
same person but I think it was a different computer on the first post.
 
 
 

Serial port programming for linux

Post by Floyd L. Davids » Sun, 17 Oct 2004 19:03:49





>>>Hello everyone:
>>>I am working on a school project where I need to read some data

>>>from a Basic Stamp micrprocessor via the serial port. I have the
>> Hmmm... somebody else was fooling with one of those a few
>> months
>> ago.  Hmmm... last July, in alt.os.linux.slackware.  You might
>> want to use google to read the discussion.  Do a search on
>> "Basic Stamp 2pe" and you'll find it.
>> Is this by any chance an "Engineering Design II" class?
>> Hmmmm... that was a person signing as "sonoman", but using the

>> same person, or just sitting in the same seat?

...

Quote:>Thank you very much for the info. You have very good memory, I
>am the same person but I think it was a different computer on
>the first post.

Hee hee, actually I have a poor memory, so I cheat.  I
substitute Google's really good memory (or in this case I just
did a grep on my own archive of posted articles and found "Basic
Stamp", which was the only part of that that I actually did
remember).  It was unique enough to trigger my interest then,
and now.

You seem to be using a different laptop, and a different Linux
distribution too.  The big thing is not having a serial port and
using USB with a USB-Serial converter.

Did you get the previous configuration to work?  If so virtually
everything you did will tranfer to the new hardware.

It happens that I'd never used a USB-RS232 converter device
until just a few weeks ago.  I can't say that using one of them
is much experience, but at least now I've got some idea what
they do or don't do.  The one I'm using is a FTDI device
(FU8U232 if I remember right) that comes with a Crystalfonts
CFA-633, similar to what is shown in this URL.

  http://www.crystalfontz.com/products/633usb/index.html

Except despite that being the page for the USB model, the
pictures show a serial model, without the FTDI device mounted.
If you don't yet have a USB to serial converter, Crystalfontz
sells two of them.  I'm not sure what the different is offhand,
but if you poke around on their web page you can find it.  If
you don't, post again and I'll get the details and the exact
URL.

The FTDI web page with data sheets and other info is

  http://www.ftdichip.com/FTProduct.htm

--
FloydL. Davidson           <http://web.newsguy.com/floyd_davidson>

 
 
 

Serial port programming for linux

Post by Sonoma » Sun, 17 Oct 2004 22:43:56




>>Hello everyone:
>>I am working on a school project where I need to read some data from a
>>Basic Stamp micrprocessor via the serial port. I have the microprocessor
>>connected to my notebook from the serial (microprocessor) to the USB in
>>(notebook side which does not have a 9 pin serial port) via a serial to
>>usb adaptor (is this an issue?). I wrote a piece of software that sends

> Yes it is an issue.  You must have a driver for the USB to serial interface.  

> ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
> http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
> ---= East/West-Coast Server Farms - Total Privacy via Encryption =---

Correct, I had to recompiler the kernel with the new modules and drivers
added. But since the wine and basic stamp setup is not working I cannot
test it.
 
 
 

Serial port programming for linux

Post by Aaron Soloche » Sun, 17 Oct 2004 23:19:18



> Hello everyone:
> I am working on a school project where I need to read some data from a
> Basic Stamp micrprocessor via the serial port. I have the microprocessor
> connected to my notebook from the serial (microprocessor) to the USB in
> (notebook side which does not have a 9 pin serial port) via a serial to
> usb adaptor (is this an issue?). I wrote a piece of software that sends
> some data to the serial port from the Basic Stamp microprocessor, this
> part I have tested and I know it works with test data. Now I need to bring
> it in to the notebook via the serial (usb) port.

> I need some pointers on where to get started. I have already found some
> info using c and c++ but it is a bit over my head. Does anyone has or
> knows of a simple tutorial that can take me through the basics of serial
> programming so I can begin to understand the more advanced stuff? Do any
> of you have any other ideas on how to bring the data into the notebook?
> ANY help in this matter will be greatly appreciated.

> Thanks in advance,

> MR

> I am using Fedora Core 3 Test 2

Here are a few functions I use for serial setup.  This is used for talking
to a PIC, so should be applicable. I'm no expert in this, so if people see
errors or bad code, please let me know.

/* open and configure the port */
int openport(char *portname){
  struct termios oldtio, newtio;
  char *error = 0;
  int fd;

  /* open read/write, non-controlling, non-blocking */
  fd = open(portname, O_RDWR | O_NOCTTY | O_NONBLOCK);
  if (fd == -1){
    fprintf(stderr, "Unable to open %s: ", portname);
    perror(error);
    exit(-1);
  }

  tcgetattr(fd, &oldtio);          //save current port settings
  bzero(&newtio, sizeof(newtio));  //clear struct for new settings

  newtio.c_cflag = B115200 | CS8 | CLOCAL | CREAD;
  newtio.c_iflag = IGNPAR | ICRNL;
  newtio.c_oflag = 0;
  newtio.c_lflag = 0;

  newtio.c_cc[VINTR]    = 0;     /* Ctrl-c */
  newtio.c_cc[VQUIT]    = 0;     /* Ctrl-\ */
  newtio.c_cc[VERASE]   = 0;     /* del */

  newtio.c_cc[VEOF]     = 4;     /* Ctrl-d */
  newtio.c_cc[VTIME]    = 0;     /* inter-character timer unused */
  newtio.c_cc[VMIN]     = 1;     /* blocking read until 1 character arrives */
  newtio.c_cc[VSWTC]    = 0;     /* '\0' */
  newtio.c_cc[VSTART]   = 0;     /* Ctrl-q */
  newtio.c_cc[VSTOP]    = 0;     /* Ctrl-s */
  newtio.c_cc[VSUSP]    = 0;     /* Ctrl-z */
  newtio.c_cc[VEOL]     = 0;     /* '\0' */
  newtio.c_cc[VREPRINT] = 0;     /* Ctrl-r */
  newtio.c_cc[VDISCARD] = 0;     /* Ctrl-u */
  newtio.c_cc[VWERASE]  = 0;     /* Ctrl-w */
  newtio.c_cc[VLNEXT]   = 0;     /* Ctrl-v */
  newtio.c_cc[VEOL2]    = 0;     /* '\0' */

  /* flush the port, and apply the new settings now */
  tcflush(fd, TCIFLUSH);
  tcsetattr(fd, TCSANOW, &newtio);

  return (fd);

Quote:}

Then you can write to it with write(), but my wrapper function for that is
too specific to be interesting to you.  

Now here is my reading function.  It takes a timeval for the timeout which
is previously declared and initialized like this:

struct timeval tv;

tv.tv_sec = 0;
tv.tv_usec = 600000;

Also note that my code was for reading multiple ports.  If fd is -1, it
reads all the ports I have setup.  I'm leaving this in because you asked
for more advanced stuff, but be aware of the multiple port thing.  Also
note that my protocol for communicating with the pic is comprised of 1
character commands.  That is, I'm reading 1 character at a time because
that's all I care about, not because I have to.

/* read a character off the port */
int readcommand(int fd, struct timeval* tv){
  char inbuf[2];
  int ret, max_fd;
  fd_set set;
  inbuf[1] = 0;

  FD_ZERO(&set);

  // read from both descriptors
  if(fd == -1){
    FD_SET(pic_fd, &set);
    FD_SET(card_fd, &set);

    if(card_fd > pic_fd)
      max_fd = card_fd;
    else
      max_fd = pic_fd;
  }

  // read from a specific descriptor
  else{
    FD_SET(fd, &set);
    max_fd = fd;
  }

  // blocking select waits for input
  if(!select(max_fd+1, &set, 0, 0, tv)){
    return 0; //timed out
  }

  if(errno == EINTR) //select returned due to signal
    return -1;

// read from the PIC
  if(FD_ISSET(pic_fd, &set)){
    if(read(pic_fd, inbuf, 1) < 0){
      perror("read() failed in readcommand() for the pic");
    }
   return inbuf[0];
  }
 // read from the card reader
  if(FD_ISSET(card_fd, &set)){
    if(read(card_fd, inbuf, 1) < 0){
      perror("read() failed in readcommand() for the card reader");
    }
   return inbuf[0];
  }

  return -1;

- Show quoted text -

Quote:}

 
 
 

Serial port programming for linux

Post by David N. Welt » Tue, 19 Oct 2004 17:15:08



> I need some pointers on where to get started. I have already found
> some info using c and c++ but it is a bit over my head. Does anyone
> has or knows of a simple tutorial that can take me through the basics
> of serial programming so I can begin to understand the more advanced
> stuff? Do any of you have any other ideas on how to bring the data
> into the notebook? ANY help in this matter will be greatly appreciated.

Tcl lets you play with serial ports, with the benefit over C being
that it's a scripting..err... 'dynamic' language and so makes
experimentation easier.  I don't have a tutorial for it,
unfortunately.

--
David N. Welton
Personal:                   http://www.dedasys.com/davidw/
Apache Tcl:                 http://tcl.apache.org/
Free Software:              http://www.dedasys.com/freesoftware/
Linux Incompatibility List: http://www.leenooks.com/

 
 
 

1. Parallel Port programming question, was "Serial Port Programming"

All right, it was a unanaimous response to use the parallel port instead of
the serial port to twiddle pins on a IO port.  Anyone have a quick snipit
of code or direction to go.  I have the IOport programming howto which shows
how to set up port to be talked to, but does not explain how to exactly
talk to the port itself.  Any help would be appreciated.
--
=========================================================================
| Andrew F. Nelson      Computer Science                                |

| URL:                  http://www.cs.umn.edu/~nelson                   |
| Title:                WWW Administrator / Systems Staff               |
|                       Computer Science and Institute of Technology    |
| "Murphy's Law isn't just a saying, it is a way of life!"              |
=========================================================================

2. Where is top? (The CPU process monitoring utility)

3. How to set up serial port when programming serial communication

4. DEC personal station 5000

5. V880z with UltraSPARC-IV ?

6. HOW TO program the serial port on linux

7. Frozen mice

8. Linux serial port programming

9. Example Programs using the Parallel/Serial Port (Linux)

10. Linux Serial Port Programming problem - ioctl -

11. Programming the serial port under Linux