71
Chapter 8 Program Examples
Serial Interface Program
The following example is a communication program. It is programmed with C Language and working in
DOS environment. Where the MAIN Function could be extended for communication functions by users
and other sub functions show you how to input and output character strings using serial interfaces.
#define PORT 0
#include "dos.h"
#include "stdio.h"
#include "stdlib.h"
#include "ctype.h"
#include "string.h"
#include "conio.h"
void port_init( int port,unsigned char code );
int check_stat( int port );
/* read serial port state(16bit) */
void send_port( int port,char c );
/* send a character to serial port */
char read_port( int port );
/* receive a character form serial port */
void string_wr( char *ps );
/* write a string to serial port */
void string_rd( char *ps );
/* read a string from serial port */
char input[256]; /* query receive buffer */
main()
{ port_init( PORT,0xe3 );/* initialize serial port:baud = 9600,no verify,1 bit stop,8 bit data */
string_wr( "trig:sour bus;*trg" );
string_rd( input );
printf( "\n%s",input );
string_wr( "volt:dc:rang 1.0" );
string_wr( "func ‗volt:ac‘ );
}
/* write string to serial port */
void string_wr( char *ps )
{ char c;
int m,n;
while( check_stat(PORT) & 256) read_port(PORT);/* read data until null */
for( ;*ps; )