Newbie question on C library system() function

P

philbo30

Newbie question here...

I need to send control data (rewind by 3 lines) to a kiosk printer via
a 115200 tty and then print out information associated with a
transaction. The problem is that my control command is executed via a
bash shell system() call which is apparently much slower than my c
data processing. The end result is a printer trying to back up and
move forward at the same time. Unpretty.

Here' s the code for the system call:

char backup[32]="echo -ne \033K7 > /dev/ttyS0 \n"
int rewindsome(void)
{
system(backup);
system(backup);
system(backup);
return 0;
}

I need to make sure that the function above fully completes before the
rest of my app executes. Ideas?
 
W

Walter Roberson

I need to send control data (rewind by 3 lines) to a kiosk printer via
a 115200 tty and then print out information associated with a
transaction. The problem is that my control command is executed via a
bash shell system() call which is apparently much slower than my c
data processing. The end result is a printer trying to back up and
move forward at the same time. Unpretty.
Here' s the code for the system call:
char backup[32]="echo -ne \033K7 > /dev/ttyS0 \n"
int rewindsome(void)
{
system(backup);
system(backup);
system(backup);
return 0;
}
I need to make sure that the function above fully completes before the
rest of my app executes. Ideas?

You probably don't need the \n in there.

You could do the same thing in C by using

#include <stdio.h>
int rewindsome(void) {
FILE *printer = fopen("/dev/ttyS0","a");
if (!printer) {
perror("Problem opening the printer");
return 1;
}
fprintf( printer, "\033K7\033K7\033K7" );
fclose( printer );
return 0;
}

However, this -probably- won't solve the problem. In my experience, your
problem likely is not with system() being slow, but rather with the
printer being slow and with there being no way to check with the
printer to see whether it completed a command yet. You are sending
the characters to the printer, but the characters are getting buffered
in the printer memory and executed in the printer's own sweet time.

Lacking a way to check completion, the best you would be able to do
would be to wait for a period of time, hoping the command completes
(which would depend on what else the printer is doing); unfortunately,
there is no way in standard C to wait for a given amount of time.
Most operating systems provide ways outside of C to wait gracefully;
chances are that your operating system supports a sleep() function
for example.
 
K

Keith Thompson

philbo30 said:
Newbie question here...

I need to send control data (rewind by 3 lines) to a kiosk printer via
a 115200 tty and then print out information associated with a
transaction. The problem is that my control command is executed via a
bash shell system() call which is apparently much slower than my c
data processing. The end result is a printer trying to back up and
move forward at the same time. Unpretty.

Here' s the code for the system call:

char backup[32]="echo -ne \033K7 > /dev/ttyS0 \n"
int rewindsome(void)
{
system(backup);
system(backup);
system(backup);
return 0;
}

I need to make sure that the function above fully completes before the
rest of my app executes. Ideas?

You're missing a semicolon on the declaration of backup. Please post
actual code; otherwise, we have no way of guessing what other mistakes
you've introduced by re-typing it.

Why do you specify the size of the backup array? Why not just declare
it as

char backup[] = "...";

and let the compiler figure out how big it needs to be?

Printers, ttys, and the "echo" command are all off-topic here. If you
want information about them, you should ask in a system-specific
newsgroup, probably comp.unix.programmer or one of the Linux groups.

<OFFTOPIC>
You don't need the trailing space and newline in the argument to
system(). On some systems you might, but not on the one you appear to
be using.
</OFFTOPIC>

However, I don't think you *need* to use system() at all. Let's
assume that the echo command writes some specified output to some
specified location, and that "/dev/ttyS0" is the name of the output
file. (I happen to know that that's the case for your system, but the
C standard is silent on that question.) Why not just open the file
"/dev/ttyS0" using fopen(), and write data to it directly? You can
use the fflush() function to ensure (more or less) that the data is
actually sent.

That's likely to be faster than what you're doing, but it may or may
not not fix your problem. Is the data being sent to the printer out
of order, or is the problem in the way the printer is processing it?
After sending a command to the printer, do you need to pause before
sending another command? We can't answer those questions here (we
can't really even tell you how to pause), but that should let you make
a bit more progress.
 

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
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top