2 - Introduction to Programming
16
RS-232 Programming Example
The following program illustrates how to program the ac source using RS-232 to set the output voltage
and frequency and to read back the model number and output voltage. The program was written to run on
any controller using Microsoft QBasic.
NOTE: The ac source must be configured for RS232 and the same baud rate and parity as the
controller.
‘ Program to write and read via RS232
‘ Configure serial port for:
‘ 9600 baud
‘ 7 bit data
‘ 2 stop bits
‘ Ignore request to send
‘ Ignore carrier detect
‘ Even parity ‘ Needed with Vectra basic, ignored with QBasic
‘ Send line feed
‘ Reserve 1000 character buffer for serial I/O
‘
DECLARE FUNCTION gets$ () ‘ Function to read string from ac source
CLS ‘ Clears screen
LOCATE 1, 1 ‘ Position cursor at top left
‘ Configure Com1 Port
OPEN “com1:9600,e,7,2,rs,cd,pe,lf” FOR RANDOM AS #1 LEN = 1000
PRINT #1, “*RST” ‘ Resets the ac source
PRINT #1, “VOLT 60” ‘ Set voltage to 60 volts
PRINT #1, “FREQ 50” ‘ Set frequency to 50 hertz
PRINT #1, “OUTPUT ON” ‘ Turn on the output
PRINT #1, “*IDN?” ‘ Query the ac source identification string
PRINT gets$ ‘ Go to gets$ Function and print data returned
PRINT #1, MEAS”VOLT?”; volt ‘ Query the ac source voltage
Volt = VAL (gets$) ‘ Convert gets$ string to a value
PRINT gets$ ‘ Print the value of the voltage
END ‘ End of main program
FUNCTION gets$ ‘ Get a new line feed terminated string from device #1
C$ = “” ‘ Set C$ to null
WHILE c$ <> CHR$ (10) ‘ Set loop to stop at Line Feed
C$ = INPUT$ (1, #1) ‘ Read 1 bit into file #1
Resp$ = resp$ + c$ ‘ Concatenate bit with previous bits
WEND ‘ End of WHILE loop
gets$ = resp$ ‘ Assign response to gets$
END FUNCTION
RS-232 Troubleshooting
If you are having trouble communicating over the RS-232 interface, check the following:
♦
The computer and the ac source must be configured for the same baud rate, parity, and number
of data bits. Note that the ac source is configured for 1 start bit and 2 stop bits (these values are
fixed).
♦ The correct interface cables or adaptors must be used, as described under "RS-232 Connector" in
the User’s Guide. Note that even if the cable has the proper connectors for your system, the
internal wiring may be incorrect.
♦ The interface cable must be connected to the correct serial port on your computer (COM1, COM2,
etc.).