谁能帮我把这段VB6程序转成VB.NET程序,高分求!我通过VS自动转换后程序运行不下去
原VB6代码:
1.这是执行部分的按钮事件
Private Sub eppWrite1_Click()
Dim mLen As Long
Dim buffer As arrRBuffer
mLen = HexToBcd(eppLen1.Text)
If (mLen <= 0 Or Len(eppData1.Text) = 0) Then
MsgBox "请输入写数据和长度! ", vbExclamation, "CH341 "
Exit Sub
End If
If (mLen > Len(eppData1.Text) \ 2) Then
mLen = Len(eppData1.Text) \ 2
End If
Call mStrtoVal(eppData1.Text, buffer, mLen) '将输入的十六进制格式字符数据转成数值数据
If (mOpen = True) Then
If (CH341EppWriteAddr(mIndex, buffer, mLen) = False) Then
MsgBox "EPP写数据块1失败! ", vbExclamation, "CH341 "
End If
eppLen1.Text = Hex(mLen)
Else
MsgBox "设备未打开! ", vbExclamation, "CH341 "
End If
End Sub
**********
这里面用到的APICH341EppWriteAddr(mIndex, buffer, mLen)的定义如下
Declare Function CH341EppReadAddr Lib "CH341DLL.DLL " (ByVal iIndex As Long, ByRef oBuffer As Any, ByRef ioLength As Long) As Boolean
2.程序的公共模块(注意,问题主要出现在下面定义的结构类型上)
Option Explicit
Type arrRBuffer
buf(mMAX_BUFFER_LENGTH - 1) As Byte
End Type
Public Function mCharToBcd(ByVal iChar As String) As Byte ' 输入的ASCII字符
Dim mBCD As Byte
If iChar > = "0 " And iChar <= "9 " Then
mBCD = iChar - "0 "
ElseIf iChar > = "A " And iChar <= "F " Then
mBCD = Asc(iChar) - Asc( "A ") + &HA
ElseIf iChar > = "a " And iChar <= "f " Then
mBCD = Asc(iChar) - Asc( "a ") + &HA
Else
mBCD = &HFF
End If
mCharToBcd = mBCD
End Function
Sub mStrtoVal(str As String, ByRef strOut As arrRBuffer, strleng As Long)
Dim i, j As Long
Dim mLen As Long
Dim strRev(mMAX_BUFFER_LENGTH - 1) As Byte
mLen = strleng * 2
j = 0
For i = 0 To mLen - 1 Step 2
If (mCharToBcd(Mid(str, i + 1, 1)) = &HFF Or mCharToBcd(Mid(str, i + 2, 1)) = &HFF) Then
GoTo con
End If
' strRev(j) = mCharToBcd(Mid(str, i + 1, 1)) * 16 + mCharToBcd(Mid(str, i + 2, 1))
strRev(j) = mCharToBcd(Mid(str, i + 1, 1)) * 16 + mCharToBcd(Mid(str, i + 2, 1))
Debug.Print Hex(strRev(j))
j = j + 1
con: Next
j = 0
While (j < strleng)
strOut.buf(j) = strRev(j)
j = j + 1
Wend
End Sub
[解决办法]
Type arrRBuffer
buf(mMAX_BUFFER_LENGTH - 1) As Byte
End Type
------------------------------------------
VB.NET 下面:
Structure arrRBuffer
<MarshalAs(UnmanagedType.ByValArray,SizeConst=mMAX_BUFFER_LENGTH - 1)> _
Dim buf() as Byte;
End Structure
[解决办法]
转换器用不成的,手动转吧