Correspondence of chr$()?

  • Thread starter Richard Müller
  • Start date
R

Richard Müller

Good morning everybody,
I want to send imprintable characters to a serial LCD device, represented
by their ASCII-value (dec. or hex). For example to clear the screen I
have to send the character with its decimal value of 43. As far as I have
learnt, C++ doesn't know the chr$(43) style. By searching the internet I
found the following solution:
-----------------
int cls = 43;
....
wr=write(fd1, char(cls), 1); //fd1 is the opened display
-----------------
By compiling with g++ (linux) I get:

neuer:/home/richard/prog_PAR # g++ demo_write.cpp
demo_write.cpp: In function ‘int main()’:
demo_write.cpp:30: error: invalid conversion from ‘char’ to ‘const void*’
demo_write.cpp:30: error: initializing argument 2 of ‘ssize_t write
(int, const void*, size_t)’

How do I send the ASCII codes to fd1?

Thanks for helping -
Richard
 
T

Tomislav Novak

Good morning everybody,
I want to send imprintable characters to a serial LCD device, represented
by their ASCII-value (dec. or hex). For example to clear the screen I
have to send the character with its decimal value of 43. As far as I have
learnt, C++ doesn't know the chr$(43) style. By searching the internet I
found the following solution:
-----------------
int cls = 43;
...
wr=write(fd1, char(cls), 1); //fd1 is the opened display
-----------------

write() expects a pointer as a second argument:

char cls = 43;
write(fd1, &cls, 1);
 
J

James Kanze

I want to send imprintable characters to a serial LCD device,
represented by their ASCII-value (dec. or hex). For example to
clear the screen I have to send the character with its decimal
value of 43. As far as I have learnt, C++ doesn't know the
chr$(43) style.

That's because it wouldn't mean anything. In C++, char's are
small integers, so you can just initialize a char with 43, and
otput it.
By searching the internet I
found the following solution:
-----------------
int cls = 43;
...
wr=write(fd1, char(cls), 1); //fd1 is the opened display
-----------------

I don't know where you found that, but it's completely wrong.
For starters, write is part of the Posix system ABI, not C=+,
and it might not be present on your system. And the Posix write
takes a pointer, and not an int, as it's second character.
By compiling with g++ (linux) I get:
neuer:/home/richard/prog_PAR # g++ demo_write.cpp
demo_write.cpp: In function ‘int main()’:
demo_write.cpp:30: error: invalid conversion from ‘char’ to ‘const void*’
demo_write.cpp:30: error: initializing argument 2 of ‘ssize_t write
(int, const void*, size_t)’
How do I send the ASCII codes to fd1?

It depends on what fd1 is. If you're using standard C++ IO (or
the legacy C IO), you'll have to open the stream in binary mode
if you expect to send any special characters (or count on some
addtional support from the implementation), but something like:

std::cout.put( 43 );

is likely to work, provided your system doesn't do anything
special with 43 when it is output to a text stream.

In practice, no system using an ASCII based encoding, like
Unicode or one of the ISO 8859 encodings will, because that's
the plus sign in those codes. Of course, outputting the plus
sign to the terminal will display a plus sign, and not clear the
stream. (On an ANSI terminal, you'll have to output "\x1B"
"2J". But I don't know if that's a very common convention in
modern systems. A somewhat more portable, albeit less elegant
solution, is to output a hundred or so "\n". Which is
guaranteed to work for any screen with less than a hundred or so
lines, portably.)
 
R

Richard Müller

In practice, no system using an ASCII based encoding, like Unicode or
one of the ISO 8859 encodings will, because that's the plus sign in
those codes.

You're right, of course. I looked into the wrong column, CLS is hex 1B 43
or dec 27 67. Sorry for the mistake.
Thanks for your information!
Richard
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top