From owner-freebsd-questions@FreeBSD.ORG Fri Sep 9 00:23:15 2005 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 F2EFE16A41F for ; Fri, 9 Sep 2005 00:23:14 +0000 (GMT) (envelope-from paulh@bdug.org.au) Received: from mail.bdug.org.au (mail.bdug.org.au [202.72.170.31]) by mx1.FreeBSD.org (Postfix) with ESMTP id B996443D45 for ; Fri, 9 Sep 2005 00:23:13 +0000 (GMT) (envelope-from paulh@bdug.org.au) Received: from localhost (localhost.bdug.org.au [127.0.0.1]) by mail.bdug.org.au (Postfix) with ESMTP id 7A47CE20; Fri, 9 Sep 2005 08:24:15 +0800 (WST) Received: from mail.bdug.org.au ([127.0.0.1]) by localhost (ant.bdug.org.au [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 13735-04; Fri, 9 Sep 2005 08:24:13 +0800 (WST) Received: from w2k2 (unknown [192.168.0.102]) by mail.bdug.org.au (Postfix) with ESMTP id 0F2CEDD2; Fri, 9 Sep 2005 08:24:13 +0800 (WST) From: "Paul Hamilton" To: Date: Fri, 9 Sep 2005 08:23:10 +0800 Message-ID: <053c01c5b4d4$a8ba8670$6600a8c0@w2k2> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable X-Priority: 3 (Normal) X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook, Build 10.0.6626 Importance: Normal X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1506 X-Virus-Scanned: amavisd-new at bdug.org.au Subject: Re: C program to write to the com port - RESOLVED 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: Fri, 09 Sep 2005 00:23:15 -0000 Hi, Thanks to the patience and persistens of Giorgos, Garret and David, I = now have a *sample* program that will transmit 3 bytes of data (mini-ssc protocol), via a serial port to a 8 channel servo controller board. I = will continue to develop this as needed. =20 DISCLAIMER: This is being posted for archival purposes, and no doubt = can use a lot of improvement! -------------------------------------------------------------------------= --- -------- /* Name: testssc.c =20 * compile with: gcc testssc.c -o testssc * =20 * Your serial cable should be plugged into com port 1. =20 * You only need the pin 3 and pin 5 (DB9) plugged into the controller. * The servo should be plugged into the first servo channel/port. * This test program when run will move the servo from midrange,=20 * to position 01. This is for demonstrational use only. * Tested with FreeBSD 5.4 * Paul Hamilton 8th Aug 2005 */ #include #include #include #include #include /*Originally it was termio.h*/ #include #include // Use serial port 0 (com port 1)=20 static char *opt_comport=3D"/dev/cuaa0"; int main(int argc, char **argv) { int fd; struct termios options; unsigned char buf[4]; // ok, lets try opening the com port printf("Opening Com port: %s\n\n", opt_comport); if((fd =3D open(opt_comport, O_RDWR | O_NOCTTY )) < 0)=20 { printf("Problems opening %s\n", opt_comport); return (-1); } // set the required com port parrameters options.c_cflag &=3D ~CSIZE; /* Mask the character size bits */ options.c_cflag |=3D CS8; /* Select 8 data bits */ options.c_cflag &=3D ~PARENB; // set no parity options.c_cflag &=3D ~CSTOPB; // set 1 stop bit options.c_oflag &=3D ~OPOST; // Raw output tcgetattr(fd, &options); /* * Set the baud rates to 9600... */ cfsetispeed(&options, B9600); cfsetospeed(&options, B9600); /* * Enable the receiver and set local mode... */ options.c_cflag |=3D (CLOCAL | CREAD); /* * Set the new options for the port... */ tcsetattr(fd, TCSANOW, &options); // ok, lets transmit our 3 bytes through com port 1 snprintf(buf,4,"%c%c%c%c",0xff,0x00,0x01,0); printf("buf=3D%x,%x,%x,%x\n", buf[0],buf[1],buf[2],buf[3]); if (write(fd, buf, 3) !=3D 3) err(1, "write"); close(fd); }; -------------------------------------------------------------------------= --- -------- Cheers, Paul Hamilton PS. I have three books on programming in C winging their way to = Australia. I have a lot to learn :-)