Hi all!

I have had 'some' luck in converting a vb script, for a barcode scanner, to KiX, using DynaWrap, and I can talk to the scanner and I get how many barcodes there are in memory, but thats it...

The thread about the DynaWrap-stuff can be found here...


Here's the vb-code I have problems with:
 Code:
Public Sub ReadBarCode_Click()
    Dim nRC As Long
    Dim nNumOfBarcodes As Long
    Dim i As Long
    Dim j As Long
        
    Dim arrbyteBarcode(99) As Byte '100 elements
    Dim nBytesRead As Long
    Dim bstrBarcode As String
    Dim bstrTmp As String * 50


    'Determine if we can read the data
    nRC = csp2ReadData
        
    If nRC > 0 Then

        nNumOfBarcodes = nRC

        DisplayInBCWindow "Reading " & CStr(nRC) & _
            " Barcodes at " & Time

        'Check to see that we are in ascii mode...
        If csp2GetASCIIMode = PARAM_ON Then

            DisplayInBCWindow "ASCII Mode ON"

            For i = 0 To (nNumOfBarcodes - 1)
                nBytesRead = csp2GetPacket(arrbyteBarcode(0), i, 100)

                If nBytesRead > 0 Then
                    'bstrBarcode = "Rcvd: "

                    'Display the Barcode type
                    nRC = csp2GetCodeType(arrbyteBarcode(1), bstrTmp, Len(bstrTmp))

                    DisplayInBCWindow bstrTmp
                    bstrBarcode = " "

                    ' display the barcode is ascii
                    ' skip the length, type, .... timestamp
                    For j = 2 To (nBytesRead - 5)
                        bstrBarcode = bstrBarcode & Chr(arrbyteBarcode(j))
                        'DisplayInBCWindow Chr(arrbyteBarcode(j))
                    Next j

                    'Display the timestamp
                    nRC = csp2TimeStamp2Str(arrbyteBarcode(nBytesRead - 4), bstrTmp, Len(bstrTmp))

                    DisplayInBCWindow bstrBarcode & " " & Left(bstrTmp, 20)

                End If
            Next i
        Else
            'Add binary mode packets handling here..
            DisplayInBCWindow "Binary Mode ON"
        End If
    Else
        DisplayInBCWindow "No Barcodes to Read."
    End If
End Sub



And here's what I have come up with:

 Code:
Break off

Dim $i, $j, $arrbyteBarcode, $bstrTmp, $bstrBarcode

$Port = 15

For $x = 0 to 99
  $Test = "" + $Test + $cnt + ","
  $cnt = $cnt + 1
Next
$Test = Left($Test,Len($Test)-1)

$Test = Split($Test,",")

$arrbyteBarcode = ByteArray($Test)

$dyn = CreateObject("DynamicWrapper")

; ********************************************************************
; DynaWrap Reference
; ********************************************************************
/*
{'a', sizeof(IDispatch*),    VT_DISPATCH}, // a   IDispatch*
{'c', sizeof(unsigned char), VT_I4},       // c   signed char  
{'d', sizeof(double),        VT_R8},       // d   8 byte real 
{'f', sizeof(float),         VT_R4},       // f   4 byte real 
{'k', sizeof(IUnknown*),     VT_UNKNOWN},  // k   IUnknown* 
{'h', sizeof(long),          VT_I4},       // h   HANDLE 
{'l', sizeof(long),          VT_I4},       // l   long 
{'p', sizeof(void*),         VT_PTR},      // p   pointer 
{'s', sizeof(BSTR),          VT_LPSTR},    // s   string 
{'t', sizeof(short),         VT_I2},       // t   short 
{'u', sizeof(UINT),          VT_UINT},     // u   unsigned int 
{'w', sizeof(BSTR),          VT_LPWSTR},   // w   wide string
*/
; ********************************************************************


$nul = $dyn.Register("C:\Windows\System\Csp2.dll", "csp2Init", "i=l", "f=s", "r=l")
; (Declare Function csp2Init Lib "csp2.dll" (ByVal nComPort As Long) As Long)

$nul = $dyn.Register("C:\Windows\System\Csp2.dll", "csp2ReadData", "f=s", "r=l")
; (Declare Function csp2ReadData Lib "csp2.dll" () As Long)

$nul = $dyn.Register("C:\Windows\System\Csp2.dll", "csp2ClearData", "f=s", "r=l")
; (Declare Function csp2ClearData Lib "csp2.dll" () As Long)

$nul = $dyn.Register("C:\Windows\System\Csp2.dll", "csp2PowerDown", "f=s", "r=l")
; (Declare Function csp2PowerDown Lib "csp2.dll" () As Long)

$nul = $dyn.Register("C:\Windows\System\Csp2.dll", "csp2GetASCIIMode", "f=s", "r=l")
; (Declare Function csp2GetASCIIMode Lib "csp2.dll" () As Long)

$nul = $dyn.Register("C:\Windows\System\Csp2.dll", "csp2GetPacket", "i=dll", "f=s", "r=l")
; (Declare Function csp2GetPacket Lib "csp2.dll" (stPacketData As Byte, ByVal lgBarcodeNumber As Long, ByVal maxLength As Long) As Long)

$nul = $dyn.Register("C:\Windows\System\Csp2.dll", "csp2GetCodeType", "i=lsl", "f=s", "r=l")
; (Declare Function csp2GetCodeType Lib "csp2.dll" (ByVal CodeID As Long, ByVal CodeType As String, ByVal nMaxLength As Long) As Long)

$nul = $dyn.Register("C:\Windows\System\Csp2.dll", "csp2TimeStamp2Str", "i=dsl", "f=s", "r=l")
; (Declare Function csp2TimeStamp2Str Lib "csp2.dll" (Stamp As Byte, ByVal value As String, ByVal nMaxLength As Long) As Long)


$dOpen = $dyn.csp2Init($Port)
? @error

$dRD = $dyn.csp2ReadData()
? $dRD

$dASCII = $dyn.csp2GetASCIIMode

If $dRD > 0
  $nNumOfBarcodes = $dRD

  ?? "Reading " + CStr($dRD) + " Barcodes at " + @Time

  If $dASCII
    ? "ASCII Mode ON"

debug on
? Vartype($arrbyteBarcode)
; Displays: 8209 = (Byte[] Array right?)

    For $i = 0 To ($nNumOfBarcodes - 1)
      $nBytesRead = $dyn.csp2GetPacket($arrbyteBarcode[0],$i,100) ; $arrbyteBarcode[0] - can't be right... I would use the same element each time?

      If $nBytesRead > 0
        $k = $i + 1
        $dRD = $dyn.csp2GetCodeType($arrbyteBarcode[1],$bstrTmp,Len($bstrTmp)); $arrbyteBarcode[1] - can't be right... I would use the same element each time?
 
        ?? $bstrTmp
        $bstrBarcode = " "

        For $j = 2 To ($nBytesRead - 5)
          $bstrBarcode = $bstrBarcode + Chr($arrbyteBarcode[$j])
          ?? Chr(arrbyteBarcode[$j])
        Next

        $dRD = $dyn.csp2TimeStamp2Str($arrbyteBarcode($nBytesRead - 4),$bstrTmp,Len($bstrTmp))

        ?? $bstrBarcode + " " + Left($bstrTmp,20)

      Endif
    Next
  Else
    ;Add binary mode packets handling here..
    ?? "Binary Mode ON"
  Endif
Else
  ?? "No Barcodes to Read."
Endif


Exit 0


; ******************************************************
Function ByteArray($array)
  Dim $stream, $i

  $stream = CreateObject("ADODB.Stream")
  If @error
    Exit @error
  EndIf

  $stream.Type = 2
  $stream.CharSet = "windows-1252"
  $stream.Open

  For $i=0 to Ubound($array)
    $stream.WriteText(Chr(Val("&"+$array[$i])))
  Next

  $stream.Position = 0
  $stream.Type = 1
  $ByteArray = $stream.Read
  $stream.Close
Endfunction
; ******************************************************



I get to the 'debug'-line, and the I'm stuck...
I know that $arrbyteBarcode is a byte array, but the VB uses 'arrbyteBarcode(0)' and I can't use $arrbyteBarcode[0], or can I?
(but then the whole point of using an array would have no meaning, since I would end up with using the same element all the time)

And IF I would get past that part, next problem would probably be $bstrTmp... in the VB it is declared like this: 'Dim bstrTmp As String * 50'
What's the '* 50' about?

any help / pointers is much appreciated.

/Viggen