From owner-freebsd-questions@FreeBSD.ORG Thu Sep 7 00:13:47 2006 Return-Path: X-Original-To: freebsd-questions@freebsd.org Delivered-To: freebsd-questions@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id D995C16A585 for ; Thu, 7 Sep 2006 00:13:47 +0000 (UTC) (envelope-from af300wsm@gmail.com) Received: from py-out-1112.google.com (py-out-1112.google.com [64.233.166.178]) by mx1.FreeBSD.org (Postfix) with ESMTP id D0FBB43D5E for ; Thu, 7 Sep 2006 00:13:37 +0000 (GMT) (envelope-from af300wsm@gmail.com) Received: by py-out-1112.google.com with SMTP id o67so33321pye for ; Wed, 06 Sep 2006 17:13:36 -0700 (PDT) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:to:subject:in-reply-to:mime-version:content-type:references; b=R4dQpKshtx7JFSihqnzOHHTcLapdVA81AqdoNfj3TAF0aofvzvKyK65s8XFHQy0q698SJaRrwJrhVpJ1a1A2+fJyfYMcGwOlWCUJYD4aedH7H1pe5d0Zm7fWVuTtyxYXKyTXVY5lq578LugtevvqcWLttNQ55kKBYWoaH9E8Ma0= Received: by 10.35.63.2 with SMTP id q2mr110650pyk; Wed, 06 Sep 2006 17:13:36 -0700 (PDT) Received: by 10.35.44.12 with HTTP; Wed, 6 Sep 2006 17:13:36 -0700 (PDT) Message-ID: <340a29540609061713m78f85d8bu549e84c6fecb37a9@mail.gmail.com> Date: Wed, 6 Sep 2006 18:13:36 -0600 From: "Andrew Falanga" To: "=?KOI8-R?B?88XSx8XKIPPPwsvP?=" , freebsd-questions In-Reply-To: <200609042233.24740.bug2bug@bug2bug.tk> MIME-Version: 1.0 References: <200609042233.24740.bug2bug@bug2bug.tk> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline X-Content-Filtered-By: Mailman/MimeDel 2.1.5 Cc: Subject: Re: A question about programming RS-232 X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 07 Sep 2006 00:13:47 -0000 I am by no means the worlds best serial programmer, but recently I have done some work on this subject and I noticed one thing in the code sample above that should be avoided. However, I'll give you what I saw in-line: #include > #include > #include > #include > > int main(void) { > int t = 0, num = 10, fd, iOut; char *ch; > struct termios my_termios; > ch = (char *)malloc(6); > memset(ch, 250, 6); > fd = open("/dev/cuad0", O_RDWR | O_NONBLOCK); Ok, great, we've opened our serial device. Unless you need this to be a controlling terminal, you should open with open( "/dev/cuad0", O_RDWR | O_NONBLOCK | O_NOCTTY ); Check with the open man page to make sure I've given you the correct constant for opening as a non-controlling terminal. printf("Opened com port\n"); > if(fd < 0) return 0; > // tcflush(fd, TCIFLUSH); > my_termios.c_cflag = CS8 | CLOCAL; > if(cfsetspeed(&my_termios, B9600) < 0) return 0; > if(tcsetattr(fd, TCSANOW, &my_termios) < 0) return 0; You've set the attributes you want to use in the structure you defined, my_termios. However, you should call tcgetattr() before changing what you want to change (and make sure you always turn things on as you have done above with bitwise or). So, your code should look something like, // assume an open file descriptor named fd struct termios my_termios; if( tcgetattr( fd, &my_termios ) < 0 ) { fprintf( stderr, "error in getting termios properties\n" ); return AN_ERROR; } // turn on what you want my_termios.c_cflag = CS8 | CLOCAL; if( tcsetattr( fd, &my_termios ) < 0 } { fprintf( stderr, "error in setting new properties to serial port\n" ); return AN_ERROR; } I don't know if this will solve your problems but I do know I read that you should always get the current settings because the serial driver may use certain bits and you don't want to turn them off. Also, if you're going to return the port settings to the state before you took hold of it, make two termios structures and stuff the original settings away to be restored upon exit or close of the port. Lastly, here is a link to a serial programming guide that I found quite helpful. The info is probably dated to some degree, but it is non the less useful. http://www.easysw.com/~mike/serial/serial.html Andy