Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > write binary data to serial port

Reply
Thread Tools

write binary data to serial port

 
 
Tom Van Ginneken
Guest
Posts: n/a
 
      07-23-2004
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


 
Reply With Quote
 
 
 
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      07-23-2004
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"

 
Reply With Quote
 
 
 
 
Jens.Toerring@physik.fu-berlin.de
Guest
Posts: n/a
 
      07-23-2004
Tom Van Ginneken <> 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 ___
\__________________________ http://www.toerring.de
 
Reply With Quote
 
Thomas Matthews
Guest
Posts: n/a
 
      07-23-2004
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

 
Reply With Quote
 
kal
Guest
Posts: n/a
 
      07-24-2004
"Tom Van Ginneken" <> wrote in message news:<h%3Mc.192048$>...

> 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);
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
emulate a serial port in windows (create a virtual 'com' port) Pom Python 2 01-31-2007 07:49 PM
Serial Port(COM)Port Saref Aref Computer Information 2 02-14-2005 07:38 AM
Can I connect router Serial interface directly to a PC serial port? Faustino Dina Cisco 2 08-18-2004 02:30 AM
Re: Serial port and PS/2 port schematics OR Assistive Tech. suggestion naive.verizon@locality.net Computer Support 1 07-10-2003 11:46 AM
Re: Serial port and PS/2 port schematics °Mike° Computer Support 1 07-09-2003 10:30 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57