VISUAL BASIC SERIALPORT CLASS ( COMPONENT )  WITH ADR ( RS232 ) DEVICES

This section will demonstrate how to send and receive data from the ADR101 using TURBO C. It outlines the commands used to, configure the serial port (bioscom), send data out through the serial port (fprintf), and receive data through the serial port (fscanf).

Commands used in TURBO C to access the ADR101 require the following include files to be declared at the start of TURBO C programs;

 

#include <stdio.h>
#include <bios.h>        

 

CONFIGURING THE SERIAL PORT

The first step in accessing the ADR101 via the serial port is configuring the serial port to the proper communication parameters which are, 9600 baud, 8 bit words, no parity. This is done using the "bioscom" command. The syntax for this command is;

bioscom (0,settings,com1);

where settings is previously defined as HEX E3 and com1 is defined as 0. Defining "settings" and "com1" should be done using;

#define  com1  0
#define  settings (0xE3)        

These statements should be placed immediatly following your include files (see programming examples). The bioscom command needs only to be executed once before the ADR101 is accessed.

SENDING COMMANDS TO THE ADR101

To send commands to the ADR101 the "fprintf" command is used. For example, the following command sends an RA0 ( read analog port 0 ) command to the ADR101;

fprintf (stdaux,"RA0 \xD");

The \xD suffix sends a carriage return after the command which is needed by the ADR101 to recognize a command. Integer variables may also be used in the command line. For example, the following command sends a MAddd ( make port A=ddd ) command, where DOUT is a previously defined integer value of 0 to 255.

fprintf (stdaux,"MA %d \xD",DOUT);

RECEIVING DATA FROM THE ADR101

If a command sent to the ADR101 is a responsive command, that is, one that results in data being sent back to the host, the data is retrieved using the "fscanf" command. After this command is used the serial buffer must be re-initialized using the "rewind" command. The syntax for this command is;

rewind (stdaux);

This command is executed following the "fscanf" command. For example, the following commands send a RA0 command and stores the retrieved data in a floating point variable named AN0;

fprintf (stdaux,"RA0 \xD");
fscanf (stdaux,"%e",&an0);
rewind (stdaux);        

Data can also be retrieved in integer format if digital ports are accessed. For example, the following commands send a PA ( read port A ) command to the ADR101 and stores the retrieved data in an integer variable named PORTA;

fprintf (stdaux,"PA \xD"); fscanf (stdaux,"%D",&PORTA); rewind (stdaux);

The following test programs outline the proper syntax for using the commands in simple applications. The first program retrieves the status of analog port 0 and displays the data on the video screen. The second program configures PORT A as output, sets the port to decimal 255, reads back the port status and displays the data on the video screen.

/* PROGRAM EXAMPLE ONE - ANALOG PORT TEST PROGRAM */

#include <stdio.h>        
#include <bios.h>        
#define com1 0        
#define settings (0xE3)         
main( )         
{ /* declare an0 as a floating point number */         
float an0 ;         
/* configure com1 9600 baud, 8 bit words, no parity */         
bioscom (0,settings,com1);         
/* send RA0 command to ADR101 on com1 */         
fprintf(stdaux,"RA0 \xD");         
/* initialize com1 buffer */         
fscanf (stdaux,"%e",&an0);         
/* print data on screen */         
rewind (stdaux);         
/* read data from com1 and store it at address of an0 */         
printf ("ANALOG PORT 0= %e PERCENT \n",an0); }         

 

/* PROGRAM EXAMPLE TWO - DIGITAL PORT TEST PROGRAM */

#include <sdio.h>         
#include <bios.h>         
#define com1 0         
#define settings (0xE3)         
main ( )         
{ /* declare PORTA and DOUT as integer numbers */         
int PORTA,DOUT ;         
/* set DOUT to integer 255 */         
DOUT=255;         
/* configure com1 9600 baud, 8 bit words, no parity */         
bioscom (0,settings,com1);         
/* send CPA00000000 command to ADR101 on com1 */         
fprintf (stdaux,"CPA00000000 \xD");         
/* send MAddd (ddd=DOUT) command to ADR101 on com1 */         
fprintf (stdaux,"MA %d \xD",DOUT );         
/* send PA command to ADR101 on com1 */         
fprintf (stdaux,"PA \xD");         
/* initialize com1 buffer */         
fscanf (stdaux,"%d",&PORTA );         
/* print data on screen */         
rewind (stdaux);         
/* read data from com1 and store at address of PORTA */         
printf ("PORT A is %d DECIMAL \n",PORTA); }         

 Back to Programming Page