how to print ASCII "ETX" character?

Discussion in 'C Programming' started by jeffpierce12@hotmail.com, Oct 20, 2006.

  1. Guest

    Hello,

    I am trying to send some characters to a scanner that I have hooked up
    to the COM 1 port on my PC. I am running Linux operating system, and I
    have the following sample program:

    #include <stdio.h>

    int main (void)
    {
    FILE *fd;

    fd = fopen("/dev/ttyS0", "w");
    fprintf (fd, "2");
    fprintf (fd, "B");
    <here>
    fprintf (fd, "0");
    fprintf (fd, "A");

    fclose(fd);
    return 0;
    }

    At the <here> line, I would like to send to the device an ASCII "ETX"
    character, which is a hex 03. What is the syntax of the fprintf
    statement to do this?

    Thank you.
     
    , Oct 20, 2006
    #1
    1. Advertising

  2. Michael Guest


    > #include <stdio.h>
    >
    > int main (void)
    > {
    > FILE *fd;
    >
    > fd = fopen("/dev/ttyS0", "w");
    > fprintf (fd, "2");
    > fprintf (fd, "B");
    > <here>
    > fprintf (fd, "0");
    > fprintf (fd, "A");
    >
    > fclose(fd);
    > return 0;
    > }
    >
    > At the <here> line, I would like to send to the device an ASCII "ETX"
    > character, which is a hex 03. What is the syntax of the fprintf
    > statement to do this?


    fprintf(fd, "%c", 0x03);

    Note: You should also change your other fprintfs to look like:
    fprintf(fd, "%c", '2');
    etc.

    fprintf is expecting a file descriptor, a format argument, and
    argument(s) to that. The code you have will work, but is dangerous. I
    once had to track down a seg fault where someone had done this:
    void foo(char* str) {
    fprintf(fd, str);
    }
    instead of
    void foo(char* str) {
    fprintf(fd, "%s", str);
    }


    That worked fine until str contained a percent sign, then it seg
    faulted.

    Be nice to future programmers; use the correct form.

    Michael
     
    Michael, Oct 20, 2006
    #2
    1. Advertising

  3. In article <>,
    <> wrote:

    >At the <here> line, I would like to send to the device an ASCII "ETX"
    >character, which is a hex 03. What is the syntax of the fprintf
    >statement to do this?


    Choices:

    putc(0x03, fd);

    fprintf(fd, "\3");

    fprintf(fd, "%c", 3);


    It is recommended not to use fprintf() if you are not doing any
    formatting -- putc() and fputs() and fwrite() are usually more
    efficient.

    --
    I was very young in those days, but I was also rather dim.
    -- Christopher Priest
     
    Walter Roberson, Oct 20, 2006
    #3
  4. Mike Wahler Guest

    <> wrote in message
    news:...
    > Hello,
    >
    > I am trying to send some characters to a scanner that I have hooked up
    > to the COM 1 port on my PC. I am running Linux operating system, and I
    > have the following sample program:
    >
    > #include <stdio.h>
    >
    > int main (void)
    > {
    > FILE *fd;
    >
    > fd = fopen("/dev/ttyS0", "w");
    > fprintf (fd, "2");


    This will write two characters to 'fd':
    '2' and '\0'. Is that what you wanted?

    > fprintf (fd, "B");


    This will write two characters to 'fd':
    'B' and '\0'. Is that what you wanted?

    > <here>
    > fprintf (fd, "0");


    This will write two characters to 'fd':
    '0' and '\0'. Is that what you wanted?

    > fprintf (fd, "A");


    This will write two characters to 'fd':
    'A' and '\0'. Is that what you wanted?

    > fclose(fd);
    > return 0;
    > }
    >
    > At the <here> line, I would like to send to the device an ASCII "ETX"
    > character, which is a hex 03. What is the syntax of the fprintf
    > statement to do this?


    if(fprintf(fd, "%c", (char)3) != 1)
    ; /* something wrong */


    Note that the format specifier for a single character
    is %c, not %s.

    Also, in all cases, you should be checking the return
    value from 'fprintf()'. It can fail, but you won't
    know unless you check.


    -Mike
     
    Mike Wahler, Oct 20, 2006
    #4
  5. In article <5Db_g.16446$>,
    Mike Wahler <> wrote:

    >> fprintf (fd, "2");


    >This will write two characters to 'fd':
    >'2' and '\0'. Is that what you wanted?


    No it won't. The \0 of the string literal "2" terminates the
    format -- otherwise -every- printf and fprintf would put in the
    extra \0.
    --
    "law -- it's a commodity"
    -- Andrew Ryan (The Globe and Mail, 2005/11/26)
     
    Walter Roberson, Oct 20, 2006
    #5
  6. SM Ryan Guest

    wrote:
    # Hello,
    #
    # I am trying to send some characters to a scanner that I have hooked up
    # to the COM 1 port on my PC. I am running Linux operating system, and I
    # have the following sample program:
    #
    # #include <stdio.h>
    #
    # int main (void)
    # {
    # FILE *fd;
    #
    # fd = fopen("/dev/ttyS0", "w");
    # fprintf (fd, "2");
    # fprintf (fd, "B");
    # <here>
    # fprintf (fd, "0");
    # fprintf (fd, "A");

    You can embed characters except '\0' in a string and print
    them out alongside printable character.

    So you could do
    fputs("2B\x03" "0A",fd);

    #define ETX "\x03"
    fputs("2B" ETX "0A",fd);

    fputc('2',fd); fputc('B',fd); fputc(3,fd); fputc('0',fd); fputc('A',fd);

    enum {ETX=3};
    fputc('2',fd); fputc('B',fd); fputc(ETX,fd); fputc('0',fd); fputc('A',fd);

    fprintf(fd,"2B\x03" "0A");

    #define ETX "\x03"
    fprintf(fd,"2B" ETX "0A");

    enum {ETX=3};
    fprintf(fd,"2B%c0A",ETX);

    You can sometmes #define strings that make commands and let the
    compiler paste them together.
    #define ESC "\x1B"
    #define BEGIN_ROLE ESC "char("
    #define END_ROLE ")"
    #define ARTICHOKE_MODE BEGIN_ROLE "poor little artie" END_ROLE
    #define CLOTHES_MODE ESC "clothes;"
    #define CLOSE "\x18"
    printf(
    CLOSE "B" CLOTHES_MODE
    CLOSE ARTICHOKE_MODE
    "\nWhy he's just a rainbow!\n");

    --
    SM Ryan http://www.rawbw.com/~wyrmwif/
    No pleasure, no rapture, no exquisite sin greater than central air.
     
    SM Ryan, Oct 21, 2006
    #6
  7. Mike Wahler Guest

    "Walter Roberson" <-cnrc.gc.ca> wrote in message
    news:ehbhia$jd8$...
    > In article <5Db_g.16446$>,
    > Mike Wahler <> wrote:
    >
    >>> fprintf (fd, "2");

    >
    >>This will write two characters to 'fd':
    >>'2' and '\0'. Is that what you wanted?

    >
    > No it won't. The \0 of the string literal "2" terminates the
    > format -- otherwise -every- printf and fprintf would put in the
    > extra \0.


    Oops, you're right; I was thinking of 'sprintf()'.

    -Mike
     
    Mike Wahler, Oct 21, 2006
    #7
  8. Groovy hepcat Mike Wahler was jivin' on Sat, 21 Oct 2006 02:24:39 GMT
    in comp.lang.c.
    Re: how to print ASCII "ETX" character?'s a cool scene! Dig it!

    >"Walter Roberson" <-cnrc.gc.ca> wrote in message
    >news:ehbhia$jd8$...
    >> In article <5Db_g.16446$>,
    >> Mike Wahler <> wrote:
    >>
    >>>> fprintf (fd, "2");

    >>
    >>>This will write two characters to 'fd':
    >>>'2' and '\0'. Is that what you wanted?

    >>
    >> No it won't. The \0 of the string literal "2" terminates the
    >> format -- otherwise -every- printf and fprintf would put in the
    >> extra \0.

    >
    >Oops, you're right; I was thinking of 'sprintf()'.


    No you weren't, because that does the same thing as fprintf(), but
    sending its output to memory rather than a file.

    --

    Dig the even newer still, yet more improved, sig!

    http://alphalink.com.au/~phaywood/
    "Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
    I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
     
    Peter Shaggy Haywood, Oct 24, 2006
    #8
  9. Groovy hepcat Peter "Shaggy" Haywood was jivin' on Tue, 24 Oct 2006
    03:53:53 GMT in comp.lang.c.
    Re: how to print ASCII "ETX" character?'s a cool scene! Dig it!

    >Groovy hepcat Mike Wahler was jivin' on Sat, 21 Oct 2006 02:24:39 GMT
    >in comp.lang.c.
    >Re: how to print ASCII "ETX" character?'s a cool scene! Dig it!
    >
    >>"Walter Roberson" <-cnrc.gc.ca> wrote in message
    >>news:ehbhia$jd8$...
    >>> In article <5Db_g.16446$>,
    >>> Mike Wahler <> wrote:
    >>>
    >>>>> fprintf (fd, "2");
    >>>
    >>>>This will write two characters to 'fd':
    >>>>'2' and '\0'. Is that what you wanted?
    >>>
    >>> No it won't. The \0 of the string literal "2" terminates the
    >>> format -- otherwise -every- printf and fprintf would put in the
    >>> extra \0.

    >>
    >>Oops, you're right; I was thinking of 'sprintf()'.

    >
    > No you weren't, because that does the same thing as fprintf(), but
    >sending its output to memory rather than a file.


    Of course, it does store a '\0' at the end. So perhaps you were
    thinking of sprintf() after all. :)

    --

    Dig the even newer still, yet more improved, sig!

    http://alphalink.com.au/~phaywood/
    "Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
    I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
     
    Peter Shaggy Haywood, Oct 24, 2006
    #9
    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. TOXiC
    Replies:
    5
    Views:
    1,292
    TOXiC
    Jan 31, 2007
  2. Replies:
    2
    Views:
    5,505
    Ian Collins
    Jan 19, 2006
  3. keto
    Replies:
    0
    Views:
    1,006
  4. David Cournapeau

    print a vs print '%s' % a vs print '%f' a

    David Cournapeau, Dec 30, 2008, in forum: Python
    Replies:
    0
    Views:
    376
    David Cournapeau
    Dec 30, 2008
  5. John Gregory
    Replies:
    0
    Views:
    304
    John Gregory
    Jul 5, 2009
Loading...

Share This Page