--------------Boundary-00=_TE47G6G0000000000000 Content-Type: Text/Plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Eric,=0D =0D I've noticed that you initialized fd=3D0. Note that the swich statement h= as 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, ju= st after calling the control functions on fd=3D0.=0D =0D Isn't a good idea to initialize fd=3D-1?=0D =0D Lucas Hartmann=0D =0D -------Mensagem original-------=0D =0D De: pic microcontroller discussion list=0D Data: 02/16/04 17:45:04=0D Para: PICLIST@MITVMA.MIT.EDU=0D Assunto: Re: [EE]: Serial Port and Linux=0D =0D Are you opening the serial port with all the right options? I've never h= ad that=0D happen, but it sounds like some sort of echo. Do you know that the devic= e you=0D are reading from is not actually sending the newlines?=0D =0D Here is my serial port initialization function. You'll need to set the options=0D to what you need, but... Maybe you will find something missing in your c= ode =0D I've used this exact code in several projects over the last year. Includ= ing one=0D just a few weeks ago, so I know it works.=0D =0D Just pass this function the port number and it returns a file descriptor = to the=0D open port.=0D =0D =0D int initialize_serial(int port_num)=0D {=0D struct termios options;=0D int fd=3D0;=0D =0D //open port=0D switch(port_num)=0D {=0D case 0:=0D fd=3Dopen("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDE= LAY);=0D break;=0D case 1:=0D fd=3Dopen("/dev/ttyS1", O_RDWR | O_NOCTTY | O_NDE= LAY);=0D break;=0D case 2:=0D fd=3Dopen("/dev/ttyS2", O_RDWR | O_NOCTTY | O_NDE= LAY);=0D break;=0D case 3:=0D fd=3Dopen("/dev/ttyS3", O_RDWR | O_NOCTTY | O_NDE= LAY);=0D break;=0D }=0D =0D //make sure it opened okay=0D if(fd=3D=3D-1)=0D {=0D printf("Error opening serial port\r\n");=0D }=0D =0D fcntl(fd, F_SETFL, 0);=0D =0D //get the current port options=0D tcgetattr(fd, &options);=0D =0D //set baud rate=0D cfsetispeed(&options, B38400);=0D cfsetospeed(&options, B38400);=0D =0D //set up port for 8N1=0D options.c_cflag |=3D (CLOCAL | CREAD);=0D options.c_cflag &=3D ~PARENB;=0D options.c_cflag &=3D ~CSTOPB;=0D options.c_cflag &=3D ~CSIZE;=0D options.c_cflag |=3D CS8;=0D =0D //options.c_lflag |=3D (ICANON);=0D options.c_lflag &=3D ~(ECHO | ECHOE | ECHOK | ECHONL | ISIG);=0D =0D options.c_cc[VTIME]=3D0;=0D options.c_cc[VMIN]=3D6;=0D =0D //apply new settings=0D tcsetattr(fd, TCSANOW, &options);=0D =0D return fd;=0D }=0D =0D =0D =0D Good luck,=0D Eric=0D =0D =0D =0D On Mon, 16 Feb 2004 15:12:28 -0500=0D Herbert Graf wrote:=0D =0D > Hello all,=0D >=0D > I'm trying to write a small program that sends a few things over a seri= al=0D > port and then reads a few things back. I've having trouble getting it t= o=0D > work.=0D >=0D > I'm using open, write and read to do my things, accessing the device through=0D > the /dev/ttyS1 interface. Things work fine for a while and then seem to= go=0D > crazy. For example, I can send a few things down the line, but then whe= n I=0D > do a single byte read from the port I continuously get the newline=0D > character.=0D >=0D > Anybody have some simple gcc compatible c code of accessing the serial port=0D > in Linux? I'm using Redhat 9.0 and gcc. Thanks, TTYL=0D >=0D > ----------------------------------=0D > Herbert's PIC Stuff:=0D > http://repatch.dyndns.org:8383/pic_stuff/=0D >=0D > --=0D > http://www.piclist.com hint: PICList Posts must start with ONE topic:=0D > [PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads=0D =0D --=0D http://www.piclist.com hint: PICList Posts must start with ONE topic:=0D [PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads=0D =20 -- http://www.piclist.com hint: PICList Posts must start with ONE topic: [PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads --------------Boundary-00=_TE47G6G0000000000000 Content-Type: image/gif; name="IMSTP.gif" Content-Transfer-Encoding: base64 Content-ID: <54FDB7D9-7ABA-4158-AEFB-01AA54F6E7F4> R0lGODlhFAAPALMIAP9gAM9gAM8vAM9gL/+QL5AvAGAvAP9gL////wAAAAAAAAAAAAAAAAAAAAAA AAAAACH/C05FVFNDQVBFMi4wAwEAAAAh+QQJFAAIACwAAAAAFAAPAAAEVRDJSaudJuudrxlEKI6B URlCUYyjKpgYAKSgOBSCDEuGDKgrAtC3Q/R+hkPJEDgYCjpKr5A8WK9OaPFZwHoPqm3366VKyeRt E30tVVRscMHDqV/u+AgAIfkEBWQACAAsAAAAABQADwAABBIQyUmrvTjrzbv/YCiOZGmeaAQAIfkE CRQACAAsAgABABAADQAABEoQIUOrpXIOwrsPxiQUheeRAgUA49YNhbCqK1kS9grQhXGAhsDBUJgZ AL2Dcqkk7ogFpvRAokSn0p4PO6UIuUsQggSmFjKXdAgRAQAh+QQFCgAIACwAAAAAFAAPAAAEEhDJ Sau9OOvNu/9gKI5kaZ5oBAAh+QQJFAAIACwCAAEAEAANAAAEShAhQ6ulcg7Cuw/GJBSF55ECBQDj 1g2FsKorWRL2CtCFcYCGwMFQmBkAvYNyqSTuiAWm9ECiRKfSng87pQi5SxCCBKYWMpd0CBEBACH5 BAVkAAgALAAAAAAUAA8AAAQSEMlJq7046827/2AojmRpnmgEADs= --------------Boundary-00=_TE47G6G0000000000000--