ReWrite program from Visual Basic to Igor

Hi everyone,

I try to rewrite or convert the visual basic program (i got it from my friend) in Igor Procedure for my data acquisition.

I don't know if it suitable or not to post this in this forum.

I just want to know your opinion if my Igor procedure is similar with the Visual Basic or not?

This is my Igor Procedure:
#pragma rtGlobals=1     // Use modern global access method.

Function tdcDmaRead() // Public Function tdcDmaRead(ud As Long, addr As Integer, byte_count As Long) in VB (Visual Basic)

Variable IX,IY
Variable DataImage
variable/G  lsb1,lsb2, msb1, msb2
String PSDbuffer
Variable UnitDescriptor,UdPSD,cmd,addr
String RdBuffer= PSDbuffer

UdPSD=UnitDescriptor

tdcprepare(UdPSD, cmd)


for (IX=1;IX<=128;IX+=1)
    for (IY=1;IY<=128;IY+=1)
   
    NI4882 ibrd= {UdPSD,4}
    PSDbuffer = S_value

 lsb1=char2num (Rdbuffer[0])
 lsb2=char2num (Rdbuffer[1])
 msb1=char2num (Rdbuffer[2])
 msb2=char2num (Rdbuffer[3])
  Print  "dalam drawimage: " , lsb2,lsb1, msb2, msb1
  Printf  "dalam drawimage: %s/x" , lsb2,lsb1, msb2, msb1
 Make/O/N=(128,128) rawdata

rawdata=rawdata+(msb1*16777216)+(msb2*65536)+(lsb1*256)+lsb2

 Print  "dalam drawimage" , lsb2,lsb1, msb2, msb1

Print "DataImage",rawdata

    endfor
endfor
End


Function tdcprepare(UdPSD, cmd) //This is a Public Function TdcPrepare(ud As Long, func As Integer, addr As Integer, byte_count As Long)  in VB
Variable cmd
Variable UdPSD
String cmdStr1
String charB,charC,charD,charE,char0
String/G PSDbuffer
Variable response,v,b,c,d,e

print " "
print "Transfer TDc"

cmd=0x05

TdcFunc(UdPSD,cmd)

b=0xFF //cnt.msb = &HFF in VB
c=0xFC//cnt.lsb = &HFC inVB
d=0x00 //adr.msb = 0 in VB
e=0x00 // adr.lsb = addr in VB

charD=num2char (d)

charE=num2char (e)

cmdStr1= charD+charE +"\0"

NI4882 ibwrt={UdPSD,cmdStr1,strlen(cmdStr1)}
print "length UdPSD:", strlen (cmdStr1)


charB=num2char (b)

charC=num2char (c)

cmdStr1=charB+charC +"\0"

NI4882 ibwrt={UdPSD,cmdStr1,strlen(cmdStr1)}
print "length UdPSD:", strlen (cmdStr1)
End



Function TdcFunc(UdPSD,cmd)// This is a Public Function TdcFunc(ud As Long, func As Integer)  in VisualBasic
Variable UdPSD,cmd
String cmdStr1
Variable v
String PSDbuffer
print "UdPSD cmd:", cmd
cmdStr1=Num2Char(cmd)   +"\0"
v=0x00
NI4882 ibeos = {UdPSD, v}
NI4882 ibwrt={UdPSD,cmdStr1, strlen(cmdStr1)}
print "length UdPSD:", strlen (cmdStr1)
NI4882 ibrd={UdPSD,strlen(cmdStr1)}
PSDbuffer=S_value

End


and this the Visual Basic program:

Public Function tdcDmaRead(ud As Long, addr As Integer, byte_count As Long)
Dim rt&, i&, j&, mulai&, selesai&, msb1&, msb2&, lsb1&, lsb2&
Dim recvbuffer As String, dummy As String

rt& = TdcPrepare(ud, DMA_READ, addr, byte_count)
If rt& <> 0 Then
MsgBox "Error while Preparing DMA Read", vbCritical, "SANS Measurement and Control"
tdcDmaRead = -1
Exit Function
End If

'Open "C:\rawdata\hasil.txt" For Output As #1
Open "hasil.txt" For Output As #1
For i& = 0 To 127
For j& = 0 To 127
If i& = 127 And j& = 127 Then
Print #1, i&, j&, rawdata(i&, j&)
Exit For
End If
recvbuffer = Space$(4)
ibrd ud, recvbuffer, 4
If (ibsta And EERR) Or (ibcntl = 0) Then
MsgBox "Error while reading DMA", vbCritical, "SANS Measurement and Control"
tdcDmaRead = -1
Exit Function
End If
lsb2& = Asc(Left(recvbuffer, 1))
lsb1& = Asc(Right(Left(recvbuffer, 2), 1))
msb2& = Asc(Right(Left(recvbuffer, 3), 1))
msb1& = Asc(Right(Left(recvbuffer, 4), 1))
rawdata(j&, 127 - i&) = rawdata(j&, 127 - i&) + (msb1& * 16777216) + (msb2& * 65536) + (lsb1& * 256) + lsb2&

Next j&
Next i&
Close #1
End Function

Public Function TdcPrepare(ud As Long, func As Integer, addr As Integer, byte_count As Long)
Dim rt&, Point_Address As String, Point_Count As String
Dim adr As memaddr, cnt As ByteCnt

adr.msb = 0
adr.lsb = addr

'((128*128)-1)*4=FFFC
cnt.msb = &HFF
cnt.lsb = &HFC

rt& = TdcSendFunc(ud, func)
If rt& <> 0 Then
TdcPrepare = -1
Exit Function
End If

Point_Address = Chr(adr.msb) & Chr(adr.lsb)
ibwrt ud, Point_Address, 2
If (ibsta And EERR) Or (ibcntl = 0) Then
TdcPrepare = -1
Exit Function
End If
Point_Count = Chr(cnt.msb) & Chr(cnt.lsb)
ibwrt ud, Point_Count, 2
If (ibsta And EERR) Or (ibcntl = 0) Then
TdcPrepare = -1
Exit Function
End If

End Function

Public Function TdcFunc(ud As Long, func As Integer)
Dim rt&
DoEvents
rt& = TdcSendFunc(ud, func)
If rt& <> 0 Then
TdcFunc = rt&
Exit Function
End If

rt& = 0
rt& = WrtInreg()
If rt& <> 0 Then
TdcFunc = rt&
Exit Function
End If

rt& = 0
rt& = ReadInreg
If rt& <> 0 Then
TdcFunc = rt&
Exit Function
End If

End Function


Hope any of you can give your opinion(s).
Thank you.
I have not looked at your VB code but here are some comments on your Igor code:

Function tdcDmaRead()
    Variable IX,IY
    Variable DataImage

    // *** I see no reason to make these global
    variable/G  lsb1,lsb2, msb1, msb2

    String PSDbuffer
    Variable UnitDescriptor,UdPSD,cmd,addr
    String RdBuffer= PSDbuffer
 
    // *** You have never assigned any value to UnitDescriptor.
    UdPSD=UnitDescriptor


    // *** cmd is uninitialized
    tdcprepare(UdPSD, cmd)   // *******


    // *** Wrong
    for (IX=1;IX<=128;IX+=1)
        for (IY=1;IY<=128;IY+=1)

    // *** Right - Igor is zero-based, like C
    for (IX=0;IX<128;IX+=1)
        for (IY=0;IY<128;IY+=1)


    // *** This could all be replaced by a call to NIGPIBRead2 to read into a 32-bit integer
    NI4882 ibrd= {UdPSD,4}
    PSDbuffer = S_value
    lsb1=char2num (Rdbuffer[0])
    lsb2=char2num (Rdbuffer[1])
    msb1=char2num (Rdbuffer[2])
    msb2=char2num (Rdbuffer[3])


// *** This should be outside of the loops
Make/O/N=(128,128) rawdata

// *** This makes no sense to me
rawdata=rawdata+(msb1*16777216)+(msb2*65536)+(lsb1*256)+lsb2
// *** You probably want something like:
// rawdata[IX][IY] = ...


At this point I conclude that you need to step back and read about Igor programming as there are too many mistakes and misconceptions to correct in this forum.

I recommend that read the following chapters of the Igor manual:
IV-1 - Working With Commands
IV-2 Programming Overview
IV-3 User-Defined Functions

Hi hrodstein,
Thank you for reply my post.
The value for Unit Descriptor will be assigned when i use ibdev where i not include it in this post.
Did i need to put some value for cmd to initialized the cmd?
I would second Howard's suggestion that you read the Igor Pro manual as noted and perhaps follow the tutorials. To that I would add that, starting from ground zero to write IP code to do what is needed may be less frustrating and ultimately cleaner for later debugging and use than trying to translate verbatim VB code directly to IP.

--
J. J. Weimer
Chemistry / Chemical & Materials Engineering, UAHuntsville
Quote:
Did i need to put some value for cmd to initialized the cmd?


You are passing cmd as a parameter from tdcDmaRead to tdcPrepare. In tdcDmaPrepare, you have this:
cmd = 0x05


If you know that cmd should be 0x05 then there is no need to pass it as a parameter into tdcPrepare.
hrodstein, Thank you for reply.The cmd value is 0x05.. :)
jjweimer..yes it is better for me start from zero. I just use the VB program as my reference because I need to write IP program similar like the VB program..hehe