True. I didn't consider that situation. I use this for my own debugging/one-off projects and have never actually passed anything to it other than a 0 or a 1 :) Good suggestion. Eric On Mon, 16 Feb 2004 18:16:05 -0300 Lucas Hartmann wrote: > Eric, > > I've noticed that you initialized fd=0. Note that the swich statement has no > default: clause, so calling the function with an out-of-range number (eg 5), > will skip the open(...)'s and fd will keep as 0. When fd is checked for > error it's compared -1 so it will return success in a wrong situation, just > after calling the control functions on fd=0. > > Isn't a good idea to initialize fd=-1? > > Lucas Hartmann > > -------Mensagem original------- > > De: pic microcontroller discussion list > Data: 02/16/04 17:45:04 > Para: PICLIST@MITVMA.MIT.EDU > Assunto: Re: [EE]: Serial Port and Linux > > Are you opening the serial port with all the right options? I've never had > that > happen, but it sounds like some sort of echo. Do you know that the device > you > are reading from is not actually sending the newlines? > > Here is my serial port initialization function. You'll need to set the > options > to what you need, but... Maybe you will find something missing in your code > > I've used this exact code in several projects over the last year. Including > one > just a few weeks ago, so I know it works. > > Just pass this function the port number and it returns a file descriptor to > the > open port. > > > int initialize_serial(int port_num) > { > struct termios options; > int fd=0; > > //open port > switch(port_num) > { > case 0: > fd=open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY); > break; > case 1: > fd=open("/dev/ttyS1", O_RDWR | O_NOCTTY | O_NDELAY); > break; > case 2: > fd=open("/dev/ttyS2", O_RDWR | O_NOCTTY | O_NDELAY); > break; > case 3: > fd=open("/dev/ttyS3", O_RDWR | O_NOCTTY | O_NDELAY); > break; > } > > //make sure it opened okay > if(fd==-1) > { > printf("Error opening serial port\r\n"); > } > > fcntl(fd, F_SETFL, 0); > > //get the current port options > tcgetattr(fd, &options); > > //set baud rate > cfsetispeed(&options, B38400); > cfsetospeed(&options, B38400); > > //set up port for 8N1 > options.c_cflag |= (CLOCAL | CREAD); > options.c_cflag &= ~PARENB; > options.c_cflag &= ~CSTOPB; > options.c_cflag &= ~CSIZE; > options.c_cflag |= CS8; > > //options.c_lflag |= (ICANON); > options.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL | ISIG); > > options.c_cc[VTIME]=0; > options.c_cc[VMIN]=6; > > //apply new settings > tcsetattr(fd, TCSANOW, &options); > > return fd; > } > > > > Good luck, > Eric > > > > On Mon, 16 Feb 2004 15:12:28 -0500 > Herbert Graf wrote: > > > Hello all, > > > > I'm trying to write a small program that sends a few things over a serial > > port and then reads a few things back. I've having trouble getting it to > > work. > > > > I'm using open, write and read to do my things, accessing the device > through > > the /dev/ttyS1 interface. Things work fine for a while and then seem to go > > crazy. For example, I can send a few things down the line, but then when I > > do a single byte read from the port I continuously get the newline > > character. > > > > Anybody have some simple gcc compatible c code of accessing the serial > port > > in Linux? I'm using Redhat 9.0 and gcc. Thanks, TTYL > > > > ---------------------------------- > > Herbert's PIC Stuff: > > http://repatch.dyndns.org:8383/pic_stuff/ > > > > -- > > http://www.piclist.com hint: PICList Posts must start with ONE topic: > > [PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads > > -- > http://www.piclist.com hint: PICList Posts must start with ONE topic: > [PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads > > > -- > http://www.piclist.com hint: PICList Posts must start with ONE topic: > [PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads > > -- http://www.piclist.com hint: PICList Posts must start with ONE topic: [PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads