MIGRACION

28/03/2006 - 23:58 por Magda Koray | Informe spam
Hola a todos, sigo en mi tema de migracion de VB 6.0 a VB.NET.
El siguiente codigo lo migre y aparentemente no hay error al compilar, pero
cuando lo ejecuto, me sale el siguiente error: THE GIVEN SESSION OR OBJECT
REFERENCE IS INVALID.
TENGO VARIAS DUDAS:
1. A que hace hace referencia este error?
2. Como migro el codigo que hace referencia a error_handler?

Adjunto codigo en VB 6.0 y codigo en VB.NET.

Les agradezco mucho si alguien puede darme una luz.

ESTE ES EL CODIGO EN VB.6.0

Option Explicit

Private Sub Main()
' Declare Variables used in the program
Dim status As Long 'VISA function status return code
Dim defrm As Long 'Session to Default Resource Manager
Dim vi As Long 'Session to instrument
Dim strRes As String * 100 'Fixed length string to hold *IDN? Results
Dim x As Integer 'Loop Variable
Dim output As String 'output string variable
Dim ArrayPtr(1) As Long 'Array of Pointers
Dim ResultsArray(8192) As Single 'trace element array of Real,32 values
'For Real,64 data use Double. For Int,32 data use Long
Dim length As Long 'Number of trace elements return from instrument
Dim fnum As Integer 'File Number to used to open file to store data
Dim isOpen As Boolean 'Boolean flag used to keep track of open file

'Set the default number of trace elements to the ResultsArray size
'Note: PSA and ESA currently support up to 8192 trace points
length = 8192

'Set the array of pointers to the addresses of the variables
ArrayPtr(0) = VarPtr(length)
ArrayPtr(1) = VarPtr(ResultsArray(0))

On Error GoTo Error_Handler


'Change the instrument mode to Spectrum Analysis
status = viVPrintf(vi, ":INST:NSEL 1" + Chr$(10), 0)
If (status < 0) Then GoTo VisaErrorHandler

' Set instrument trace data format to 32-bit Real
' Note: For higher precision use 64-bit data, ":FORM REAL,64"
' For faster data transfer for ESA, use ":FORM INT,32"
status = viVPrintf(vi, ":FORM REAL,32" + Chr$(10), 0)
If (status < 0) Then GoTo VisaErrorHandler

'Set Analyzer to single sweep mode
status = viVPrintf(vi, ":INIT:CONT 0" + Chr$(10), 0)
If (status < 0) Then GoTo VisaErrorHandler

'Trigger a sweep and wait for sweep to complete
status = viVPrintf(vi, ":INIT:IMM;*WAI" + Chr$(10), 0)
If (status < 0) Then GoTo VisaErrorHandler

'Query the trace data from the instrument
'Note: Change the "%#zb" to "%#Zb" for Real,64 data
' For Int,32 leave the modifier as "%#zb"
status = viVQueryf(vi, ":TRAC:DATA? TRACE1" + Chr$(10), _
"%#zb", ArrayPtr(0))

'Close the vi session and the resource manager session
Call viClose(vi)
Call viClose(defrm)

'Print number of elements returned
MsgBox ("Number of trace elements returned = " & length)

'Create a string from the ResultsArray to output to a file
For x = 0 To length - 1
output = output & ResultsArray(x) & vbCrLf
Next x

'Print Results to the Screen
MsgBox (output)

'Store the results in a text file
fnum = FreeFile() 'Get the next free file number
Open "bintrace.txt" For Output As #fnum
isOpen = True
Print #fnum, output

' Intentionally flow into Error Handler to close file
Error_Handler:
' Raise the error (if any), but first close the file
If isOpen Then Close #fnum
If Err Then Err.Raise Err.Number, , Err.Description
Exit Sub

VisaErrorHandler:
Dim strVisaErr As String * 200
Call viStatusDesc(defrm, status, strVisaErr)
MsgBox "*** Error : " & strVisaErr, vbExclamation, "VISA Error Message"
Exit Sub
End Sub

ESTE ES EL CODIGO EN VB.NET

Dim status As Integer
Dim defrm As Integer
Dim vi As Integer
Dim strRes As New VB6.FixedLengthString(100)
Dim x As Short
Dim output As System.String
Dim ArrayPtr(1) As Integer
Dim ResultsArray(8192) As System.Single
Dim length As Integer
Dim fnum As Short
Dim isopen As System.Boolean

ArrayPtr(0) = length
ArrayPtr(1) = ResultsArray(0)

On Error GoTo ErrorHandler

status = viVPrintf(vi, ":INST:NSEL 1" & Chr(10), 0)
If (status < 0) Then GoTo VisaErrorHandler


status = viVPrintf(vi, ":FORM REAL,64" & Chr(10), 0)
If (status < 0) Then GoTo VisaErrorHandler

status = viVPrintf(vi, ":INIT:CONT 0" & Chr(10), 0)
If (status < 0) Then GoTo VisaErrorHandler

status = viVPrintf(vi, ":INIT:IMM;*WAI" & Chr(10), 0)
If (status < 0) Then GoTo VisaErrorHandler

status = viVQueryf(vi, ":TRAC:DATA? TRACE1" & Chr(10), "%#Zb",
ArrayPtr(0))

Call viClose(vi)
Call viClose(defrm)

MsgBox("Numero de elementos de la traza = " & length,
MsgBoxStyle.OKOnly)

ErrorHandler:
' Display the error message
MsgBox("*** Error : " & ErrorToString(), MsgBoxStyle.Exclamation)
Exit Sub

VisaErrorHandler:
Dim strVisaErr As New VB6.FixedLengthString(200)
Call viStatusDesc(defrm, status, strVisaErr.Value)
MsgBox("*** Error : " & strVisaErr.Value, MsgBoxStyle.Exclamation,
"VISA Error Message")
Exit Sub

Preguntas similare

Leer las respuestas

#1 Lucas
29/03/2006 - 04:18 | Informe spam
Los errores los deberías capturar con Try.. Catch...

Try
código
Catch ex As Exception
código en caso de error
End Try

Exception es la excepción general, hay excepciones particulares, busca una
que se adecue a tus necesidades.

Espero que haya podido ayudarte en algo.
email Siga el debate Respuesta Responder a este mensaje
Ads by Google
Help Hacer una preguntaRespuesta Tengo una respuesta
Search Busqueda sugerida