By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,784 Members | 2,933 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,784 IT Pros & Developers. It's quick & easy.

write binary data to serial port

P: n/a
Hi,

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.

How do I do this?

Many Thanks!!

Regards,
Tom
Nov 14 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Tom Van Ginneken vient de nous annoncer :
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.

How do I do this?

Many Thanks!!

Regards,
Tom


The C-language doesn't deal with the serial ports. You should ask to a
newsgroup dedicated to your platform, probably one with 'unix' in its
name.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

"C is a sharp tool"

Nov 14 '05 #2

P: n/a
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
Nov 14 '05 #3

P: n/a
Tom Van Ginneken wrote:
Hi,

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.

How do I do this?

Many Thanks!!

Regards,
Tom


Change the run-time library or operating system so that
a file descriptor of a serial port works correctly. Or
you could consult your operating system documents to
find out what descriptor, if any, are used for the
serial port(s).

I altered the code on one embedded system to use
additional file descriptors for serial ports. Worked
out nice!

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Nov 14 '05 #4

P: n/a
kal
"Tom Van Ginneken" <tv**********@pandora.be> wrote in message news:<h%**********************@phobos.telenet-ops.be>...
size_t write(int fd, const void *buf, size_t count);
This is the function declaration.
write (filedescriptor,"a",1);


The write function is called here to write 1 character from
the buffer "a" (which is a string literal.) This can also
be called as follows (hopefully).

write (filedescriptor,"\141",1);
write (filedescriptor,"\141b",1);
write (filedescriptor,"\141\142",1);

The following may write the characters 'a' and then 'b'.

write (filedescriptor,"\141\142",2);
write (filedescriptor,"\141b",2);
write (filedescriptor,"ab",2);

The following may write the bit pattern 00000011.

write (filedescriptor,"\003",1);
Nov 14 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.