write binary data to serial port

T

Tom Van Ginneken

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
 
E

Emmanuel Delahaye

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.
 
J

Jens.Toerring

Tom Van Ginneken said:
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
 
T

Thomas Matthews

Tom said:
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
 
K

kal

Tom Van Ginneken said:
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);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,190
Latest member
ClayE7480

Latest Threads

Top