From owner-freebsd-questions@FreeBSD.ORG Sun Jul 31 22:52:22 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 366DC16A41F for ; Sun, 31 Jul 2005 22:52:22 +0000 (GMT) (envelope-from youshi10@u.washington.edu) Received: from mxout2.cac.washington.edu (mxout2.cac.washington.edu [140.142.33.4]) by mx1.FreeBSD.org (Postfix) with ESMTP id 0BBAA43D58 for ; Sun, 31 Jul 2005 22:52:18 +0000 (GMT) (envelope-from youshi10@u.washington.edu) Received: from smtp.washington.edu (smtp.washington.edu [140.142.33.9]) by mxout2.cac.washington.edu (8.13.4+UW05.04/8.13.4+UW05.05) with ESMTP id j6VMq9gu030024 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Sun, 31 Jul 2005 15:52:09 -0700 X-Auth-Received: from [140.142.179.12] (cs333-11.spmodem.washington.edu [140.142.179.12]) (authenticated authid=youshi10) by smtp.washington.edu (8.13.4+UW05.04/8.13.4+UW05.07) with ESMTP id j6VMq3Sf026415 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NOT); Sun, 31 Jul 2005 15:52:06 -0700 Message-ID: <42ED5617.1000707@u.washington.edu> Date: Sun, 31 Jul 2005 15:52:07 -0700 From: Garrett Cooper User-Agent: Mozilla Thunderbird 1.0.6 (Macintosh/20050716) X-Accept-Language: en-us, en MIME-Version: 1.0 To: paulh@bdug.org.au, freebsd-questions@freebsd.org References: <20050731160048.GB49839@gothmog.gr> <01dd01c595ec$2ed504e0$6600a8c0@w2k2> <20050731201532.GB1052@gothmog.gr> In-Reply-To: <20050731201532.GB1052@gothmog.gr> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Cc: Giorgos Keramidas Subject: Re: C program to write to the com port 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: Sun, 31 Jul 2005 22:52:22 -0000 Giorgos Keramidas wrote: >On 2005-08-01 00:23, Paul Hamilton wrote: > > >>Yes, very full on code ;-) I will see if I can get it to compile! >> >>Thanks for that. In the mean time I pondered the endianess thing, and I >>have tried using my original code to write 1 byte at a time, 3 times to the >>com port, i.e.: >> >> // ok, lets transmit our 3 bytes to com port 1 >> n = write(dcf_dev, "0xFF", 1); >> if (n < 0) >> fputs("write() of 1 byte failed!\n", stderr); >> printf("Output status: %d bytes written out\n", n); >> >> > >That's wrong. You are not writing the binary value 0xFF but the first >character of the string "0xFF", which is '0'. Why don't you really use >a buffer, like I said? > > #include > #include > > unsigned char buf = { 0xFF, 0x00, 0x90 }; > size_t buflen = sizeof(buf) / sizeof(buf[0]); > > if (write(dcf_dev, buf, buflen) != buflen) { > err(1, "write"); > >The use of err() for printing the error message is the preferred way of >showing why things have failed, so avoid cryptic messages that seem >informational but in reality hide the reason of the failure, like: > > "failed to write byte 1" > >The failure is reported, but what is missing here is the reason for the >failure, which is more useful knowledge :-) > > > >> n = write(dcf_dev, "0x01", 1); >> if (n < 0) >> fputs("write() of 1 byte failed!\n", stderr); >> printf("Output status: %d bytes written out\n", n); >> >> > >Same bug as above. > > > >> n = write(dcf_dev, "0x31", 1); >> if (n < 0) >> fputs("write() of 1 byte failed!\n", stderr); >> printf("Output status: %d bytes written out\n", n); >> >> > >Ditto. > > > >>This way, I thought I should be able to get around all of the endianess of >>it all. >> >> > >No. Unless you start writing binary data in your "string constants", >you are not going to make it work. One way of doing this is: > > write(dcf_dev, "\xff\x00\x90", 3); > >But using string constants for binary data is going to get tricky >immediately after you find yourself in the need for embedding the string >terminating ASCII NUL character, '\0'. > >Just don't :-) > > > >>How are you on Serial port programming? Do you think I have the port >>programmed properly? >> >> > >The serial port setup part seemed fairly correct. Now you must make >sure you send the correct binary data to it. > > As for buffering, it's an incredibly great idea because otherwise your data will become corrupted in transit. That's an issue I'm dealing with in terms of programming a simple flow control system for an interface with my Tern board. For me, since we are using semi-medium sized buffers, I needed to use COM port buffers 4 times the actual size of my data being sent, because of corruption issues. That just was required because of the algorithms in use. Another way to determine sizes, search for limits.h under /usr/ssys/i386 (I think that was the location, as I don't have my machine in front of me). Another way to do it under linux would be /usr/src/limits.h for sure. -Garrett