Date: Sun, 31 Jul 2005 15:52:07 -0700 From: Garrett Cooper <youshi10@u.washington.edu> To: paulh@bdug.org.au, freebsd-questions@freebsd.org Cc: Giorgos Keramidas <keramida@ceid.upatras.gr> Subject: Re: C program to write to the com port Message-ID: <42ED5617.1000707@u.washington.edu> In-Reply-To: <20050731201532.GB1052@gothmog.gr> References: <20050731160048.GB49839@gothmog.gr> <01dd01c595ec$2ed504e0$6600a8c0@w2k2> <20050731201532.GB1052@gothmog.gr>
next in thread | previous in thread | raw e-mail | index | archive | help
Giorgos Keramidas wrote: >On 2005-08-01 00:23, Paul Hamilton <paulh@bdug.org.au> 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 <err.h> > #include <unistd.h> > > 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
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?42ED5617.1000707>