Writing Data in Binary Format

Other topics about Sample Programs

Overview

The sample program demonstrates to Write Formatted Data Arrays in Using the Binary Transfer Format.

See Entering Data into a Trace for this programming.

Sample Program in Excel VBA using VISA

Private Sub Write_BINARY_Click()

  '*** Before this program is executed, create the channel 2,

  '*** and match the number of points and data format

  '*** of channel 2 to the transmitted data.

  Dim defrm As Long

  Dim Age506x As Long

  Dim Poin As Integer

  Dim Buf As String * 16

  Dim WriteData() As Double

  Dim WrtPtr As Long

  Dim WrtFmt As String

 

  Call viOpenDefaultRM(defrm)

  '*** Open the instrument.

  Call viOpen(defrm, "GPIB0::17::INSTR", 0, 0, Age506x)

  Call viSetAttribute(Age506x, VI_ATTR_TMO_VALUE, 10000)

 

  '*** Abort sweeping of channel2/trace1.

  Call viVPrintf(Age506x, ":CALC2:PAR1:SEL" + vbLf, 0)

  Call viVPrintf(Age506x, ":INIT2:CONT OFF" + vbLf, 0)

  Call viVPrintf(Age506x, ":ABOR" + vbLf, 0)

  '*** Set the binary format.

  Call viVPrintf(Age506x, ":FORM:DATA REAL" + vbLf, 0)

  '*** Get num of point, and re-define array size of data.

  Call viVPrintf(Age506x, ":SENS1:SWE:POIN?" + vbLf, 0)

  Call viVScanf(Age506x, "%t", Buf)

  Poin = CInt(Buf)

  ReDim WriteData(Poin * 2 - 1)

  WrtPtr = VarPtr(WriteData(0))

 

  '*** Set data for array variable.

  For i = 1 To Poin

    WriteData(i * 2 - 2) = ActiveSheet.Cells(i + 5, 4).Value

    WriteData(i * 2 - 1) = ActiveSheet.Cells(i + 5, 5).Value

  Next i

  '*** set Format code & Modifier.

  WrtFmt = "%#" & Trim(CStr(Poin * 2)) & "Zb"

  '*** send data for E506x.

  Call viVPrintf(Age506x, ":CALC2:DATA:FDAT ", 0)

  Call viVPrintf(Age506x, WrtFmt + vbLf, WrtPtr)

 

  '*** end procedure

  Call viVPrintf(Age506x, ":FORM:DATA ASC" + vbLf, 0)

  Call viClose(Age506x)

  Call viClose(defrm)

 

End Sub

 

Sample Program in Excel VBA using VISA-COM

Private Sub Write_BINARY_Click()

  '*** Before this program executes, create the channel 2,

  '*** and match the number of points and data format

  '*** of channel 2 to the transmitted data.

  Dim WriteData() As Double

  Dim Poin As Integer

  '*** The variables of the resource manager and the instrument I/O are declared.

  Dim ioMgr As VisaComLib.ResourceManager

  Dim Age506x As VisaComLib.FormattedIO488

  '*** The memory area of the resource manager and the instrument I/O are acquired.

  Set ioMgr = New VisaComLib.ResourceManager

  Set Age506x = New VisaComLib.FormattedIO488

 

  '*** Open the instrument.

  Set Age506x.IO = ioMgr.Open("GPIB0::17::INSTR")

  Age506x.IO.Timeout = 10000

 

  '*** Abort sweeping of channel2/trace1.

  Age506x.WriteString ":CALC2:PAR1:SEL", True

  Age506x.WriteString ":INIT2:CONT OFF", True

  Age506x.WriteString ":ABOR", True

  '*** Set the binary format.

  Age506x.WriteString ":FORM:DATA REAL", True

 

  '*** Get num of point.

  Age506x.WriteString ":SENS1:SWE:POIN?", True

  Poin = Age506x.ReadNumber

  ReDim WriteData(Poin * 2 - 1) As Double

  '*** Set data for array variable, and send data for E506x.

  For i = 1 To Poin

    WriteData(i * 2 - 2) = ActiveSheet.Cells(i + 5, 4).Value

    WriteData(i * 2 - 1) = ActiveSheet.Cells(i + 5, 5).Value

  Next i

  Age506x.WriteIEEEBlock ":CALC2:DATA:FDAT ", WriteData, True

 

  '*** end procedure

  Age506x.WriteString ":FORM:DATA ASC", True

  Age506x.IO.Close

 

End Sub

 

Sample Program in HT Basic (write_b.htb)

10 REAL Freq,Fdata(1:1601,1:2)

20 DIM File$[300],Header$[10]

30 INTEGER Nop

40 !

50 ASSIGN @Agte506x TO 717

60 ASSIGN @Binary TO 717;FORMAT OFF

70 CALL Inp_file_name(File$)

80 !

90 OUTPUT @Agte506x;":CALC1:PAR1:SEL"

100 OUTPUT @Agte506x;":INIT1:CONT OFF"

110 OUTPUT @Agte506x;":ABOR"

120 !

130 OUTPUT @Agte506x;":SENS1:SWE:POIN?"

140 ENTER @Agte506x;Nop

150 REDIM Fdata(1:Nop,1:2)

160 !

170 ON ERROR GOTO File_error

180 ASSIGN @File TO File$

190 ENTER @File USING "K";Buff$

200 ENTER @File USING "K";Buff$

210 ENTER @File USING "K";Buff$

220 FOR I=1 TO Nop

230 ENTER @File USING "19D,2X,19D,2X,19D";Freq,Fdata(I,1),Fdata (I,2)

240 NEXT I

250 ASSIGN @File TO *

260 OFF ERROR

270 !

280 OUTPUT @Agte506x;":FORM:DATA REAL"

290 Header$="#6"&IVAL$(8*2*Nop,10)

300 OUTPUT @Agte506x;":CALC1:DATA:FDAT ";Header$;

310 OUTPUT @Binary;Fdata(*),END

320 GOTO Prog_end

330 !

340 File_error: OFF ERROR

350 PRINT "############ ERROR ############"

360 PRINT File$&" is NOT exist."

370 PRINT " or"

380 PRINT File$&" has UNSUITABLE data."

390 !

400 Prog_end: END

410 !=============================================

420 ! File Name Input Function

430 !=============================================

440 SUB Inp_file_name(Inp_name$)

450 DIM Inp_char$[9]

460 ON ERROR GOTO Inp_start

470 Inp_start: !

480 PRINT "Input File Name!"

490 INPUT "Name?",Inp_name$

500 PRINT "Input Name: "&Inp_name$

510 INPUT "OK? [Y/N]",Inp_char$

520 IF UPC$(Inp_char$)<>"Y" THEN Inp_start

530 OFF ERROR

540 SUBEND

Description

Lines 50 to 60

Assigns a GPIB address to the I/O pass.

Line 70

Passes control to a subprogram named Inp_file_name, which lets the user input a file name, and then stores the returned file name into the File$ variable.

Lines 90 to 110

These lines set channel 1's active trace to trace 1 and hold the sweep.

Lines 130 to 140

These lines retrieve the number of points in channel 1 and stores that number into the Nop variable.

Line 150

Resizes the Fdata array based on the value of the Nop variable (the number of points).

Line 170

This line points to the statement block to be executed if an error occurs in retrieving data from the file (for example, if no file matches File$).

Lines 180 to 260

These lines retrieve the formatted data from the file identified by File$, and store the data into the Fdata array.

Line 280

Sets the data transfer format to binary.

Line 290

Creates the data header and stores it into the Header$ variable.

Line 300

Sends the command that writes data into the formatted data array for the active trace (trace 1) in channel 1, following it with the data header (Header$).

Line 310

Sends the data itself (Fdata), following it with a message terminator.

Lines 340 to 380

This statement block is executed if an error occurs in retrieving data from the file.

The Inp_file_name subprogram in lines 440 to 540, which is used to enter a save filename, is described below.

Line 460

Allows the user to return to the entry start line and re-enter the data if an error (such as an invalid entry) occurs while entering the target file name.

Lines 480 to 490

These lines prompt the user to enter the target file name. The program does not continue till the user actually enters the file name.

Lines 500 to 510

These lines display the entered file name and waits for a confirmation entry (y/n key).

Line 520

Returns to the entry start line if the key the user pressed in line 510 is not the y key.