Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > how to print ASCII "ETX" character?

Reply
Thread Tools

how to print ASCII "ETX" character?

 
 
jeffpierce12@hotmail.com
Guest
Posts: n/a
 
      10-20-2006
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.

 
Reply With Quote
 
 
 
 
Michael
Guest
Posts: n/a
 
      10-20-2006

> #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

 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      10-20-2006
In article <(E-Mail Removed). com>,
<(E-Mail Removed)> 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
 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      10-20-2006

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> 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


 
Reply With Quote
 
Walter Roberson
Guest
Posts: n/a
 
      10-20-2006
In article <5Db_g.16446$(E-Mail Removed) k.net>,
Mike Wahler <(E-Mail Removed)> 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)
 
Reply With Quote
 
SM Ryan
Guest
Posts: n/a
 
      10-21-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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.
 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      10-21-2006

"Walter Roberson" <(E-Mail Removed)-cnrc.gc.ca> wrote in message
news:ehbhia$jd8$(E-Mail Removed)...
> In article <5Db_g.16446$(E-Mail Removed) k.net>,
> Mike Wahler <(E-Mail Removed)> 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


 
Reply With Quote
 
Peter Shaggy Haywood
Guest
Posts: n/a
 
      10-24-2006
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" <(E-Mail Removed)-cnrc.gc.ca> wrote in message
>news:ehbhia$jd8$(E-Mail Removed)...
>> In article <5Db_g.16446$(E-Mail Removed) k.net>,
>> Mike Wahler <(E-Mail Removed)> 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"?
 
Reply With Quote
 
Peter Shaggy Haywood
Guest
Posts: n/a
 
      10-24-2006
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" <(E-Mail Removed)-cnrc.gc.ca> wrote in message
>>news:ehbhia$jd8$(E-Mail Removed)...
>>> In article <5Db_g.16446$(E-Mail Removed) k.net>,
>>> Mike Wahler <(E-Mail Removed)> 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"?
 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Regex with ASCII and non-ASCII chars TOXiC Python 5 01-31-2007 04:48 PM
[FR/EN] how to convert the characters ASCII(0-255) to ASCII(0-127) Alextophi Perl Misc 8 12-30-2005 10:43 AM
Unlarging the print to print using PDF file to print Bun Mui Computer Support 3 09-13-2004 03:15 AM
routine/module to translate microsoft extended ascii to plain ascii James O'Brien Perl Misc 3 03-05-2004 04:33 PM



Advertisments