Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Трахтенберг М.Б. / Методички / Devices / Agilent-34401A-original.pdf
Скачиваний:
101
Добавлен:
29.06.2016
Размер:
2.37 Mб
Скачать

Chapter 6 Application Programs

RS-232 Operation Using QuickBASIC

RS-232 Operation Using QuickBASIC

The following example shows how to send command instructions and receive command responses over the RS-232 interface using QuickBASIC.

RS-232 Operation Using QuickBASIC

CLS

LOCATE 1, 1

DIM cmd$(100), resp$(1000)

Set up serial port for 9600 baud, even parity, 7 bits;

Ignore Request to Send and Carrier Detect; Send line feed,

enable parity check, reserve 1000 bytes for input buffer

OPEN "com1:9600,e,7,2,rs,cd,lf,pe" FOR RANDOM AS #1 LEN = 1000

Put the multimeter into the remote operation mode

PRINT #1, ":SYST:REM"

Query the multimeter’s id string

PRINT #1, "*IDN?" LINE INPUT #1, resp$

PRINT "*IDN? returned: ", resp$

Ask what revision of SCPI the multimeter conforms to PRINT #1, ":SYST:VERS?"

LINE INPUT #1, resp$

PRINT ":SYST:VERS? returned: ", resp$

Send a message to the multimeter’s display, and generate a beep PRINT #1, ":SYST:BEEP;:DISP:TEXT ’34401A’"

Configure the multimeter for dc voltage readings,

10 V range, 0.1 V resolution, 4 readings

PRINT #1, ":CONF:VOLT:DC 10,0.1;:SAMP:COUN 4" ’ Trigger the readings, and fetch the results PRINT #1, ":READ?"

LINE INPUT #1, resp$

PRINT ":READ? returned: ", resp$ END

192

Chapter 6 Application Programs

RS-232 Operation Using Turbo C

RS-232 Operation Using Turbo C

The following example shows how to program an AT personal computer for interrupt-driven COM port communications. SCPI commands can be sent to the Agilent 34401A and responses received for commands that query information. The following program is written in Turbo C and can be easily modified for use with Microsoft Quick C.

RS-232 Operation Using Turbo C

#include <bios.h>

 

 

 

 

#include <stdio.h>

 

 

 

 

#include <string.h>

 

 

 

 

#include <dos.h>

 

 

 

 

#include <conio.h>

 

 

 

 

#define EVEN_7 (0x18 | 0x02 | 0x04)

/* Even parity, 7 data, 2 stop */

 

 

#define ODD_7

 

(0x08 | 0x02 | 0x04)

/* Odd parity, 7 data, 2 stop */

 

 

#define NONE_8 (0x00 | 0x03 | 0x04)

/* None parity, 8 data, 2 stop */

 

 

#define BAUD300

0x40

 

 

 

 

#define BAUD600

0x60

 

 

 

 

#define BAUD1200 0x80

 

 

 

 

#define BAUD2400 0xA0

 

 

 

 

#define BAUD4800 0xC0

 

 

 

 

#define BAUD9600 0xE0

 

 

 

 

/* 8250 UART Registers */

 

 

 

#define COM

0x3F8

/* COM1 base port address */

 

 

#define THR

COM+0

/* LCR bit 7 = 0 */

 

 

6

#define RDR

COM+0

/* LCR bit 7 = 0 */

 

 

 

 

 

#define IER

COM+1

/* LCR bit 7 = 0 */

 

 

 

#define IIR

COM+2

/* The rest are don’t care for bit 7 */

 

 

#define LCR

COM+3

 

 

 

 

#define MCR

COM+4

 

 

 

 

#define LSR

COM+5

 

 

 

 

#define MSR

COM+6

 

 

 

 

Continued on next page >

 

 

 

 

 

 

 

 

 

 

Microsoft is a U.S. registered trademark of Microsoft Corporation.

193

Chapter 6 Application Programs

RS-232 Operation Using Turbo C

RS-232 Operation Using Turbo C (continued)

#define IRQ4_int

0xC

/* IRQ4 interrupt vector number */

#define IRQ4_enab

0xEF

/* IRQ4 interrupt controller enable mask */

#define INT_controller

0x20

/*

8259 Interrupt controller address */

#define End_of_interrupt

0x20

/*

Non-specific end of interrupt command */

void interrupt int_char_in(void); void send_ctlc(void);

#define INT_BUF_size 9000

char int_buf[INT_BUF_size], *int_buf_in = int_buf, *int_buf_out = int_buf; unsigned int int_buf_count = 0;

unsigned char int_buf_ovfl = 0;

int main(int argc, char *argv[])

{

void interrupt (*oldvect)(); char command[80], c;

int i;

oldvect = getvect(IRQ4_int);

/* Save old interrupt vector */

setvect(IRQ4_int,int_char_in);

/* Set up new interrupt handler */

bioscom(0,BAUD9600 | EVEN_7,0);

/* Initialize settings for COM1 */

outportb(MCR,0x9);

/* Enable IRQ buffer, DTR = 1 */

outportb(IER,0x1);

/* Enable UART data receive interrupt */

/* Enable IRQ4 in 8259 interrupt controller register */ outportb(INT_controller+1,inportb(INT_controller+1) & IRQ4_enab);

do { if(int_buf_ovfl) {

printf("\nBuffer Overflow!!!\n\n"); int_buf_in = int_buf_out = int_buf; int_buf_count = int_buf_ovfl = 0;

}

Continued on next page >

194

Chapter 6 Application Programs

RS-232 Operation Using Turbo C

RS-232 Operation Using Turbo C (continued)

printf("\nEnter command string:\n");

 

gets(command); strcat(command,"\n");

/* SCPI requires line feed */

if(command[0] == 0x19) send_ctlc();

/* If ^Y then send ^C */

else if(command[0] != ’q’) {

 

for(i=0; i<strlen(command); i++) {

 

/* Wait for DSR and transmitter hold register empty */ while(!(inportb(LSR) & inportb(MSR) & 0x20)) ;

outportb(THR,command[i]);

/* Send character */

}

 

}

 

if(strpbrk(command,"?")) {

/* If query then get response */

c = 0;

 

do {

 

while(int_buf_count && !kbhit()) {

 

putch(c = *int_buf_out++); int_buf_count--;

if(int_buf_out >= int_buf + INT_BUF_size) int_buf_out = int_buf;

}

if(kbhit())

{

if(getch()

== 0x19) send_ctlc(); /* if ^Y then send ^C */

c = 0xa;

/* Terminate loop */

}

 

}

while(c != 0xa);

}

/* End if */

 

}

 

 

while(command[0] != ’q’);

/* ’q’ to quit program */

outportb(IER,inportb(IER) & 0xfe);

/* Disable UART interrupt */

6

 

outportb(MCR,0x1);

 

/* Disable IRQ buffer, DTR = 1 */

 

/* Disable IRQ4 in 8259 interrupt controller register */

 

outportb(INT_controller+1,inportb(INT_controller+1) | ~IRQ4_enab);

 

setvect(IRQ4_int,oldvect);

/* Restore old interrupt vector */

 

return(0);

 

 

 

}

 

 

 

Continued on next page >

195

Chapter 6 Application Programs

RS-232 Operation Using Turbo C

RS-232 Operation Using Turbo C (continued)

void interrupt int_char_in(void)

 

{

 

enable();

/* Enable hardware interrupts */

if(int_buf_count < INT_BUF_size) {

 

*int_buf_in++ = inportb(RDR);

/* Read byte from UART */

int_buf_count++;

 

if(int_buf_in >= int_buf + INT_BUF_size) int_buf_in = int_buf;

int_buf_ovfl = 0;

 

}

 

else {

 

inportb(RDR);

/* Clear UART interrupt */

int_buf_ovfl = 1;

 

}

 

outportb(INT_controller,End_of_interrupt);

/* Non-specific EOI */

}

 

void send_ctlc(void)

{

outportb(MCR,0x8);

delay(10); while(!(inportb(LSR) & 0x20)) ; outportb(THR,0x3); while(!(inportb(LSR) & 0x40)) ;

int_buf_in = int_buf_out = int_buf; int_buf_count = int_buf_ovfl = 0; delay(20);

outportb(MCR,0x9);

}

/* De-assert DTR */

/* Wait 10 mS for stray characters */ /* Wait on transmitter register */ /* Send ^C */

/* Wait for ^C to be sent */ /* Clear int_char_in buffer */

/* 20mS for 34401 to clean up */ /* Assert DTR */

196

7

7

Measurement

Tutorial

Соседние файлы в папке Devices