Tom Van Ginneken <tv**********@pandora.be> wrote:
I need to write binary data to a serial port. I am using this function:
#include <unistd.h>
ssize_t write(int fd, const void *buf, size_t count);
I am able to write a alpha-numeric character to the port using this:
write (filedescriptor,"a",1);
But I want to write a byte of 1's and 0's to this port. For examples, I want
to write 00000011 to this file descriptor.
Please understand that questions about serial ports and functions like
write() are off-topic here - if you have problems with these you will
get a friendlier reception in e.g. comp.unix.programmer (at least
that's what seems to be appropriate from your use of the non-standard
include file <unistd.h>).
But what you seem to be missing is that 'a' is already a bit pattern,
on a machine with an ASCII character set it's 01100001. All you have
to do is to stick the bit pattern you want to send into a char and
then send that. If you want to send e.g. the bit patterns
10111001
00000011
10000001
you would create an array of 3 chars, set its elements to these values
and then send them, e.g.
unsigned char data[ 3 ] = { 0xB9, 0x03, 0x81 };
write( filedescriptor, data, 3 );
Since in C you can't specify binary numbers you have to convert
your bit patterns into the corresponding hexadecimal or octal or
decimal values, so
unsigned char data[ 3 ] = { 0xB9, 0x03, 0x81 }; /* hex */
unsigned char data[ 3 ] = { 0271, 03, 0201 }; /* oct */
unsigned char data[ 3 ] = { 185, 3, 129 }; /* dec */
would all do the trick for the above set of binary values.
Regards, Jens
--
\ Jens Thoms Toerring ___
Je***********@physik.fu-berlin.de
\__________________________
http://www.toerring.de