473,890 Members | 1,360 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 16035
#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************ *********@f16g2 000cwb.googlegr oups.com>,
<je**********@h otmail.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**********@h otmail.comwrote in message
news:11******** *************@f 16g2000cwb.goog legroups.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************ *******@newsrea d2.news.pas.ear thlink.net>,
Mike Wahler <mk******@mkwah ler.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**********@ho tmail.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.n rc-cnrc.gc.cawrote in message
news:eh******** **@canopus.cc.u manitoba.ca...
In article <5D************ *******@newsrea d2.news.pas.ear thlink.net>,
Mike Wahler <mk******@mkwah ler.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.n rc-cnrc.gc.cawrote in message
news:eh******* ***@canopus.cc. umanitoba.ca...
>In article <5D************ *******@newsrea d2.news.pas.ear thlink.net>,
Mike Wahler <mk******@mkwah ler.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 "technicall y correct" English; but since when was rock & roll "technicall y 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.n rc-cnrc.gc.cawrote in message
news:eh****** ****@canopus.cc .umanitoba.ca.. .
>>In article <5D************ *******@newsrea d2.news.pas.ear thlink.net>,
Mike Wahler <mk******@mkwah ler.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 "technicall y correct" English; but since when was rock & roll "technicall y correct"?
Oct 24 '06 #9

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

Similar topics

8
1972
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 it printed "^C". there is a file called charset.c in the distribution of less ftp://ftp.gnu.org/gnu/less/less-382.tar.gz] which achieves this, but it is quite long winded and i do not wish to have to copy all the relevant code, as it is longer...
1
6191
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 ? Is the stringbuilder restricted to pure readable ascii code contents ?
12
3186
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 what i need to know how to do is make a message in ascii that is what the server is looking for. for example , to send a request, the first three bytes have to be ascii "STX"
2
6603
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 displays a square. How can i solve this minor problem. PS I tried different fonts including Lucinda Console, but i still see squares or pipes like ( | ) Please give me a hint
25
11917
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 int main (){
7
9323
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 newline characters, use ASCII character 0x02. A blank like would be represented by two 0x02 characters in a row. I am having a hard time figuring out how to output this. i have tried a few things such as
66
3189
by: dattts | last post by:
what is the difference between a single character and a string consisting only one character
0
1087
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 a ... uh ... NUL character ... a character that the transmitter is free to add as much as it wishes, say to keep a connection alive or to provide a timing delay ... and which the receiver is free to discard on receipt. A no-op.
4
2042
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 honest with myself, I am not even sure that the most recent data is always UTF-8-- data is entered on web forms and I wouldn't be surprised if data of other encodings is slipping in. Up to the point I have just ignored the problem-- on the web...
0
9980
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
11236
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10830
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10925
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10468
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
8018
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7172
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5855
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
4276
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.