From: "Suma.H.S" <hssuma@yahoo.com>
To: <uttara@yahoogroups.com>
Sorry for the delay...
> Hello,
> How do u generate a clock pulse on a CRO
I do not know how you would generate pulses on a CRO.
Probably it depends whether the CRO could directly
be connected to the computer (via COM1 or COM2).
If not, there should be some sort of intermediate controller
between the computer and the CRO. In that case you will be
writing a C program for the controller to interact with the
CRO. Considering the latter case to be true, following
can be observed:
In Linux:
You can use inb(2) and outb(2) calls. These are actually
macros and should be compiled with -O or -O2 compiler
option. But, there is a restriction on using these calls.
A user mode program can not do I/O on ports unless
it has permission to do it. Two solutions to this are:
* use ioperm(2) to allow i/o. Should be executed by the
superuser. See "man ioperm"
* Or simply run as superuser.
In DOS:
You can use inp() and outp().
> or on to the Printer using "C"...
> I know i shud give the port address..and the period shud be 12 micro secs...
Linux specific:
It is very easy to write a program that prints a waveform on the printer.
In Linux, every device is considered as a file. A printer is also a file.
FOR A LOCAL PRINTER:
Here the printer is attached to the system via COM1 or COM2.
The solution is the simplest.
/* local_print.c */
#include <stdio.h>
void
do_local_printing ( const FILE *fp, const char * print_info )
{
/*
* PRINT_INFO is a pointer to a zero terminated buffer
* containg info to be printed (better say, to be written).
* You need an algorithm to fill this buffer, and pass it
* to this function. See section Algorithm below.
*/
fprintf ( fp, "%s", print_info );
return;
}
int
main ()
{
/* Assuming COM1 */
FILE *fp = fopen ( "/dev/ttyS0", "r" );
if ( NULL == fp )
{
perror ( "Priter error" );
exit ( 1 );
}
/* Do something to generate print information */
...
do_local_printing ( fp, print_info );
...
exit ( 0 );
}
FOR A NETWORK PRINTER:
In this, a printer is a shareable resource. You would have
seen this kind of setup in the college or in an organization.
We use socket programming to do this kind of printing from
the user mode.
In this setup, the printer(s) will have an IP address. You
should find out what is the IP address and port number of
your printer.
/* ntwrk_prn.c */
/*
* I assume 500, but this should be confirmed with
* the system administrator
*/
#define PORT 500
/* Consult system administrator */
static const char* ip_address = "172.16.1.11";
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
void
do_network_printing ( int sd, const char *print_info,
int num_bytes )
{
/*
* NUM_BYTES is the number of bytes to be printed
*/
int ret = write ( sd, print_info, num_bytes );
if ( ret != num_bytes )
{
perror ( "Written with some error" );
}
return;
}
int
main()
{
struct sockaddr server_address;
int ret;
int sd = socket ( AF_INET, SOCK_STREAM, 0 );
if ( -1 == sd )
{
perror ( "Printer error" );
exit ( 1 );
}
bzero ( &server_address, sizeof server_address );
server_address.sin_family = AF_INET;
server_address.sin_port = htons ( PORT );
ret = inet_pton ( AF_INET, ip_address,
&server_address.sin_address );
if ( 1 != ret )
{
perror ( "Printer error" );
exit ( 2 );
}
ret = connect ( sd, (struct sockaddr *) &server_address,
sizeof server_address );
if ( -1 == ret )
{
perror ( "Printer error" );
exit ( 3 );
}
/* Do something to generate print information */
...
do_network_printing ( sd, print_info, strlen ( print_info ) );
...
return;
}
ALGORITHM:
This is somewhat complex thing to do. I will only present an
algorithm, and leave coding upon you. For the following
discussion, I will assume a square waveform for it is easier
to draw. (See the attached waveform.jpg)
I use following variables and assume that a valid value is
passed:
AMPLITUDE - amplitude of the waveform
PERIOD - distance between corresponding edges
BUFFER - Output of this algorithm to be sent for
printing. I assume char buffer[25][80].
All initialized to spaces.
H_CHAR - '-'
V_CHAR - '|'
A_CHAR - '#'; Time axis
H_SCALE - Number of characters that make half of
the PERIOD (H_CHAR)
V_SCALE - Number of characters that make the
AMPLITUDE (V_CHAR)
X_POS - x coordinate of the origin
Y_POS - y coordinate of the origin
P_VOLT - Maximum positive voltage
N_VOLT - Maximum negative voltage
Voltage range: -5v to +5v.
Step 1: Draw the voltage axis and time axis.
Step 2: Let N_PERIODS = number of waves to draw
Step 3: Initialize BUFFER to all spaces
Step 4: For I = 1 to N_PERIODS Repeat
Step 5: For J = 1 to H_SCALE Repeat
BUFFER[X_POS+J][Y_POS+P_VOLT] = H_CHAR
INC J
EndFor
For J = P_VOLT to N_VOLT Repeat
BUFFER[X_POS+H_SCALE][Y_POS+J] = V_CHAR
DEC J
EndFor
For J = H_SCALE+1 to 2*H_SCALE-1 Repeat
BUFFER[X_POS+J][Y_POS+N_VOLT] = H_CHAR
INC J
EndFor
For J = P_VOLT to N_VOLT Repeat
BUFFER[X_POS+2*H_SCALE][Y_POS+J] = V_CHAR
DEC J
EndFor
EndFor (*Step 3*)
(*End of algorithm*)
This is really a brute force method to solve this problem.
But, I hope it works. Please do let me know if you are
able to implement and run this.
Thanks.
--
Vijay Kumar R Zanvar