write binary data to serial port

Discussion in 'C Programming' started by Tom Van Ginneken, Jul 23, 2004.

  1. 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
     
    Tom Van Ginneken, Jul 23, 2004
    #1
    1. Advertising

  2. 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"
     
    Emmanuel Delahaye, Jul 23, 2004
    #2
    1. Advertising

  3. Tom Van Ginneken

    -berlin.de Guest

    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 ___ -berlin.de
    \__________________________ http://www.toerring.de
     
    -berlin.de, Jul 23, 2004
    #3
  4. 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
     
    Thomas Matthews, Jul 23, 2004
    #4
  5. Tom Van Ginneken

    kal Guest

    "Tom Van Ginneken" <> wrote in message news:<h%3Mc.192048$-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);
     
    kal, Jul 24, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. D
    Replies:
    0
    Views:
    1,001
  2. SoftwareTester
    Replies:
    9
    Views:
    9,348
    Bengt Richter
    Nov 18, 2004
  3. Pom
    Replies:
    2
    Views:
    1,772
    Bas-i
    Jan 31, 2007
  4. collinm

    Read Write serial port

    collinm, Mar 21, 2005, in forum: C Programming
    Replies:
    0
    Views:
    655
    collinm
    Mar 21, 2005
  5. msalerno
    Replies:
    3
    Views:
    431
    Ilmari Karonen
    Jul 14, 2005
Loading...

Share This Page