473,396 Members | 1,814 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

how to print ASCII "ETX" character?

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 <hereline, 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 '06 #1
8 15822
#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 <hereline, 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

Oct 20 '06 #2
In article <11*********************@f16g2000cwb.googlegroups. com>,
<je**********@hotmail.comwrote:
>At the <hereline, 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
Oct 20 '06 #3

<je**********@hotmail.comwrote in message
news:11*********************@f16g2000cwb.googlegro 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 <hereline, 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
Oct 20 '06 #4
In article <5D*******************@newsread2.news.pas.earthlin k.net>,
Mike Wahler <mk******@mkwahler.netwrote:
>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)
Oct 20 '06 #5
je**********@hotmail.com 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.
Oct 21 '06 #6

"Walter Roberson" <ro******@ibd.nrc-cnrc.gc.cawrote in message
news:eh**********@canopus.cc.umanitoba.ca...
In article <5D*******************@newsread2.news.pas.earthlin k.net>,
Mike Wahler <mk******@mkwahler.netwrote:
>>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
Oct 21 '06 #7
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" <ro******@ibd.nrc-cnrc.gc.cawrote in message
news:eh**********@canopus.cc.umanitoba.ca...
>In article <5D*******************@newsread2.news.pas.earthlin k.net>,
Mike Wahler <mk******@mkwahler.netwrote:
>>>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"?
Oct 24 '06 #8
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" <ro******@ibd.nrc-cnrc.gc.cawrote in message
news:eh**********@canopus.cc.umanitoba.ca...
>>In article <5D*******************@newsread2.news.pas.earthlin k.net>,
Mike Wahler <mk******@mkwahler.netwrote:

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"?
Oct 24 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Sam Halliday | last post by:
i want to have a function which can print the printable form (possibly a 2 character string) of a character on UNIX like systems. for example, if i were to pass the ascii value '\3', i would like...
1
by: Sagaert Johan | last post by:
Hi I am construncting a string containing some control chars (STX/ETX) I noticed that adding a byte with value 2 is the same as adding a character '2' ??? How can i solve this problem ?...
12
by: nephish | last post by:
Hello there, i need to write a script that can transfer info back and forth with a data server at so-and-so ip. i have Programming Python, which covers socket programming. So thats cool. But...
2
by: FrankvdHorst | last post by:
Hallo, In an application that I'm creating I want to display ☻ and ♥ in a standard textbox. In this message they are displayed correctly. But when I'm programming in Visual Studio my textbox...
25
by: mdh | last post by:
I wrote a little insignificant program, to help me write a more significant one!!...that was supposed to print all Ascii values from 0 to 127: >>>> #include <stdio.h> # define UPPER_LIMT 127...
7
by: LLcoolQ | last post by:
I am writing an interface file to a vendor. The requirements say: Format of the text: Text must be in the format as you would like it to appear without embedded newline characters. Instead of...
66
by: dattts | last post by:
what is the difference between a single character and a string consisting only one character
0
by: BruceMcF | last post by:
On Mar 11, 8:04 am, billg...@cs.uofs.edu (Bill Gunshannon) wrote: Precisely. What makes ASCII NUL an appropriate terminator for terminated strings is that fact that it is defined *in* ASCII as...
4
by: harrelson | last post by:
I have a large amount of data in a postgresql database with the encoding of SQL_ASCII. Most recent data is UTF-8 but data from several years ago could be of some unknown other data type. Being...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.