#195375 - 2009-08-10 05:53 PM
Trouble with converting VB to KiX
|
Viggen
Starting to like KiXtart
Registered: 2002-03-22
Posts: 110
|
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: 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:
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
|
Top
|
|
|
|
#195546 - 2009-08-24 03:20 PM
Re: Trouble with converting VB to KiX
[Re: Viggen]
|
Viggen
Starting to like KiXtart
Registered: 2002-03-22
Posts: 110
|
Solved! 
I finally gave up and sat down with VS. After six hours of swearing I had a .exe that connects to the reader, puts the barcodes in a text-file, and then erases the info in the reader.
So now I can drop the whole DynaWrap-part and just use shell to run the exe, and then read the text-file.
/Viggen
|
Top
|
|
|
|
#195551 - 2009-08-24 04:53 PM
Re: Trouble with converting VB to KiX
[Re: Arend_]
|
Arend_
MM club member
   
Registered: 2005-01-17
Posts: 1896
Loc: Hilversum, The Netherlands
|
What happens if you run this script:
Dim $dyn
$dyn = CreateObject("DynamicWrapper")
$=$dyn.Register("C:\Windows\System\Csp2.dll","csp2Init","i=l","f=s","r=l")
$=$dyn.Register("C:\Windows\System\Csp2.dll","csp2ReadData","f=s","r=l")
$=$dyn.Register("C:\Windows\System\Csp2.dll","csp2ClearData","f=s","r=l")
$=$dyn.Register("C:\Windows\System\Csp2.dll","csp2PowerDown","f=s","r=l")
$=$dyn.Register("C:\Windows\System\Csp2.dll","csp2GetASCIIMode","f=s","r=l")
$=$dyn.Register("C:\Windows\System\Csp2.dll","csp2GetPacket","i=dll","f=s","r=l")
$=$dyn.Register("C:\Windows\System\Csp2.dll","csp2GetCodeType","i=lsl","f=s","r=l")
$=$dyn.Register("C:\Windows\System\Csp2.dll","csp2TimeStamp2Str","i=dsl","f=s","r=l")
ReadBarCode
Function ReadBarCode
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 = $dyn.csp2ReadData
If $nRC > 0
$nNumOfBarcodes = $nRC
? "Reading " + CStr($nRC) + " Barcodes at " + @Time
;Check to see that we are in ascii mode...
If $dyn.csp2GetASCIIMode = "PARAM_ON"
? "ASCII Mode ON"
For $i=0 To $nNumOfBarcodes-1
$nBytesRead = $dyn.csp2GetPacket($arrbyteBarcode[0], $i, 100)
If $nBytesRead > 0
;Display the Barcode type
$nRC = $dyn.csp2GetCodeType($arrbyteBarcode[1], $bstrTmp, Len($bstrTmp))
? $bstrTmp
$bstrBarcode = " "
; display the barcode is ascii
; skip the length, type, .... timestamp
For $j=2 To $nBytesRead-5
$bstrBarcode = $bstrBarcode + Chr($arrbyteBarcode[$j])
Next
;Display the timestamp
$nRC = $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
EndFunction
|
Top
|
|
|
|
#196083 - 2009-09-25 07:41 PM
Re: Trouble with converting VB to KiX
[Re: Arend_]
|
Viggen
Starting to like KiXtart
Registered: 2002-03-22
Posts: 110
|
Hi!
Sorry for taking so long to answer this apronk
Before I post the "solution", I'll answer the other part first.
After changing a few lines in the script I got it to talk to the reader. (had to comment out the "dim $dyn"-line and run the init part)
When I hit the "$nBytesRead = $dyn.csp2GetPacket($arrbyteBarcode[0], $i, 100)"-line I get "Fatal exception occurred".
On to the other code then 
Here goes (please be warned... this is not pretty) 
Option Strict Off
Option Explicit On
Imports System
Imports System.IO
Imports Microsoft.VisualBasic
Module ReadBarCodes
Sub Main()
If My.Application.CommandLineArgs.Count > 0 Then
'MsgBox(My.Application.CommandLineArgs(0))
OpenCom(My.Application.CommandLineArgs(0))
'Environment.ExitCode = 0
If Environment.ExitCode = 0 Then
ReadBarCode()
Else
Environment.ExitCode = 1
End If
Else
MsgBox("Inga argument angivna")
Environment.ExitCode = 1
End If
End Sub
Sub OpenCom(ByVal port)
Dim dwRC As Long
Dim dwComPort As Long
dwComPort = port
'Open the com port
dwRC = csp2Init(dwComPort)
If dwRC <> STATUS_OK Then
MsgBox("Kunde inte ansluta till läsaren. Felkod: (Com - " & CStr(dwComPort + 1) & ")")
Environment.ExitCode = 1
Else
Environment.ExitCode = 0
End If
End Sub
Sub ReadBarCode()
Dim nRC As Integer
Dim nNumOfBarcodes As Integer
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim l As Integer
Dim CDir As String
Dim arrbyteBarcode(99) As Byte '100 elements
Dim nBytesRead As Integer
Dim bstrBarcode As String
Dim bstrTmp As String
CDir = CurDir()
'Determine if we can read the data
nRC = csp2ReadData
If nRC > 0 Then
Dim LogFile As String = CDir & "\bc"
If File.Exists(LogFile) Then
File.Delete(LogFile)
End If
Dim objWriter As New StreamWriter(LogFile)
'cs1504 has barcodes!
nNumOfBarcodes = nRC
objWriter.WriteLine(nNumOfBarcodes)
Dim aryText(nNumOfBarcodes) As String
'k = 1
k = 0
'aryText(0) = "Reading " & CStr(nRC) & " Barcodes at " & TimeOfDay
'MsgBox("Reading " & CStr(nRC) & " Barcodes at " & TimeOfDay)
'Check to see that we are in ascii mode...
If csp2GetASCIIMode = PARAM_ON Then
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))
aryText(k) = bstrBarcode & " " & Left(bstrTmp, 20)
k = k + 1
End If
Next i
For l = 0 To (k - 1)
objWriter.WriteLine(aryText(l))
Next l
Else
'Add binary mode packets handling here..
MsgBox("Binary Mode är PÅ")
End If
objWriter.Close()
ClearAndPowerDown()
Environment.ExitCode = 0
Else
MsgBox("Det fanns inga streckoder i läsaren")
Environment.ExitCode = 1
End If
End Sub
Sub ClearAndPowerDown()
Dim nRC2 As Integer
nRC2 = csp2ClearData()
If nRC2 <> STATUS_OK Then
End If
End Sub
End Module
/Viggen
|
Top
|
|
|
|
Moderator: Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart
|
0 registered
and 625 anonymous users online.
|
|
|