以文本方式查看主题 - Foxtable(狐表) (http://foxtable.com/bbs/index.asp) -- 专家坐堂 (http://foxtable.com/bbs/list.asp?boardid=2) ---- vb代码在ft怎么用 (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=127534) |
||||||||||||
-- 作者:jaegea -- 发布时间:2018/11/16 9:04:00 -- vb代码在ft怎么用 请问以下vb代码如何修改可以在ft中使用
Declare Function API_PCDRead Lib "mi.dll" (ByVal handle As Integer, ByVal deviceAddr As Integer, ByVal mode As Byte, ByVal blk_Addr As Byte, ByVal Num_blk As Byte, ByRef snr As Byte, ByRef Buffer As Byte) As Integer \'\'\'读卡代码 Private Sub cmdRead_Click() Dim ret As Integer Dim blk_Addr, devAddr As Integer Dim mode, Num_blk As Byte \'Dim aKeyA(20) As Byte Dim asnr(20) As Byte Dim aRecvBuffer(2048) As Byte Dim strRet As String mode = &O0 \'设备地址,如果只有一个 devAddr = myVal(cboAddress.Text) \'读取开始地址 blk_Addr = Val(cboStartReadAdress.Text) \'读取模式 If Option1.Value = True And opReadKeyA.Value = True Then mode = &O0 If Option1.Value = True And opReadKeyB.Value = True Then mode = &O2 If opReadAll.Value = True And opReadKeyA.Value = True Then mode = &O1 If opReadAll.Value = True And opReadKeyB.Value = True Then mode = &O3 \'读取块数 Num_blk = Val(cboStartReadNumber.Text) \'一个指针,传递的是六个字节的密钥 ret = hexToBin(cboReadKey.Text, asnr) ret = API_PCDRead(hComm, devAddr, mode, blk_Addr, Num_blk, asnr(0), aRecvBuffer(0)) If ret = 0 Then lstEvents.AddItem ("读卡成功..") lstEvents.AddItem ("卡的序列号为: " + strByHex(asnr, 4)) lstEvents.AddItem (CStr(Now) + " 读卡数据为:") lstEvents.AddItem (strByHex(aRecvBuffer, Num_blk * 16)) Else If ret = Null Then lstEvents.AddItem (CStr(Now) + " 读卡出错,无数据接收...:") \'End If \'If ret = 1 Then \'lstEvents.AddItem (CStr(Now) + " false..") Else strRet = strByHex(aRecvBuffer, 1) lstEvents.AddItem (CStr(Now) + " 操作失败原因为: " + falsereason(strByHex(aRecvBuffer, 1))) strRet = falsereason(strByHex(aRecvBuffer, 1)) End If End If End Sub Function myVal(ByVal str As String) Dim retVal As Integer retVal = 0 If Len(str) = 1 Then If str >= "0" And str <= "9" Then retVal = Val(str) ElseIf str >= "a" And str <= "z" Then retVal = Asc(str) - Asc("a") + 10 ElseIf str >= "A" And str <= "Z" Then retVal = Asc(str) - Asc("A") + 10 End If End If myVal = retVal End Function Function hexToBin(ByVal str As String, ByRef Buffer() As Byte) Dim strRemain As String Dim firstChar As Boolean Dim i, count, inputLen, remainLen As Integer i = 0 count = 0 firstChar = True strRemain = str While Len(strRemain) > 0 If Mid(strRemain, 1, 1) = " " Then firstChar = True strRemain = Mid(strRemain, 2) ElseIf firstChar = True Then If Len(strRemain) = 1 Then Buffer(count) = myVal(strRemain) ElseIf Len(strRemain) >= 2 Then Buffer(count) = myVal(Mid(strRemain, 1, 1)) * 16 + myVal(Mid(strRemain, 2, 1)) strRemain = Mid(strRemain, 3) End If count = count + 1 firstChar = False Else strRemain = Mid(strRemain, 2) End If Wend hexToBin = count End Function \'从数组中取多少为 Function strByHex(ByRef Buffer() As Byte, ByVal bufferSize As Integer) Dim i As Integer Dim strRet, strSingle As String strRet = "" For i = 0 To bufferSize - 1 strSingle = Hex$(Buffer(i)) If Len(strSingle) = 1 Then strSingle = "0" & strSingle End If If bufferSize = 1 Then strRet = strSingle Else strRet = strRet + strSingle + " " End If Next strByHex = strRet End Function [此贴子已经被作者于2018/11/16 9:16:22编辑过]
|
||||||||||||
-- 作者:有点甜 -- 发布时间:2018/11/16 9:19:00 -- 1、全局代码
\'\'\'全局引用dll,引用的dll在附件中 Public Function myVal(ByVal str As String) retVal = 0 myVal = retVal
i = 0 Do While Len(strRemain) > 0 hexToBin = count \'从数组中取多少为 strRet = "" strByHex = strRet
2、调用代码,涉及到对应的控件,无法直接改写执行。请自行测试。 [此贴子已经被作者于2018/11/16 9:21:41编辑过]
|
||||||||||||
-- 作者:jaegea -- 发布时间:2018/11/16 9:32:00 -- Dim blk_Addr, devAddr As Integer Dim mode, Num_blk As Byte Dim asnr(20) As Byte \'\'\'\'定义一个20字节的指针 Dim aRecvBuffer(2048) As Byte \'\'\'\'\'\'\'定义一个2048字节的指针 Dim strRet As String \'一个指针,传递的是六个字节的密钥,传入密钥数据到缓冲区asnr ret = hexToBin(cboReadKey.Text, asnr) \'调用接口发送设备号(hComm),地址(devAddr),开始读取的地址(blk_Addr),读取模式(Num_blk),密钥(asnr(0)),接收数据的缓冲区(aRecvBuffer(0))给控件 ret = API_PCDRead(hComm, devAddr, mode, blk_Addr, Num_blk, asnr(0), aRecvBuffer(0)) 在这里面报“尝试读取或写入受保护的内存。这通常指示其他内存已损坏。”错误,请问怎么处理? |
||||||||||||
-- 作者:有点甜 -- 发布时间:2018/11/16 9:37:00 -- 改成
ret = API_PCDRead(hComm, devAddr, mode, blk_Addr, Num_blk, asnr, aRecvBuffer) |
||||||||||||
-- 作者:有点甜 -- 发布时间:2018/11/16 9:40:00 -- 全局代码改成
Declare Function API_PCDRead Lib "mi.dll" (ByVal handle As Integer, ByVal deviceAddr As Integer, ByVal mode As Byte, ByVal blk_Addr As Byte, ByVal Num_blk As Byte, ByRef snr As Byte(), ByRef Buffer As Byte()) As Integer |
||||||||||||
-- 作者:jaegea -- 发布时间:2018/11/16 10:15:00 -- 一样的错误 System.AccessViolationException: 尝试读取或写入受保护的内存。这通常指示其他内存已损坏。 在 UserCode.API_PCDRead(Int32 handle, Int32 deviceAddr, Byte mode, Byte blk_Addr, Byte Num_blk, Byte[]& snr, Byte[]& Buffer) 在 UserCode.Av1WhxpP8xXMvDH07(ControlEventArgs e) |
||||||||||||
-- 作者:有点甜 -- 发布时间:2018/11/16 10:28:00 -- 1、叫对方给你一个vb.net或者c#的实例,贴出对应代码。
2、试试改成
Declare Function API_PCDRead Lib "mi.dll" (ByVal handle As Integer, ByVal deviceAddr As Integer, ByVal mode As Byte, ByVal blk_Addr As Byte, ByVal Num_blk As Byte, ByRef snr As stringbuilder, ByRef Buffer As stringbuilder) As Integer
Dim asnr as New StringBuilder(20) \'\'\'\'定义一个20字节的指针
ret = API_PCDRead(hComm, devAddr, mode, blk_Addr, Num_blk, asnr, aRecvBuffer) |
||||||||||||
-- 作者:jaegea -- 发布时间:2018/11/16 10:55:00 -- 改成StringBuilder还是一样的错误,只有VC++、VB、 delphi的例子
中控CR10MW 通讯协议 [此贴子已经被作者于2018/11/16 11:02:00编辑过]
|
||||||||||||
-- 作者:有点甜 -- 发布时间:2018/11/16 11:28:00 -- 改成
Declare Function API_PCDRead Lib "mi.dll" (ByVal handle As Integer, ByVal deviceAddr As Integer, ByVal mode As Byte, ByVal blk_Addr As Byte, ByVal Num_blk As Byte, ByVal snr As stringbuilder, ByVal Buffer As stringbuilder) As Integer
或者
Declare Function API_PCDRead Lib "mi.dll" (ByVal handle As Integer, ByVal deviceAddr As Integer, ByVal mode As Byte, ByVal blk_Addr As Byte, ByVal Num_blk As Byte, ByVal snr As Byte(), ByVal Buffer As Byte()) As Integer |
||||||||||||
-- 作者:jaegea -- 发布时间:2018/11/16 13:31:00 -- System.AccessViolationException: 尝试读取或写入受保护的内存。这通常指示其他内存已损坏。 在 UserCode.API_PCDRead(Int32 handle, Int32 deviceAddr, Byte mode, Byte blk_Addr, Byte Num_blk, StringBuilder snr, StringBuilder Buffer) 在 UserCode.Av1WhxpP8xXMvDH07(ControlEventArgs e) Byte() 或 stringbuilder,ByVal 或 ByRef都是报这错误
|